Implement change report on feedback module

This commit is contained in:
Gregor Riepl 2021-01-18 20:27:20 +01:00
parent d63860df8f
commit 3791dfa777

View file

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