UoS³ Flight Computer Firmware
 All Data Structures Files Functions Groups Pages
led.c
1 #include "../led.h"
2 
3 #include <avr/io.h>
4 
5 typedef struct LED {
6  volatile uint8_t *peripheral_register;
7  uint8_t peripheral;
8  volatile uint8_t *port;
9  uint8_t pin;
10  bool initialised;
11  enum { state_ON
12  , state_OFF
13  } state;
14 } LED;
15 
16 static LED LED_leds[4] =
17  { { &DDRC, DDC0, &PORTC, PC0, false, state_OFF } // LED 1
18  , { &DDRC, DDC1, &PORTC, PC1, false, state_OFF } // LED 2
19  , { &DDRC, DDC2, &PORTC, PC2, false, state_OFF } // LED 3
20  , { &DDRC, DDC3, &PORTC, PC3, false, state_OFF } // LED 4
21  };
22 #define NUMBER_OF_LEDS ( sizeof(LED_leds) / sizeof(LED) )
23 
24 /* Generic Utility functions */
25 static void LED_init(LED *led)
26 {
27  if(led->initialised)
28  return;
29 
30  *led->peripheral_register |= _BV(led->peripheral);
31 }
32 
33 void LED_on(uint8_t led_num)
34 {
35  if(led_num >= NUMBER_OF_LEDS)
36  return;
37 
38  LED_init(&LED_leds[led_num]);
39 
40  *LED_leds[led_num].port |= _BV(LED_leds[led_num].pin);
41 
42  LED_leds[led_num].state = state_ON;
43 }
44 
45 void LED_off(uint8_t led_num)
46 {
47  if(led_num >= NUMBER_OF_LEDS)
48  return;
49 
50  LED_init(&LED_leds[led_num]);
51 
52  *LED_leds[led_num].port &= ~(_BV(LED_leds[led_num].pin));
53 
54  LED_leds[led_num].state = state_OFF;
55 }
56 
57 void LED_set(uint8_t led_num, bool state)
58 {
59  if(led_num >= NUMBER_OF_LEDS)
60  return;
61 
62  if(state==true)
63  {
64  LED_on(led_num);
65  }
66  else
67  {
68  LED_off(led_num);
69  }
70 }
Definition: led.c:5
void LED_set(uint8_t led_num, bool state)
Definition: led.c:25
void LED_off(uint8_t led_num)
Definition: led.c:20
void LED_on(uint8_t led_num)
Definition: led.c:15