Skip to content

Commit

Permalink
Cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehaenger committed Oct 28, 2024
1 parent 12874d2 commit 485e0a8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
30 changes: 17 additions & 13 deletions Firmware/LowLevel/src/datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ enum HighLevelMode {
MODE_RECORDING = 3 // ROS connected, Manual mode during recording etc
};

#define LL_EMERGENCY_BIT_LATCH (1 << 0)
#define LL_EMERGENCY_BIT_LIFT (1 << 1) // Lift (or tilt)
#define LL_EMERGENCY_BIT_STOP (1 << 2) // Stop

// Need the old emergency_bitmask definition because CoverUI send it
#define LL_EMERGENCY_BIT_CU_LIFT 0b00001000 // LIFT | LIFTX (up to CoverUI FW 2.0x)
#define LL_EMERGENCY_BIT_CU_BUMP 0b00010000 // LBUMP | RBUMP (up to CoverUI FW 2.0x)
#define LL_EMERGENCY_BIT_CU_STOP1 0b00000010 // Stop1
#define LL_EMERGENCY_BIT_CU_STOP2 0b00000100 // Stop2
#define LL_EMERGENCY_BIT_CU_LIFTX 0b00100000 // CoverUI-LIFTX (as of CoverUI FW 2.1x)
#define LL_EMERGENCY_BIT_CU_RBUMP 0b01000000 // CoverUI-RBUMP (as of CoverUI FW 2.1x)

#define LL_STATUS_BIT_UI_AVAIL 0b10000000
// clang-format off
#define LL_EMERGENCY_BIT_LATCH (1 << 0) // Any emergency latch
#define LL_EMERGENCY_BIT_STOP (1 << 1) // Stop
#define LL_EMERGENCY_BIT_LIFT (1 << 2) // Lift (or tilt)

// CoverUI will stay with the old emergency_bitmask definition
#define LL_EMERGENCY_BIT_CU_LATCH (1 << 0) // Any emergency latch
#define LL_EMERGENCY_BIT_CU_STOP1 (1 << 1) // Stop1
#define LL_EMERGENCY_BIT_CU_STOP2 (1 << 2) // Stop2
#define LL_EMERGENCY_BIT_CU_LIFT (1 << 3) // LIFT | LIFTX (up to CoverUI FW 2.0x)
#define LL_EMERGENCY_BIT_CU_BUMP (1 << 4) // LBUMP | RBUMP (up to CoverUI FW 2.0x)
#define LL_EMERGENCY_BIT_CU_LIFTX (1 << 5) // CoverUI-LIFTX (as of CoverUI FW 2.1x)
#define LL_EMERGENCY_BIT_CU_RBUMP (1 << 6) // CoverUI-RBUMP (as of CoverUI FW 2.1x)

#define LL_STATUS_BIT_UI_AVAIL (1 << 7)
// clang-format on

#pragma pack(push, 1)
struct ll_status {
Expand Down Expand Up @@ -136,6 +139,7 @@ struct ConfigOptions {
bool ignore_charging_current : 1;
} __attribute__((packed));
#pragma pack(pop)
static_assert(sizeof(ConfigOptions) == 1, "Enlarging struct ConfigOption to a sizeof > 1 will break packet compatibilty");

typedef char iso639_1[2]; // Two char ISO 639-1 language code

Expand Down
15 changes: 7 additions & 8 deletions Firmware/LowLevel/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ uint16_t ui_interval = 1000; // UI send msg (LED/State) interval (
#define CONFIG_FILENAME "/openmower.cfg" // Where our llhl_config get saved in LittleFS (flash)
uint16_t config_crc_in_flash = 0;
struct ll_high_level_config llhl_config; // LL/HL configuration (is initialized with YF-C500 defaults)
static_assert(sizeof(ConfigOptions) == 1, "Enlarging struct ConfigOption to a sizeof > 1 will break packet compatibilty");

// Hall input sources, same order as in ll_high_level_config.hall_configs
const std::function<bool()> halls[MAX_HALL_INPUTS] = {
Expand Down Expand Up @@ -566,23 +565,23 @@ void sendConfigMessage(const uint8_t pkt_type) {
uint8_t *msg = (uint8_t *)malloc(msg_size);
if (msg == NULL)
return;
*msg = pkt_type;
msg[0] = pkt_type;
memcpy(msg + 1, &llhl_config, sizeof(struct ll_high_level_config)); // Copy our live config into the message, behind type
sendMessage(msg, msg_size);
sendMessage(msg, msg_size); // sendMessage() also calculate the packet CRC
free(msg);
}

/**
* @brief applyConfig applies those members who are not undefined/unknown.
* This function get called either when receiving a ll_high_level_config packet from HL, or during boot after read from LittleFS
* @param buffer
* @param size
* @param size of buffer (without packet type nor CRC)
*/
void applyConfig(const uint8_t *buffer, const size_t size) {
// This is a flexible length packet where the size may vary when ll_high_level_config struct get enhanced only on one side.
// If payload size is larger than our struct size, ensure that we only copy those we know of = our struct size.
// If payload size is larger than our struct size, ensure that we only copy those we know of (which is our struct size).
// If payload size is smaller than our struct size, copy only the payload we got, but ensure that the unsent member(s) have reasonable defaults.
size_t payload_size = min(sizeof(ll_high_level_config), size - 2); // exclude crc
size_t payload_size = min(sizeof(ll_high_level_config), size);

// Use a temporary rcv_config for easier member access.
// If payload is smaller (older), struct already contains reasonable defaults.
Expand Down Expand Up @@ -670,7 +669,7 @@ void onPacketReceived(const uint8_t *buffer, size_t size) {
// copy the state
last_high_level_state = *((struct ll_high_level_state *) buffer);
} else if (buffer[0] == PACKET_ID_LL_HIGH_LEVEL_CONFIG_REQ || buffer[0] == PACKET_ID_LL_HIGH_LEVEL_CONFIG_RSP) {
applyConfig(buffer + 1, size - 1); // Skip type
applyConfig(buffer + 1, size - 3); // Skip packet- type and CRC

// Store in flash
saveConfigToFlash();
Expand Down Expand Up @@ -898,6 +897,6 @@ void readConfigFromFlash() {
return;

config_crc_in_flash = crc;
applyConfig(buffer, size);
applyConfig(buffer, size - 2); // Skip CRC
free(buffer);
}

0 comments on commit 485e0a8

Please sign in to comment.