1 #include <avr/io.h> 2 #include <stddef.h> 3 4 #include "millis.h" 5 #include "scheduler.h" 6 7 #define BTN_A_PIN 2 8 #define BTN_B_PIN 3 9 #define LED_1_PIN 4 10 #define LED_2_PIN 5 11 #define LED_3_PIN 6 12 #define LED_4_PIN 7 13 14 void toggle_led_1(void); 15 void toggle_led_2(void); 16 void read_button_a(void); 17 void read_button_b(void); 18 19 typedef enum { 20 TOGGLE_LED_1_ID, 21 TOGGLE_LED_2_ID, 22 READ_BTN_A_ID, 23 READ_BTN_B_ID, 24 TASKS_COUNT 25 } task_id_e; 26 27 task_t tasks[] = { 28 // on_update period id 29 { toggle_led_1, 1000, TOGGLE_LED_1_ID }, 30 { toggle_led_2, 500, TOGGLE_LED_2_ID }, 31 { read_button_a, 0, READ_BTN_A_ID }, 32 { read_button_b, 0, READ_BTN_B_ID } 33 }; 34 35 void io_setup(void) { 36 // outputs 37 DDRD |= (1<<LED_1_PIN) | (1<<LED_2_PIN) | (1<<LED_3_PIN) | (1<<LED_4_PIN); 38 39 // inputs with internall pullup 40 DDRD &= ~((1<<BTN_A_PIN) | (1<<BTN_B_PIN)); 41 PORTD |= (1<<BTN_A_PIN) | (1<<BTN_B_PIN); 42 } 43 44 int main(void) { 45 io_setup(); 46 47 millis_init(&scheduler_tick); 48 49 scheduler_init(tasks, sizeof(tasks)/sizeof(tasks[0])); 50 51 while(1) { 52 scheduler_update(); 53 } 54 55 return 0; 56 } 57 58 void toggle_led_1(void) { 59 PIND = (1 << LED_1_PIN); 60 } 61 62 void toggle_led_2(void) { 63 PIND = (1 << LED_2_PIN); 64 } 65 66 void read_button_a(void) { 67 if (PIND & (1<<BTN_A_PIN)) { 68 PORTD &= ~(1<<LED_4_PIN); 69 scheduler_set_task_period(READ_BTN_A_ID, 0); 70 } else { 71 PORTD |= (1<<LED_4_PIN); 72 scheduler_set_task_period(READ_BTN_A_ID, 150); 73 } 74 } 75 76 void read_button_b(void) { 77 if (PIND & (1<<BTN_B_PIN)) { 78 PORTD &= ~(1<<LED_3_PIN); 79 } else { 80 PORTD |= (1<<LED_3_PIN); 81 } 82 }
