diff --git a/feedback/firmware/main.c b/feedback/firmware/main.c index 8e38607..881b366 100644 --- a/feedback/firmware/main.c +++ b/feedback/firmware/main.c @@ -5,6 +5,7 @@ #include static bool report_change = false; +static uint16_t saved_status; static void init() { // Set PB as input, pull-up off @@ -42,7 +43,7 @@ static void init() { sei(); } -static void send_status() { +static uint16_t get_status() { // Get GPIO state // PB -> invert -> Dend..Hend, Dempty..Fempty uint8_t statusl = ~PINB; @@ -53,6 +54,10 @@ static void send_status() { uint8_t swres = (pinc & _BV(4)) ? _BV(2) : 0; uint8_t statush = gempty | hempty | swres; + return ((uint16_t) statush << 8) | ((uint16_t) statusl); +} + +static void send_status(uint16_t status) { if (can_check_free_buffer()) { can_t msg = { .id = 0x10, @@ -60,13 +65,19 @@ static void send_status() { .rtr = 0, }, .length = 2, - .data = { statush, statusl, }, + .data = { (uint8_t) (status >> 8), status & 0xff, }, }; can_send_message(&msg); } } static void loop() { + // Check for status changes + uint16_t new_status = get_status(); + if (report_change && new_status != saved_status) { + send_status(new_status); + saved_status = new_status; + } // Check for new messages if (can_check_message()) { can_t msg = { 0 }; @@ -74,7 +85,7 @@ static void loop() { if (status != 0) { switch (msg.id) { case 0x10: - send_status(); + send_status(get_status()); break; case 0x4f: if (msg.length == 1) {