static void led_set(uint8_t led, uint8_t state)
{
if (led == 0) {
- if (state) {
- PORTC |= _BV(PC5);
- led_bitmap |= 1;
- } else {
- PORTC &= ~_BV(PC5);
+ switch (state) {
+ case 0:
led_bitmap &= ~1;
+ led_bitmap &= ~2;
+ break;
+ case 1:
+ led_bitmap |= 1;
+ led_bitmap &= ~2;
+ break;
+ default: // error
+ led_bitmap |= 2;
+ break;
}
} else {
- if (state) {
- PORTB |= _BV(PB5);
- led_bitmap |= 2;
- } else {
- PORTB &= ~_BV(PB5);
- led_bitmap &= ~2;
+ switch (state) {
+ case 0:
+ led_bitmap &= ~4;
+ led_bitmap &= ~8;
+ break;
+ case 1:
+ led_bitmap |= 4;
+ led_bitmap &= ~8;
+ break;
+ default:
+ led_bitmap |= 8;
+ break;
}
}
}
+static void leds_update()
+{
+ if (led_bitmap & 1) {
+ PORTC |= _BV(PC5);
+ } else {
+ PORTC &= ~_BV(PC5);
+ }
+
+ if (led_bitmap & 2) {
+ DDRC &= ~_BV(PC5);
+ } else {
+ DDRC |= _BV(PC5);
+ }
+
+ if (led_bitmap & 4) {
+ PORTB |= _BV(PB5);
+ } else {
+ PORTB &= ~_BV(PB5);
+ }
+
+ if (led_bitmap & 8) {
+ DDRB |= _BV(PB5);
+ } else {
+ DDRB &= ~_BV(PB5);
+ }
+}
+
static void eval_bitmaps()
{
uint16_t free_b = 0, err_b = 0, mask;
err_bitmap = err_b;
if (led1_sensors) {
- if (led1_sensors & free_bitmap) {
+ if (led1_sensors & err_bitmap) {
+ led_set(0, 2);
+ } else if (led1_sensors & free_bitmap) {
led_set(0, 1);
} else {
led_set(0, 0);
}
if (led2_sensors) {
- if (led2_sensors & free_bitmap) {
+ if (led2_sensors & err_bitmap) {
+ led_set(1, 2);
+ } else if (led2_sensors & free_bitmap) {
led_set(1, 1);
} else {
led_set(1, 0);
}
eval_bitmaps();
+ leds_update(); // might be written from modbus
// led_set(0,
// distances[4] > 100 || distances[11] > 100);
}