|
| 1 | +#ifndef _UI_DATATYPES_H_ |
| 2 | +#define _UI_DATATYPES_H_ |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | + |
| 6 | +#include <cmath> |
| 7 | +// Protocol Header Info |
| 8 | +enum TYPE { |
| 9 | + Get_Version = 0xB0, |
| 10 | + Set_Buzzer = 0xB1, |
| 11 | + Set_LEDs = 0xB2, |
| 12 | + Get_Button = 0xB3, |
| 13 | + Get_Emergency = 0xB4, // Stock-CoverUI |
| 14 | + Get_Rain = 0xB5, // Stock-CoverUI |
| 15 | + Get_Subscribe = 0xB6 |
| 16 | +}; |
| 17 | + |
| 18 | +// Function definitions for the 18 LEDS |
| 19 | +enum LED_id { |
| 20 | + LED_CHARGING = 0, |
| 21 | + LED_BATTERY_LOW = 1, |
| 22 | + LED_POOR_GPS = 2, |
| 23 | + LED_MOWER_LIFTED = 3, |
| 24 | + LED5 = 4, |
| 25 | + LED6 = 5, |
| 26 | + LED7 = 6, |
| 27 | + LED8 = 7, |
| 28 | + LED9 = 8, |
| 29 | + LED10 = 9, |
| 30 | + LED11 = 10, |
| 31 | + LED_LOCK = 11, |
| 32 | + LED_S2 = 12, |
| 33 | + LED_S1 = 13, |
| 34 | + LED15 = 14, |
| 35 | + LED16 = 15, |
| 36 | + LED17 = 16, |
| 37 | + LED18 = 17 |
| 38 | +}; |
| 39 | + |
| 40 | +enum LED_state { LED_off = 0b000, LED_blink_slow = 0b101, LED_blink_fast = 0b110, LED_on = 0b111 }; |
| 41 | + |
| 42 | +// Stock-CoverUI (same bitmask as in ll_status.emergency_bitmask of datatypes.h) |
| 43 | +enum Emergency_state { |
| 44 | + Emergency_latch = 0b00001, |
| 45 | + Emergency_stop1 = 0b00010, |
| 46 | + Emergency_stop2 = 0b00100, |
| 47 | + Emergency_lift1 = 0b01000, |
| 48 | + Emergency_lift2 = 0b10000 |
| 49 | +}; |
| 50 | + |
| 51 | +enum Topic_state { |
| 52 | + Topic_set_leds = 1 << 0, |
| 53 | + Topic_set_ll_status = 1 << 1, |
| 54 | + Topic_set_hl_state = 1 << 2, |
| 55 | +}; |
| 56 | + |
| 57 | +#pragma pack(push, 1) |
| 58 | +struct msg_get_version { |
| 59 | + uint8_t type; // command type |
| 60 | + uint8_t reserved; // padding |
| 61 | + uint16_t version; |
| 62 | + uint16_t crc; // CRC 16 |
| 63 | +} __attribute__((packed)); |
| 64 | +#pragma pack(pop) |
| 65 | + |
| 66 | +#pragma pack(push, 1) |
| 67 | +struct msg_set_buzzer { |
| 68 | + uint8_t type; // command type |
| 69 | + uint8_t repeat; // Repeat X times |
| 70 | + uint8_t on_time; // Signal on time |
| 71 | + uint8_t off_time; // Signal off time |
| 72 | + uint16_t crc; // CRC 16 |
| 73 | +} __attribute__((packed)); |
| 74 | +#pragma pack(pop) |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief Use this to update the LED matrix |
| 78 | + * Each LED gets three bits with the following meaning: |
| 79 | + * 0b000 = Off |
| 80 | + * 0b001 = reserved for future use |
| 81 | + * 0b010 = reserved for future use |
| 82 | + * 0b011 = reserved for future use |
| 83 | + * 0b100 = reserved for future use |
| 84 | + * 0b101 = On slow blink |
| 85 | + * 0b110 = On fast blink |
| 86 | + * 0b111 = On |
| 87 | + */ |
| 88 | +#pragma pack(push, 1) |
| 89 | +struct msg_set_leds { |
| 90 | + uint8_t type; // command type |
| 91 | + uint8_t reserved; // padding |
| 92 | + uint64_t leds; |
| 93 | + uint16_t crc; // CRC 16 |
| 94 | +} __attribute__((packed)); |
| 95 | +#pragma pack(pop) |
| 96 | + |
| 97 | +#pragma pack(push, 1) |
| 98 | +struct msg_event_button { |
| 99 | + uint8_t type; // command type |
| 100 | + uint16_t button_id; |
| 101 | + uint8_t press_duration; |
| 102 | + uint16_t crc; // CRC 16 |
| 103 | +} __attribute__((packed)); |
| 104 | +#pragma pack(pop) |
| 105 | + |
| 106 | +// Stock-CoverUI |
| 107 | +#pragma pack(push, 1) |
| 108 | +struct msg_event_rain { |
| 109 | + uint8_t type; // Command type |
| 110 | + uint8_t reserved; // Padding |
| 111 | + uint32_t value; |
| 112 | + uint32_t threshold; // If value < threshold then it rains. Why a threshold? Cause there might be a future option to |
| 113 | + // make it configurable on Stock-CoverUI |
| 114 | + uint16_t crc; // CRC 16 |
| 115 | +} __attribute__((packed)); |
| 116 | +#pragma pack(pop) |
| 117 | + |
| 118 | +// Stock-CoverUI |
| 119 | +#pragma pack(push, 1) |
| 120 | +struct msg_event_emergency { |
| 121 | + uint8_t type; // Command type |
| 122 | + uint8_t state; // Same as in ll_status.emergency_bitmask of datatypes.h |
| 123 | + uint16_t crc; // CRC 16 |
| 124 | +} __attribute__((packed)); |
| 125 | +#pragma pack(pop) |
| 126 | + |
| 127 | +// Cover UI might subscribe in what data it's interested to receive |
| 128 | + |
| 129 | +#pragma pack(push, 1) |
| 130 | +struct msg_event_subscribe { |
| 131 | + uint8_t type; // Command type |
| 132 | + uint8_t topic_bitmask; // Bitmask of data subscription(s), see Topic_state |
| 133 | + uint16_t interval; // Interval (ms) how often to send topic(s) |
| 134 | + uint16_t crc; // CRC 16 |
| 135 | +} __attribute__((packed)); |
| 136 | +#pragma pack(pop) |
| 137 | + |
| 138 | +inline void setLed(struct msg_set_leds &msg, int led, uint8_t state) { |
| 139 | + // mask the current led state |
| 140 | + uint64_t mask = ~(((uint64_t)0b111) << (led * 3)); |
| 141 | + msg.leds &= mask; |
| 142 | + msg.leds |= ((uint64_t)(state & 0b111)) << (led * 3); |
| 143 | +} |
| 144 | + |
| 145 | +inline void setBars7(struct msg_set_leds &msg, double value) { |
| 146 | + int on_leds = round(value * 7.0); |
| 147 | + for (int i = 0; i < 7; i++) { |
| 148 | + setLed(msg, LED11 - i, i < on_leds ? LED_on : LED_off); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +inline void setBars4(struct msg_set_leds &msg, double value) { |
| 153 | + if (value < 0) { |
| 154 | + for (int i = 0; i < 4; i++) { |
| 155 | + setLed(msg, i + LED15, LED_blink_fast); |
| 156 | + } |
| 157 | + } else { |
| 158 | + int on_leds = round(value * 4.0); |
| 159 | + for (int i = 0; i < 4; i++) { |
| 160 | + setLed(msg, LED18 - i, i < on_leds ? LED_on : LED_off); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +#endif // _UI_DATATYPES_H_ |
0 commit comments