Revision 257
updated timer rtc_add_function and implemented a rtc_remove_function
branches/encoders/code/lib/include/libdragonfly/time.h | ||
---|---|---|
47 | 47 |
/** @brief Get the value of the realtime clock. **/ |
48 | 48 |
int rtc_get(void); |
49 | 49 |
|
50 |
int add_function(int prescale_opt, void *rtc_f (void));
|
|
50 |
int rtc_add_function(void (*rtc_f) (void), int prescale_opt);
|
|
51 | 51 |
|
52 |
int rtc_init(int prescale_opt, void *rtc_func(void)); |
|
52 |
int rtc_remove_function(void (*rtc_func)(void)); |
|
53 |
int rtc_get_function_count(void); |
|
53 | 54 |
|
55 |
int rtc_init(int prescale_opt, void (*rtc_func)(void)); |
|
56 |
|
|
54 | 57 |
/** @} **/ |
55 | 58 |
|
56 | 59 |
#endif |
branches/encoders/code/lib/src/libdragonfly/time.c | ||
---|---|---|
36 | 36 |
static volatile int _rtc_pulse[MAX_FUNCTIONS]; |
37 | 37 |
static volatile int _rtc_scale[MAX_FUNCTIONS]; |
38 | 38 |
static int function_count = 0; //Number of functions |
39 |
static void (*_rtc_f[MAX_FUNCTIONS])(void); |
|
40 |
|
|
39 |
static void (*_rtc_f[MAX_FUNCTIONS])(void); |
|
41 | 40 |
/** |
42 | 41 |
* @defgroup time Time |
43 | 42 |
* @brief Time functions |
... | ... | |
122 | 121 |
return argc-1; |
123 | 122 |
} |
124 | 123 |
|
125 |
int add_function(int prescale_opt, void *rtc_func (void))
|
|
124 |
int rtc_add_function(void (*rtc_func) (void), int prescale_opt)
|
|
126 | 125 |
{ |
127 |
if(function_count+1>=MAX_FUNCTIONS)
|
|
126 |
if(function_count>=MAX_FUNCTIONS) |
|
128 | 127 |
return -1; |
129 |
function_count++; |
|
130 | 128 |
_rtc_f[function_count] = rtc_func; |
131 | 129 |
_rtc_pulse[function_count] = 0; |
132 | 130 |
_rtc_scale[function_count] = prescale_opt; |
133 |
return 1; |
|
131 |
//return position in array that function was added to, increment count |
|
132 |
//this position may change if functions are removed |
|
133 |
return function_count++; |
|
134 | 134 |
} |
135 | 135 |
|
136 |
int rtc_init(int prescale_opt, void *rtc_func(void)){ |
|
136 |
//remove function pointer |
|
137 |
int rtc_remove_function(void (*rtc_func)(void)) |
|
138 |
{ |
|
139 |
for(int i = 0; i < function_count; i++) |
|
140 |
if(_rtc_f[i] == rtc_func) { |
|
141 |
//If not last function replace with last and reduce number of functions |
|
142 |
if(i < function_count-1) { |
|
143 |
_rtc_f[i] = _rtc_f[function_count-1]; |
|
144 |
_rtc_pulse[i] = _rtc_pulse[function_count-1]; |
|
145 |
_rtc_scale[i] = _rtc_scale[function_count-1]; |
|
146 |
} |
|
147 |
return --function_count; |
|
148 |
} |
|
149 |
|
|
150 |
return -1; |
|
151 |
} |
|
152 |
|
|
153 |
int rtc_get_function_count() |
|
154 |
{ |
|
155 |
return function_count; |
|
156 |
} |
|
157 |
|
|
158 |
int rtc_init(int prescale_opt, void (*rtc_func)(void)){ |
|
137 | 159 |
int temp[] = {prescale_opt}; |
138 | 160 |
void (*temp2[MAX_FUNCTIONS])(void) = {rtc_func}; |
139 | 161 |
return rtc_multi_init(temp, temp2, 1); |
... | ... | |
168 | 190 |
|
169 | 191 |
void rtc_reset_all(void) |
170 | 192 |
{ |
171 |
for(int i =0; i <MAX_FUNCTIONS; i++)
|
|
193 |
for(int i =0; i < function_count; i++)
|
|
172 | 194 |
_rtc_val[i] = 0; |
173 | 195 |
} |
174 | 196 |
|
branches/encoders/code/projects/template/main.c | ||
---|---|---|
1 | 1 |
#include <dragonfly_lib.h> |
2 | 2 |
|
3 |
|
|
3 | 4 |
void a1() |
4 | 5 |
{ |
5 | 6 |
usb_puts("a\n"); |
... | ... | |
47 | 48 |
|
48 | 49 |
void a10() |
49 | 50 |
{ |
50 |
usb_puts("A"); |
|
51 |
for(int i = 0; i <10; i++) |
|
52 |
{ |
|
53 |
buzzer_chirp(1000,C4); |
|
54 |
buzzer_chirp(1000,C5); |
|
51 |
usb_puts("A"); |
|
55 | 52 |
} |
56 |
} |
|
57 | 53 |
|
58 | 54 |
int main(void) |
59 | 55 |
{ |
60 | 56 |
dragonfly_init(ALL_ON); |
61 |
buzzer_init(); |
|
62 |
void (*functions[])(void) = {a1,a2,a3,a4,a5,a6,a7,a8,a9,a10}; |
|
63 |
int test[] = {1,2,3,4,5,6,7,8,9,1000}; |
|
64 |
rtc_multi_init(test, functions, 10); |
|
57 |
void (*functions[])(void) = {a1}; |
|
58 |
void (*allF[])(void) = {a1,a2,a3,a4,a5,a6,a7,a8,a9,a10}; |
|
59 |
int test[] = {20}; |
|
65 | 60 |
|
66 |
while(1){};
|
|
61 |
rtc_multi_init(test, functions, 1);
|
|
67 | 62 |
|
63 |
while(1){ |
|
64 |
if(button1_click() == 1) |
|
65 |
{ |
|
66 |
int time = rtc_get_function_count(); |
|
67 |
usb_puts("number of functions: "); |
|
68 |
usb_puti(rtc_add_function(allF[time], time*10)); |
|
69 |
usb_puts("\n"); |
|
70 |
} |
|
71 |
if(button2_click() == 1) |
|
72 |
{ |
|
73 |
int time = rtc_get_function_count(); |
|
74 |
if(time > 0) |
|
75 |
{ |
|
76 |
usb_puts("number of functions: "); |
|
77 |
usb_puti(rtc_remove_function(allF[time-1])); |
|
78 |
usb_puts("\n"); |
|
79 |
} |
|
80 |
} |
|
81 |
}; |
|
82 |
|
|
68 | 83 |
return 0; |
69 | 84 |
|
70 | 85 |
} |
branches/encoders/code/projects/template/Makefile | ||
---|---|---|
14 | 14 |
# USE_WIRELESS = 1 |
15 | 15 |
|
16 | 16 |
# com1 = serial port. Use lpt1 to connect to parallel port. |
17 |
AVRDUDE_PORT = com1
|
|
17 |
AVRDUDE_PORT = com2
|
|
18 | 18 |
# |
19 | 19 |
# |
20 | 20 |
################################### |
branches/encoders/code/projects/libdragonfly/time.c | ||
---|---|---|
36 | 36 |
static volatile int _rtc_pulse[MAX_FUNCTIONS]; |
37 | 37 |
static volatile int _rtc_scale[MAX_FUNCTIONS]; |
38 | 38 |
static int function_count = 0; //Number of functions |
39 |
static void (*_rtc_f[MAX_FUNCTIONS])(void); |
|
40 |
|
|
39 |
static void (*_rtc_f[MAX_FUNCTIONS])(void); |
|
41 | 40 |
/** |
42 | 41 |
* @defgroup time Time |
43 | 42 |
* @brief Time functions |
... | ... | |
122 | 121 |
return argc-1; |
123 | 122 |
} |
124 | 123 |
|
125 |
int add_function(int prescale_opt, void *rtc_func (void))
|
|
124 |
int rtc_add_function(void (*rtc_func) (void), int prescale_opt)
|
|
126 | 125 |
{ |
127 |
if(function_count+1>=MAX_FUNCTIONS)
|
|
126 |
if(function_count>=MAX_FUNCTIONS) |
|
128 | 127 |
return -1; |
129 |
function_count++; |
|
130 | 128 |
_rtc_f[function_count] = rtc_func; |
131 | 129 |
_rtc_pulse[function_count] = 0; |
132 | 130 |
_rtc_scale[function_count] = prescale_opt; |
133 |
return 1; |
|
131 |
//return position in array that function was added to, increment count |
|
132 |
//this position may change if functions are removed |
|
133 |
return function_count++; |
|
134 | 134 |
} |
135 | 135 |
|
136 |
int rtc_init(int prescale_opt, void *rtc_func(void)){ |
|
136 |
//remove function pointer |
|
137 |
int rtc_remove_function(void (*rtc_func)(void)) |
|
138 |
{ |
|
139 |
for(int i = 0; i < function_count; i++) |
|
140 |
if(_rtc_f[i] == rtc_func) { |
|
141 |
//If not last function replace with last and reduce number of functions |
|
142 |
if(i < function_count-1) { |
|
143 |
_rtc_f[i] = _rtc_f[function_count-1]; |
|
144 |
_rtc_pulse[i] = _rtc_pulse[function_count-1]; |
|
145 |
_rtc_scale[i] = _rtc_scale[function_count-1]; |
|
146 |
} |
|
147 |
return --function_count; |
|
148 |
} |
|
149 |
|
|
150 |
return -1; |
|
151 |
} |
|
152 |
|
|
153 |
int rtc_get_function_count() |
|
154 |
{ |
|
155 |
return function_count; |
|
156 |
} |
|
157 |
|
|
158 |
int rtc_init(int prescale_opt, void (*rtc_func)(void)){ |
|
137 | 159 |
int temp[] = {prescale_opt}; |
138 | 160 |
void (*temp2[MAX_FUNCTIONS])(void) = {rtc_func}; |
139 | 161 |
return rtc_multi_init(temp, temp2, 1); |
... | ... | |
168 | 190 |
|
169 | 191 |
void rtc_reset_all(void) |
170 | 192 |
{ |
171 |
for(int i =0; i <MAX_FUNCTIONS; i++)
|
|
193 |
for(int i =0; i < function_count; i++)
|
|
172 | 194 |
_rtc_val[i] = 0; |
173 | 195 |
} |
174 | 196 |
|
branches/encoders/code/projects/libdragonfly/time.h | ||
---|---|---|
47 | 47 |
/** @brief Get the value of the realtime clock. **/ |
48 | 48 |
int rtc_get(void); |
49 | 49 |
|
50 |
int add_function(int prescale_opt, void *rtc_f (void));
|
|
50 |
int rtc_add_function(void (*rtc_f) (void), int prescale_opt);
|
|
51 | 51 |
|
52 |
int rtc_init(int prescale_opt, void *rtc_func(void)); |
|
52 |
int rtc_remove_function(void (*rtc_func)(void)); |
|
53 |
int rtc_get_function_count(void); |
|
53 | 54 |
|
55 |
int rtc_init(int prescale_opt, void (*rtc_func)(void)); |
|
56 |
|
|
54 | 57 |
/** @} **/ |
55 | 58 |
|
56 | 59 |
#endif |
Also available in: Unified diff