mirror of
https://github.com/randybb/esphome-configs.git
synced 2026-12-06 09:06:52 +01:00
303 lines
9.4 KiB
YAML
303 lines
9.4 KiB
YAML
---
|
|
# Hardware: M5Stack AtomS3 + ATOMIC CAN base + CardKB on the Atom's Grove port
|
|
# CANopen keypad prototype (node 3): CardKB key code + press counter in TPDO 0 (canopen from ESPHome dev + canopen_entities)
|
|
# plus a Blink Marine PKP 2600SI (2x6) emulation as a second local node 0x15: keys 1-6 / q-y, LED RPDOs drawn on the display
|
|
# Pinout:
|
|
# GPIO1 Grove SCL -> i2c_grove (i2c scl)
|
|
# GPIO2 Grove SDA -> i2c_grove (i2c sda)
|
|
# GPIO5 -> can_bus (canbus esp32_can tx_pin)
|
|
# GPIO6 -> can_bus (canbus esp32_can rx_pin)
|
|
# GPIO15 -> display_tft (display mipi_spi cs_pin)
|
|
# GPIO16 -> gpio_tft_backlight (output ledc)
|
|
# GPIO17 -> spi (spi clk_pin)
|
|
# GPIO21 -> spi (spi mosi_pin)
|
|
# GPIO33 -> display_tft (display mipi_spi dc_pin)
|
|
# GPIO34 -> display_tft (display mipi_spi reset_pin)
|
|
# GPIO41 -> Button (binary_sensor gpio) (inverted)
|
|
# I2C:
|
|
# 0x5F cardkb (m5stack_keyboard), CardKB
|
|
substitutions:
|
|
device: atoms3
|
|
name: AtomS3
|
|
area: DEV
|
|
comment: CANopen keypad
|
|
|
|
esphome:
|
|
name: mcu-${device}
|
|
friendly_name: ${name}
|
|
area: ${area}
|
|
comment: ${comment}
|
|
on_boot:
|
|
- lambda: |-
|
|
id(cardkb).add_on_key_callback([](uint8_t key) {
|
|
id(key_code).publish_state(key);
|
|
float n = id(key_count).state; // NaN before the first key
|
|
uint32_t count = std::isnan(n) ? 1 : ((uint32_t) n + 1) % 65535;
|
|
id(key_count).publish_state(count);
|
|
id(key_event).publish_state((count % 255) * 256 + key); // one field: count (high byte) + key (low byte)
|
|
// PKP: keys 1-6 = top row, q w e r t y = bottom row (keys 7-12); CardKB has no release -> 200 ms press
|
|
static const char *const PKP_KEYS = "123456qwerty";
|
|
const char *p = key ? strchr(PKP_KEYS, key) : nullptr;
|
|
if (p != nullptr) {
|
|
id(pkp_keys) = 1u << (p - PKP_KEYS);
|
|
id(pkp_send_keys).execute();
|
|
}
|
|
id(display_tft).update();
|
|
});
|
|
// PKP RPDOs: 0x200 LED on, 0x300 LED blink (40 bits: R1-12, G1-12, B1-12), 0x400 brightness 0x00-0x3F
|
|
static canopen::FrameListener pkp_rx{0x015, 0x0FF, [](void *, uint32_t can_id, const uint8_t *d, uint8_t len) {
|
|
if (id(pkp).get_nmt_state() != canopen::NMTState::NMT_STATE_OPERATIONAL)
|
|
return;
|
|
uint64_t bits = 0;
|
|
for (uint8_t i = 0; i < len && i < 5; i++)
|
|
bits |= (uint64_t) d[i] << (8 * i);
|
|
if (can_id == 0x215)
|
|
id(pkp_led_on) = bits;
|
|
else if (can_id == 0x315)
|
|
id(pkp_led_blink) = bits;
|
|
else if (can_id == 0x415 && len > 0)
|
|
id(pkp_brightness) = d[0] > 0x3F ? 0x3F : d[0];
|
|
else
|
|
return;
|
|
id(display_tft).update();
|
|
}, nullptr};
|
|
id(pkp).add_frame_listener(pkp_rx);
|
|
|
|
esp32:
|
|
variant: esp32s3
|
|
flash_size: 8MB
|
|
toolchain: esp-idf
|
|
framework:
|
|
advanced:
|
|
flash_chip: gd
|
|
type: esp-idf
|
|
|
|
external_components:
|
|
- source: github://mrk-its/esphome-canopen@canopen_entities
|
|
components: [canopen_entities]
|
|
refresh: 1h
|
|
- source: github://randybb/esphome-components
|
|
components: [m5stack_keyboard]
|
|
refresh: 1h
|
|
# TEMP: esphome#23831 (OTA over CANopen, CiA 302-3); drop once merged
|
|
- source: github://pr#23831
|
|
components: [canopen]
|
|
refresh: 1h
|
|
|
|
packages:
|
|
common: !include common/common.yaml
|
|
ipv6_only: !include common/ipv6_only.yaml
|
|
status_led: !include
|
|
file: common/status_led.yaml
|
|
vars:
|
|
light_id: status_frame
|
|
|
|
logger:
|
|
logs:
|
|
# canbus logs every frame at DEBUG, which slows the loop during SDO block transfers
|
|
canbus: INFO
|
|
|
|
i2c:
|
|
- id: i2c_grove
|
|
sda: 2
|
|
scl: 1
|
|
|
|
m5stack_keyboard:
|
|
- id: cardkb
|
|
i2c_id: i2c_grove
|
|
address: 0x5F
|
|
|
|
canbus:
|
|
- platform: esp32_can
|
|
id: can_bus
|
|
rx_pin: 6
|
|
tx_pin: 5
|
|
can_id: 3
|
|
bit_rate: 500kbps
|
|
# IDF default is 5 frames; an SDO block (16 segments in ~8 ms) overflowed it (EMCY 0x8110, partial block acks)
|
|
rx_queue_len: 32
|
|
|
|
canopen:
|
|
- id: can_open
|
|
canbus_id: can_bus
|
|
node_id: 3
|
|
|
|
# Blink Marine PKP 2600SI emulation (docs: home-gitops canopen-nodes.adoc): like the real one, not active until NMT Start
|
|
- id: pkp
|
|
canbus_id: can_bus
|
|
node_id: 0x15
|
|
nmt_startup: pre_operational
|
|
device_name: PKP2600SI
|
|
|
|
globals:
|
|
- id: pkp_keys
|
|
type: uint16_t
|
|
initial_value: '0'
|
|
- id: pkp_led_on
|
|
type: uint64_t
|
|
initial_value: '0'
|
|
- id: pkp_led_blink
|
|
type: uint64_t
|
|
initial_value: '0'
|
|
- id: pkp_brightness
|
|
type: uint8_t
|
|
initial_value: '0x3F'
|
|
|
|
script:
|
|
# keys state TPDO 0x195: bytes 0-1 keys 1-12, byte 4 tick (+1 per 100 ms); pressed now, released 200 ms later
|
|
- id: pkp_send_keys
|
|
mode: restart
|
|
then:
|
|
- lambda: |-
|
|
if (id(pkp).get_nmt_state() != canopen::NMTState::NMT_STATE_OPERATIONAL)
|
|
return;
|
|
uint8_t d[5] = {(uint8_t) id(pkp_keys), (uint8_t) (id(pkp_keys) >> 8), 0, 0, (uint8_t) (millis() / 100)};
|
|
id(pkp).send_frame(0x195, d, sizeof(d));
|
|
- delay: 200ms
|
|
- lambda: |-
|
|
id(pkp_keys) = 0;
|
|
if (id(pkp).get_nmt_state() != canopen::NMTState::NMT_STATE_OPERATIONAL)
|
|
return;
|
|
uint8_t d[5] = {0, 0, 0, 0, (uint8_t) (millis() / 100)};
|
|
id(pkp).send_frame(0x195, d, sizeof(d));
|
|
|
|
ota:
|
|
- platform: canopen
|
|
canopen_id: can_open
|
|
|
|
# CardKB sends only the press (Shift/Sym/Fn resolved in the CardKB): a consumer reacts to a change of key_count
|
|
# and reads the key from key_code (both in the same frame), or watches key_event alone (count % 255 << 8 | key)
|
|
canopen_entities:
|
|
canopen_id: can_open
|
|
entities:
|
|
- {id: key_code, index: 1, tpdo: 0, size: 1}
|
|
- {id: key_count, index: 2, tpdo: 0, size: 2}
|
|
- {id: side_button, index: 3, tpdo: 0}
|
|
- {id: key_event, index: 4, tpdo: 0, size: 2}
|
|
|
|
sensor:
|
|
- platform: template
|
|
id: key_code
|
|
name: Key Code
|
|
accuracy_decimals: 0
|
|
update_interval: never
|
|
- platform: template
|
|
id: key_count
|
|
name: Key Count
|
|
accuracy_decimals: 0
|
|
update_interval: never
|
|
- platform: template
|
|
id: key_event
|
|
name: Key Event
|
|
accuracy_decimals: 0
|
|
update_interval: never
|
|
|
|
binary_sensor:
|
|
- platform: gpio
|
|
id: side_button
|
|
name: Button
|
|
pin:
|
|
number: 41
|
|
inverted: true
|
|
|
|
output:
|
|
- platform: ledc
|
|
pin: 16
|
|
id: gpio_tft_backlight
|
|
# virtual RGB light for common/status_led.yaml, shown full screen on the display
|
|
- platform: template
|
|
id: status_frame_r
|
|
type: float
|
|
write_action: []
|
|
- platform: template
|
|
id: status_frame_g
|
|
type: float
|
|
write_action: []
|
|
- platform: template
|
|
id: status_frame_b
|
|
type: float
|
|
write_action: []
|
|
|
|
light:
|
|
- platform: monochromatic
|
|
id: display_tft_backlight
|
|
output: gpio_tft_backlight
|
|
restore_mode: ALWAYS_ON
|
|
- platform: rgb
|
|
id: status_frame
|
|
internal: true
|
|
red: status_frame_r
|
|
green: status_frame_g
|
|
blue: status_frame_b
|
|
restore_mode: ALWAYS_OFF
|
|
|
|
spi:
|
|
clk_pin: 17
|
|
mosi_pin: 21
|
|
|
|
font:
|
|
- file: "gfonts://Roboto"
|
|
id: font_key
|
|
size: 48
|
|
|
|
display:
|
|
- platform: mipi_spi
|
|
model: ST7789V
|
|
id: display_tft
|
|
byte_order: little_endian # colours were byte-swapped (purple showed as green)
|
|
dimensions:
|
|
height: 128
|
|
width: 128
|
|
offset_height: 2
|
|
offset_width: 1
|
|
cs_pin: 15
|
|
dc_pin: 33
|
|
reset_pin: 34
|
|
update_interval: 250ms # PKP LED blink
|
|
lambda: |-
|
|
// status screen (common/status_led.yaml): the whole display in the status colour
|
|
auto &sv = id(status_frame)->current_values;
|
|
if (sv.is_on()) {
|
|
it.fill(Color(sv.get_red() * 255, sv.get_green() * 255, sv.get_blue() * 255));
|
|
return;
|
|
}
|
|
// PKP LEDs, 2x6 like the keypad: on colour; with blink, alternating with the blink colour (off if not on)
|
|
bool phase = (millis() / 500) & 1;
|
|
uint8_t lvl = 255 * id(pkp_brightness) / 0x3F;
|
|
for (int i = 0; i < 12; i++) {
|
|
auto rgb = [i](uint64_t b) { return (b >> i & 1) | (b >> (i + 12) & 1) << 1 | (b >> (i + 24) & 1) << 2; };
|
|
int on = rgb(id(pkp_led_on)), bl = rgb(id(pkp_led_blink));
|
|
int c = phase && bl ? bl : (bl && !on ? 0 : on);
|
|
int x = (i % 6) * 21 + 1, y = (i / 6) * 32;
|
|
it.filled_rectangle(x, y + 1, 19, 30, Color(c & 1 ? lvl : 0, c & 2 ? lvl : 0, c & 4 ? lvl : 0));
|
|
it.rectangle(x, y + 1, 19, 30, Color(64, 64, 64));
|
|
}
|
|
// last key
|
|
if (std::isnan(id(key_code).state))
|
|
return;
|
|
uint8_t k = id(key_code).state;
|
|
// CardKB: Shift/Sym come as plain characters, Fn and the specials as codes; unknown ones as hex
|
|
const char *s = nullptr;
|
|
char c[5];
|
|
switch (k) {
|
|
case 0x08: s = "BS"; break;
|
|
case 0x09: s = "Tab"; break;
|
|
case 0x0D: s = "Ent"; break;
|
|
case 0x1B: s = "Esc"; break;
|
|
case 0x20: s = "Spc"; break;
|
|
case 0x7F: s = "Del"; break;
|
|
case 0xB4: s = "Lt"; break;
|
|
case 0xB5: s = "Up"; break;
|
|
case 0xB6: s = "Dn"; break;
|
|
case 0xB7: s = "Rt"; break;
|
|
default:
|
|
if (k > 0x20 && k < 0x7F) {
|
|
c[0] = (char) k;
|
|
c[1] = 0;
|
|
} else {
|
|
snprintf(c, sizeof(c), "%02X", k);
|
|
}
|
|
s = c;
|
|
}
|
|
it.print(64, 96, id(font_key), TextAlign::CENTER, s);
|