Skip to content

Commit

Permalink
Allow configuration of standard (debug) LED on different pin and port.
Browse files Browse the repository at this point in the history
  • Loading branch information
breaker27 committed Jan 27, 2023
1 parent 8bfa807 commit 4f0768f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
31 changes: 21 additions & 10 deletions firmware/src_common/util_hw.c
Expand Up @@ -43,9 +43,12 @@
#include "rfm12.h"
#include "aes256.h"

#define LED_PIN 7
#define LED_PORT PORTD
#define LED_DDR DDRD
#define LED_PIN_DEFAULT 7
#define LED_PORT_DEFAULT PORTD
#define LED_DDR_DEFAULT DDRD

static volatile uint8_t* led_port;
static uint8_t led_pin;

// Value has to be volatile, because otherwise the adc_measure function would not
// detect that the value was changed from the ADC interrupt.
Expand Down Expand Up @@ -221,27 +224,35 @@ uint16_t read_battery(void)
return (uint16_t)((uint32_t)1100 * 1024 / read_adc(14));
}

void util_init_led(volatile uint8_t* ddr, volatile uint8_t* port, uint8_t pin)
{
led_port = port;
led_pin = pin;

sbi(*ddr, pin);
}

void util_init(void)
{
sbi(LED_DDR, LED_PIN);
util_init_led(&LED_DDR_DEFAULT, &LED_PORT_DEFAULT, LED_PIN_DEFAULT);
}

void led_dbg(uint8_t ms)
{
sbi(LED_PORT, LED_PIN);
sbi(*led_port, led_pin);
_delay_ms(ms);
cbi(LED_PORT, LED_PIN);
cbi(*led_port, led_pin);
}

void switch_led(bool b_on)
{
if (b_on)
{
sbi(LED_PORT, LED_PIN);
sbi(*led_port, led_pin);
}
else
{
cbi(LED_PORT, LED_PIN);
cbi(*led_port, led_pin);
}
}

Expand All @@ -251,9 +262,9 @@ void led_blink(uint16_t on, uint16_t off, uint8_t times)

for (i = 0; i < times; i++)
{
sbi(LED_PORT, LED_PIN);
sbi(*led_port, led_pin);
_delay_ms(on);
cbi(LED_PORT, LED_PIN);
cbi(*led_port, led_pin);
_delay_ms(off);
}
}
Expand Down
1 change: 1 addition & 0 deletions firmware/src_common/util_hw.h
Expand Up @@ -48,6 +48,7 @@ uint16_t read_adc(uint8_t adc_input);
uint16_t read_battery(void);

void util_init(void);
void util_init_led(volatile uint8_t* ddr, volatile uint8_t* port, uint8_t pin);
void led_dbg(uint8_t ms);
void switch_led(bool b_on);
void led_blink(uint16_t on, uint16_t off, uint8_t times);
Expand Down

0 comments on commit 4f0768f

Please sign in to comment.