Skip to content

Commit ed41bd1

Browse files
ClemensElfleinrovo89
authored andcommitted
wip cover UI
1 parent 9535f45 commit ed41bd1

File tree

8 files changed

+596
-2
lines changed

8 files changed

+596
-2
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE
8989
src/drivers/input/gpio_input_driver.cpp
9090
src/drivers/input/worx_input_driver.cpp
9191
$<$<CONFIG:Debug>:src/drivers/input/simulated_input_driver.cpp>
92+
# YardForce Cover UI Driver
93+
src/drivers/ui/YardForceCoverUI/yard_force_cover_ui_driver.cpp
9294
# Raw driver debug interface
9395
src/debug/debug_tcp_interface.cpp
9496
src/debug/debug_udp_interface.cpp

robots/include/yardforce_robot.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define YARDFORCE_ROBOT_HPP
33

44
#include <drivers/charger/bq_2576/bq_2576.hpp>
5+
#include <drivers/ui/YardForceCoverUI/yard_force_cover_ui_driver.hpp>
56

67
#include "robot.hpp"
78

@@ -36,6 +37,7 @@ class YardForceRobot : public MowerRobot {
3637

3738
private:
3839
BQ2576 charger_{};
40+
YardForceCoverUIDriver cover_ui_driver_{};
3941
};
4042

4143
#endif // YARDFORCE_ROBOT_HPP

robots/src/yardforce_robot.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void YardForceRobot::InitPlatform() {
66
InitMotors();
77
charger_.setI2C(&I2CD1);
88
power_service.SetDriver(&charger_);
9+
cover_ui_driver_.Start(&UARTD7);
910
}
1011

1112
bool YardForceRobot::IsHardwareSupported() {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Copyright (c) 2011 Christopher Baker <https://christopherbaker.net>
3+
// Copyright (c) 2011 Jacques Fortier <https://github.com/jacquesf/COBS-Consistent-Overhead-Byte-Stuffing>
4+
//
5+
// SPDX-License-Identifier: MIT
6+
//
7+
8+
#ifndef SRC_COBS_H
9+
#define SRC_COBS_H
10+
11+
/// \brief A Consistent Overhead Byte Stuffing (COBS) Encoder.
12+
///
13+
/// Consistent Overhead Byte Stuffing (COBS) is an encoding that removes all 0
14+
/// bytes from arbitrary binary data. The encoded data consists only of bytes
15+
/// with values from 0x01 to 0xFF. This is useful for preparing data for
16+
/// transmission over a serial link (RS-232 or RS-485 for example), as the 0
17+
/// byte can be used to unambiguously indicate packet boundaries. COBS also has
18+
/// the advantage of adding very little overhead (at least 1 byte, plus up to an
19+
/// additional byte per 254 bytes of data). For messages smaller than 254 bytes,
20+
/// the overhead is constant.
21+
///
22+
/// \sa http://conferences.sigcomm.org/sigcomm/1997/papers/p062.pdf
23+
/// \sa http://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
24+
/// \sa https://github.com/jacquesf/COBS-Consistent-Overhead-Byte-Stuffing
25+
/// \sa http://www.jacquesf.com/2011/03/consistent-overhead-byte-stuffing
26+
class COBS {
27+
public:
28+
/// \brief Encode a byte buffer with the COBS encoder.
29+
/// \param buffer A pointer to the unencoded buffer to encode.
30+
/// \param size The number of bytes in the \p buffer.
31+
/// \param encodedBuffer The buffer for the encoded bytes.
32+
/// \returns The number of bytes written to the \p encodedBuffer.
33+
/// \warning The encodedBuffer must have at least getEncodedBufferSize()
34+
/// allocated.
35+
static size_t encode(const uint8_t* buffer, size_t size, uint8_t* encodedBuffer) {
36+
size_t read_index = 0;
37+
size_t write_index = 1;
38+
size_t code_index = 0;
39+
uint8_t code = 1;
40+
41+
while (read_index < size) {
42+
if (buffer[read_index] == 0) {
43+
encodedBuffer[code_index] = code;
44+
code = 1;
45+
code_index = write_index++;
46+
read_index++;
47+
} else {
48+
encodedBuffer[write_index++] = buffer[read_index++];
49+
code++;
50+
51+
if (code == 0xFF) {
52+
encodedBuffer[code_index] = code;
53+
code = 1;
54+
code_index = write_index++;
55+
}
56+
}
57+
}
58+
59+
encodedBuffer[code_index] = code;
60+
61+
return write_index;
62+
}
63+
64+
/// \brief Decode a COBS-encoded buffer.
65+
/// \param encodedBuffer A pointer to the \p encodedBuffer to decode.
66+
/// \param size The number of bytes in the \p encodedBuffer.
67+
/// \param decodedBuffer The target buffer for the decoded bytes.
68+
/// \returns The number of bytes written to the \p decodedBuffer.
69+
/// \warning decodedBuffer must have a minimum capacity of size.
70+
static size_t decode(const uint8_t* encodedBuffer, size_t size, uint8_t* decodedBuffer) {
71+
if (size == 0) return 0;
72+
73+
size_t read_index = 0;
74+
size_t write_index = 0;
75+
uint8_t code = 0;
76+
uint8_t i = 0;
77+
78+
while (read_index < size) {
79+
code = encodedBuffer[read_index];
80+
81+
if (read_index + code > size && code != 1) {
82+
return 0;
83+
}
84+
85+
read_index++;
86+
87+
for (i = 1; i < code; i++) {
88+
decodedBuffer[write_index++] = encodedBuffer[read_index++];
89+
}
90+
91+
if (code != 0xFF && read_index != size) {
92+
decodedBuffer[write_index++] = '\0';
93+
}
94+
}
95+
96+
return write_index;
97+
}
98+
99+
/// \brief Get the maximum encoded buffer size for an unencoded buffer size.
100+
/// \param unencodedBufferSize The size of the buffer to be encoded.
101+
/// \returns the maximum size of the required encoded buffer.
102+
static size_t getEncodedBufferSize(size_t unencodedBufferSize) {
103+
return unencodedBufferSize + unencodedBufferSize / 254 + 1;
104+
}
105+
};
106+
107+
#endif
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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

Comments
 (0)