atoms3: CANopen keypad (CardKB, PKP 2600SI emulation); m5stickc-plus: CANopen keypad + Mini JoyC
CI / YAML lint (push) Successful in 6s
CI / ESPHome config validation (push) Successful in 56s

This commit is contained in:
randy committed 2026-12-01 01:05:01 +01:00
1 parent aa3e59bb0a
commit 58a5176ed3
2 files changed
+509 -457

No files matched your search

+177 -457
View File
@@ -1,41 +1,67 @@
--- ---
# Hardware: M5Stack AtomS3 # 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: # Pinout:
# GPIO1 Grove SCL -> motor_in1 (output gpio) # GPIO1 Grove SCL -> i2c_grove (i2c scl)
# GPIO2 Grove SDA -> motor_in2 (output gpio) # GPIO2 Grove SDA -> i2c_grove (i2c sda)
# GPIO5 -> canbus/esp32_can (canbus esp32_can tx_pin) # GPIO5 -> can_bus (canbus esp32_can tx_pin)
# GPIO6 -> canbus/esp32_can (canbus esp32_can rx_pin) # GPIO6 -> can_bus (canbus esp32_can rx_pin)
# GPIO15 -> display_tft (display mipi_spi cs_pin) # GPIO15 -> display_tft (display mipi_spi cs_pin)
# GPIO16 -> gpio_tft_backlight (output ledc) # GPIO16 -> gpio_tft_backlight (output ledc)
# GPIO17 -> spi (spi clk_pin) # GPIO17 -> spi (spi clk_pin)
# GPIO21 -> spi (spi mosi_pin) # GPIO21 -> spi (spi mosi_pin)
# GPIO33 -> display_tft (display mipi_spi dc_pin) # GPIO33 -> display_tft (display mipi_spi dc_pin)
# GPIO34 -> display_tft (display mipi_spi reset_pin) # GPIO34 -> display_tft (display mipi_spi reset_pin)
# GPIO38 -> i2c_main (i2c sda)
# GPIO39 -> i2c_main (i2c scl)
# GPIO41 -> Button (binary_sensor gpio) (inverted) # GPIO41 -> Button (binary_sensor gpio) (inverted)
# I2C: # I2C:
# 0x21 pcf8574_hub (pcf8574) # 0x5F cardkb (m5stack_keyboard), CardKB
# 0x26 m5stack_relay_1 (m5stack_4relay)
# 0x3C lcd (display ssd1306_i2c)
substitutions: substitutions:
device: atoms3 device: atoms3
name: AtomS3 name: AtomS3
area: DEV area: DEV
comment: -- comment: CANopen keypad
esphome: esphome:
name: mcu-${device} name: mcu-${device}
friendly_name: ${name} friendly_name: ${name}
area: ${area} area: ${area}
comment: ${comment} comment: ${comment}
# on_boot: on_boot:
# - priority: 800 - lambda: |-
# then: id(cardkb).add_on_key_callback([](uint8_t key) {
# - lambda: |- id(key_code).publish_state(key);
# id(display_tft).enable(); float n = id(key_count).state; // NaN before the first key
# id(display_tft).transfer_byte(0x11); uint32_t count = std::isnan(n) ? 1 : ((uint32_t) n + 1) % 65535;
# id(display_tft).disable(); 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: esp32:
variant: esp32s3 variant: esp32s3
@@ -46,88 +72,13 @@ esp32:
flash_chip: gd flash_chip: gd
type: esp-idf type: esp-idf
ota:
- platform: web_server
pcf8574:
- id: 'pcf8574_hub'
address: 0x21
pcf8575: true
image:
- platform: animation
file: "assets/images/blinky.gif"
id: blinky
resize: 128x128
type: RGB565
- platform: animation
file: "assets/images/vault_boy_walking.gif"
id: vaultboy
resize: 168x121
type: RGB565
logger:
logs:
component: ERROR
web_server:
port: 80
version: 3
# light:
# - platform: esp32_rmt_led_strip
# name: LED
# id: neo
# rgb_order: GRB
# pin: 2
# num_leds: 1
# # rmt_channel: 0
# chipset: ws2812
# restore_mode: ALWAYS_OFF
text:
- platform: template
name: Test Text
# id: "it_2"
icon: "mdi:text"
optimistic: true
internal: false
initial_value: "Welcome ESPHOME - #{name}"
restore_value: true
mode: text
# max_restore_data_length: 24
# logger:
# hardware_uart: USB_SERIAL_JTAG
external_components: external_components:
- source: github://pr#7234 - source: github://mrk-its/esphome-canopen@canopen_entities
components: m5stack_4relay components: [canopen_entities]
refresh: 0s refresh: 1h
# - source: github://pr#5586 - source: github://randybb/esphome-components
# components: [ http_request ] components: [m5stack_keyboard]
# - source: github://mknjc/esphome@ds248x refresh: 1h
# components: ds248x
# - source: github://bekriebel/esphome@sh1107_fix
# components:
# - ssd1306_base
# - ssd1306_i2c
# - ssd1306_spi
# - source: github://pr#5096
# refresh: 0s
# - source: github://pr#3500
# refresh: 0s
# components:
# - web_server
# - web_server_idf
# - web_server_base
# - captive_portal
# ota:
# - platform: http_request
# web_server:
# captive_portal:
packages: packages:
common: !include common/common.yaml common: !include common/common.yaml
@@ -136,106 +87,105 @@ packages:
file: common/status_led.yaml file: common/status_led.yaml
vars: vars:
light_id: status_frame light_id: status_frame
# common: !include common/common_wo_debug.yaml
# common: !include common/common_dev.yaml i2c:
- id: i2c_grove
sda: 2
scl: 1
m5stack_keyboard:
- id: cardkb
i2c_id: i2c_grove
address: 0x5F
canbus: canbus:
- platform: esp32_can - platform: esp32_can
id: can_bus
rx_pin: 6 rx_pin: 6
tx_pin: 5 tx_pin: 5
can_id: 4 can_id: 3
# bit_rate: 500kbps bit_rate: 500kbps
bit_rate: 1000KBPS
use_extended_id: true
on_frame:
- can_id: 0x123
use_extended_id: true
then:
- lambda: |-
std::string b(x.begin(), x.end());
ESP_LOGD("can extended id 0x123", "%s", &b[0] );
- can_id: 0x123
then:
- lambda: |-
std::string b(x.begin(), x.end());
ESP_LOGD("can standard id 0x123", "%s", &b[0] );
- can_id: 0x3f1
can_id_mask: 0x7ff
use_extended_id: false
then:
- lambda: |-
std::string b(x.begin(), x.end());
ESP_LOGD("can id 0x03f1", "%s", &b[0] );
- can_id: 0x108
use_extended_id: false
then:
- lambda: |-
std::string b(x.begin(), x.end());
ESP_LOGD("can id 0x108", "%s", &b[0] );
- can_id: 0x109
use_extended_id: false
then:
- lambda: |-
std::string b(x.begin(), x.end());
ESP_LOGD("can id 0x109", "%s", &b[0] );
i2c: canopen:
# - id: i2c_groove - id: can_open
# sda: 2 canbus_id: can_bus
# scl: 1 node_id: 3
# scan: true
- id: i2c_main
sda: 38
scl: 39
scan: true
frequency: 800kHz
# mcp23017: # Blink Marine PKP 2600SI emulation (docs: home-gitops canopen-nodes.adoc): like the real one, not active until NMT Start
# - id: mcp23017_hub - id: pkp
# i2c_id: i2c_main canbus_id: can_bus
# address: 0x20 node_id: 0x15
nmt_startup: pre_operational
device_name: PKP2600SI
# ds248x: globals:
# i2c_id: i2c_main - id: pkp_keys
# address: 0x18 # should be the default type: uint16_t
# # sleep_pin: 32 # remove if not needed, if set the ds248x sleeps when no transaction is in progress initial_value: '0'
# active_pullup: true # adds a active pullup which should improve signal integrity - id: pkp_led_on
# strong_pullup: false # enables a strong pullup used to provide the needed current for temperature reading without vcc connected type: uint64_t
# bus_sleep: true # power down the bus when no transaction is in progres initial_value: '0'
- id: pkp_led_blink
type: uint64_t
initial_value: '0'
- id: pkp_brightness
type: uint8_t
initial_value: '0x3F'
# one_wire: script:
# - platform: gpio # keys state TPDO 0x195: bytes 0-1 keys 1-12, byte 4 tick (+1 per 100 ms); pressed now, released 200 ms later
# pin: 1 - id: pkp_send_keys
# id: hub_1 mode: restart
# - platform: gpio then:
# pin: 2 - lambda: |-
# id: hub_2 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));
m5stack_4relay: # CardKB sends only the press (Shift/Sym/Fn resolved in the CardKB): a consumer reacts to a change of key_count
id: m5stack_relay_1 # and reads the key from key_code (both in the same frame), or watches key_event alone (count % 255 << 8 | key)
i2c_id: i2c_main 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}
# mqtt: sensor:
# id: mqtt_client - platform: template
# broker: !secret mqtt_ip id: key_code
# # broker: 10.10.10.10 name: Key Code
# username: !secret mqtt_username accuracy_decimals: 0
# password: !secret mqtt_password update_interval: never
# discovery: true - 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
# mdns: binary_sensor:
# disabled: false - platform: gpio
id: side_button
# wifi: name: Button
# use_address: 10.17.240.52 pin:
number: 41
inverted: true
output: output:
- platform: gpio
id: motor_in1
pin: 1
- platform: gpio
id: motor_in2
pin: 2
- platform: ledc - platform: ledc
pin: 16 pin: 16
id: gpio_tft_backlight id: gpio_tft_backlight
@@ -266,155 +216,14 @@ light:
blue: status_frame_b blue: status_frame_b
restore_mode: ALWAYS_OFF restore_mode: ALWAYS_OFF
switch:
- platform: template
name: "Motor Forward"
optimistic: true
turn_on_action:
- output.turn_on: motor_in1
- output.turn_off: motor_in2
turn_off_action:
- output.turn_off: motor_in1
- output.turn_off: motor_in2
- platform: template
name: "Motor Back"
optimistic: true
turn_on_action:
- output.turn_off: motor_in1
- output.turn_on: motor_in2
turn_off_action:
- output.turn_off: motor_in1
- output.turn_off: motor_in2
# switch:
- platform: m5stack_4relay
name: Relay 1
id: relay_1
channel: 1
# interlock: &interlock_group_1 [relay_1, relay_2]
# interlock_wait_time: 1s
m5stack_4relay_id: m5stack_relay_1
- platform: m5stack_4relay
name: Relay 2
id: relay_2
channel: 2
# interlock: *interlock_group_1
# interlock_wait_time: 1s
m5stack_4relay_id: m5stack_relay_1
- platform: m5stack_4relay
name: Relay 3
id: relay_3
channel: 3
# interlock: &interlock_group_2 [relay_3, relay_4]
m5stack_4relay_id: m5stack_relay_1
- platform: m5stack_4relay
name: Relay 4
id: relay_4
channel: 4
# interlock: *interlock_group_2
m5stack_4relay_id: m5stack_relay_1
- platform: gpio
name: "PCF8574 Pin #8"
pin:
pcf8574: pcf8574_hub
number: 9
# One of INPUT or OUTPUT
mode:
output: true
inverted: false
spi: spi:
clk_pin: 17 clk_pin: 17
mosi_pin: 21 mosi_pin: 21
binary_sensor:
- platform: gpio
name: Button
pin:
number: 41
inverted: true
- platform: status
id: system_status
# - platform: gpio
# id: test_sensor_1
# pin:
# mcp23xxx: mcp23017_hub
# number: 4
# mode:
# input: true
# pullup: true
# inverted: false
# filters:
# - delayed_on_off: 50ms #Debounce
# - platform: gpio
# id: test_sensor_2
# pin:
# mcp23xxx: mcp23017_hub
# number: 5
# mode:
# input: true
# pullup: true
# inverted: false
# filters:
# - delayed_on_off: 50ms #Debounce
# - platform: gpio
# id: test_sensor_3
# pin:
# mcp23xxx: mcp23017_hub
# number: 6
# mode:
# input: true
# pullup: true
# inverted: false
# filters:
# - delayed_on_off: 50ms #Debounce
# - platform: gpio
# id: test_sensor_4
# pin:
# mcp23xxx: mcp23017_hub
# number: 7
# mode:
# input: true
# pullup: true
# inverted: false
# filters:
# - delayed_on_off: 50ms #Debounce
color:
- id: my_red
red: 100%
green: 0%
blue: 0%
- id: my_yellow
red: 100%
green: 100%
blue: 0%
- id: my_green
red: 0%
green: 100%
blue: 0%
- id: my_blue
red: 0%
green: 0%
blue: 100%
- id: my_gray
red: 50%
green: 50%
blue: 50%
font: font:
- file: "gfonts://Roboto" - file: "gfonts://Roboto"
id: font_default id: font_key
size: 20 size: 48
- file: "gfonts://Roboto"
id: roboto_32
size: 32
- file: "gfonts://Roboto"
id: roboto_24
size: 24
- file: "gfonts://Roboto"
id: roboto_12
size: 12
display: display:
- platform: mipi_spi - platform: mipi_spi
@@ -426,12 +235,10 @@ display:
width: 128 width: 128
offset_height: 2 offset_height: 2
offset_width: 1 offset_width: 1
# backlight_pin: 16
cs_pin: 15 cs_pin: 15
dc_pin: 33 dc_pin: 33
reset_pin: 34 reset_pin: 34
# eightbitcolor: true update_interval: 250ms # PKP LED blink
update_interval: 1s
lambda: |- lambda: |-
// status screen (common/status_led.yaml): the whole display in the status colour // status screen (common/status_led.yaml): the whole display in the status colour
auto &sv = id(status_frame)->current_values; auto &sv = id(status_frame)->current_values;
@@ -439,129 +246,42 @@ display:
it.fill(Color(sv.get_red() * 255, sv.get_green() * 255, sv.get_blue() * 255)); it.fill(Color(sv.get_red() * 255, sv.get_green() * 255, sv.get_blue() * 255));
return; return;
} }
//Ingress shown animation Frame. // PKP LEDs, 2x6 like the keypad: on colour; with blink, alternating with the blink colour (off if not on)
id(blinky).next_frame(); bool phase = (millis() / 500) & 1;
// Draw the animation my_animation at position [x=0,y=0] uint8_t lvl = 255 * id(pkp_brightness) / 0x3F;
it.image(0, 0, id(blinky), COLOR_ON, COLOR_OFF); 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));
// // it.print(0, 0, id(font_default), "Hello World!"); int c = phase && bl ? bl : (bl && !on ? 0 : on);
// // it.strftime(0, 0, id(font_default), TextAlign::TOP_LEFT, "%H:%M", id(ha_time).now()); int x = (i % 6) * 21 + 1, y = (i / 6) * 32;
// it.rectangle(0, 0, it.get_width(), it.get_height(), id(my_blue)); it.filled_rectangle(x, y + 1, 19, 30, Color(c & 1 ? lvl : 0, c & 2 ? lvl : 0, c & 4 ? lvl : 0));
// it.rectangle(0, 20, it.get_width(), it.get_height(), id(my_blue)); // header bar it.rectangle(x, y + 1, 19, 30, Color(64, 64, 64));
}
// it.strftime((128 / 2), (128 / 3) * 1 + 5, id(roboto_24), id(my_gray), TextAlign::CENTER, "%Y-%m-%d", id(ha_time).now()); // last key
// it.strftime((128 / 2), (128 / 3) * 2 + 5, id(roboto_32), id(my_gray), TextAlign::CENTER, "%H:%M:%S", id(ha_time).now()); if (std::isnan(id(key_code).state))
// it.print(5, 5, id(roboto_12), id(my_yellow), TextAlign::TOP_LEFT, "ESPHome"); return;
uint8_t k = id(key_code).state;
// // Comment out the above lines to see the image without text overlaid // CardKB: Shift/Sym come as plain characters, Fn and the specials as codes; unknown ones as hex
// // it.image(0, 0, id(my_image)); const char *s = nullptr;
char c[5];
// if (id(system_status).state) { switch (k) {
// it.print(115, 5, id(roboto_12), id(my_green), TextAlign::TOP_RIGHT, "Online"); case 0x08: s = "BS"; break;
// } case 0x09: s = "Tab"; break;
// else { case 0x0D: s = "Ent"; break;
// it.print(115, 5, id(roboto_12), id(my_red), TextAlign::TOP_RIGHT, "Offline"); case 0x1B: s = "Esc"; break;
// } case 0x20: s = "Spc"; break;
case 0x7F: s = "Del"; break;
- platform: ssd1306_i2c case 0xB4: s = "Lt"; break;
model: "SH1107 128x64" case 0xB5: s = "Up"; break;
# model: "SSD1306 128x64" case 0xB6: s = "Dn"; break;
id: "lcd" case 0xB7: s = "Rt"; break;
address: 0x3C default:
i2c_id: i2c_main if (k > 0x20 && k < 0x7F) {
rotation: 90 c[0] = (char) k;
flip_x: false c[1] = 0;
flip_y: false } else {
offset_y: 0 snprintf(c, sizeof(c), "%02X", k);
offset_x: 0 }
# lambda: |- s = c;
# it.line(0, 0, 128, 64); }
# it.rectangle(5, 5, 10, 10); it.print(64, 96, id(font_key), TextAlign::CENTER, s);
# it.circle(118, 54, 5);
# it.rectangle(0, 0, 128, 64);
sensor:
# - platform: sht3xd
# i2c_id: i2c_main
# address: 0x44
# temperature:
# name: TEST Temperature
# humidity:
# name: TEST Humidity
# update_interval: 10s
# - platform: mpu6886
# address: 0x68
# i2c_id: i2c_main
# # accel_x:
# # name: "${name} MPU6886 Accel X"
# # accel_y:
# # name: "${name} MPU6886 Accel Y"
# # accel_z:
# # name: "${name} MPU6886 Accel z"
# # gyro_x:
# # name: "${name} MPU6886 Gyro X"
# # gyro_y:
# # name: "${name} MPU6886 Gyro Y"
# # gyro_z:
# # name: "${name} MPU6886 Gyro z"
# temperature:
# name: Temperature
# id: blabla
- platform: homeassistant
id: test_humidity_ha
entity_id: sensor.humidity_158d0001c2a1d1
# - platform: ds248x
# address: 0x8b0516b39804ff28
# name: "${name} Dallas Temp 1"
# resolution: 12
# - platform: ds248x
# address: 0xa50516b3d32aff28
# name: "${name} Dallas Temp 2"
# resolution: 12
# - platform: ds248x
# address: 0x8f0516b39d87ff28
# name: "${name} Dallas Temp 3"
# resolution: 12
# - platform: ds248x
# address: 0x810516b3c69fff28
# name: "${name} Dallas Temp 4"
# resolution: 12
# - platform: dallas_temp
# address: 0xdb041750eae2ff28
# name: Dallas Temp 1
# one_wire_id: hub_1
# update_interval: 10s
# - platform: dallas_temp
# address: 0xb2041750cf1aff28
# name: Dallas Temp 2
# one_wire_id: hub_1
# update_interval: 30s
# - platform: dallas_temp
# address: 0xcd01191eb8b07128
# name: Dallas Temp 3
# one_wire_id: hub_1
# update_interval: 45s
# - platform: dallas_temp
# address: 0x5a01191ef3375b28
# name: Dallas Temp 4
# one_wire_id: hub_1
# time:
# - platform: sntp
# id: sntp_time
# timezone: Europe/Bratislava
# servers:
# - 0.pool.ntp.org
# - 1.pool.ntp.org
# - 2.pool.ntp.org
# esp32_ble_tracker:
# scan_parameters:
# active: true
# bluetooth_proxy:
# active: true
# esp32_improv:
# authorizer: none
+332
View File
@@ -0,0 +1,332 @@
---
# Hardware: M5StickC Plus + Hat CardKB + Hat Mini JoyC on the hat header, Unit CAN on Grove
# CANopen keypad + joystick (node 4), canopen from ESPHome dev + canopen_entities
# Pinout:
# GPIO0 hat SDA -> i2c_hat (i2c sda)
# GPIO26 hat SCL -> i2c_hat (i2c scl)
# GPIO32 Grove -> can_bus (canbus esp32_can tx_pin), swap with rx if the node stays silent
# GPIO33 Grove -> can_bus (canbus esp32_can rx_pin)
# GPIO21 internal SDA -> i2c_axp (i2c sda)
# GPIO22 internal SCL -> i2c_axp (i2c scl)
# GPIO10 red LED (active low) -> LED (light binary)
# GPIO37 button A -> Button A (binary_sensor gpio) (inverted)
# GPIO39 button B -> Button B (binary_sensor gpio) (inverted)
# I2C:
# 0x34 pmu_axp (axp192)
# 0x51 rtc_time (time bm8563)
# 0x68 imu (sensor mpu6886)
# 0x54 joyc (m5stack_mini_joyc), Hat Mini JoyC
# 0x5F cardkb (m5stack_keyboard), Hat CardKB
# Hat CardKB: its SDA/SCL pull-ups are on 5 V (measured, the ATmega itself takes 3V3 from the hat header, bridged
# to 5 V), so G0/G26 sit at 5 V, above the ESP32's VDD + 0.3 V; works, a level shifter or removing those pull-ups
# would fix it. Mini JoyC (STM32F030 PA9/PA10) is 5 V tolerant.
# GPIO13/15 LCD CLK/MOSI, GPIO5 CS, GPIO23 DC, GPIO18 RST -> display_tft (ST7789V2 135x240)
# AXP192: LDO2 LCD backlight, LDO3 LCD logic, EXTEN 5 V for the hat header and Grove
substitutions:
device: m5stickc-plus
name: M5StickC Plus
area: DEV
comment: CANopen keypad + joystick
esphome:
name: mcu-${device}
friendly_name: ${name}
area: ${area}
comment: ${comment}
on_boot:
# AXP192 EXTEN (reg 0x10 bit 2) = 5 V for the hat header and Grove (CardKB, CAN unit)
- priority: 800
then:
- lambda: id(set_exten)->execute(true);
- then:
- output.turn_on: lcd_logic
- 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)
id(display_tft).update();
});
esp32:
board: m5stick-c
toolchain: esp-idf
framework:
type: esp-idf
external_components:
- source: github://m5stack/esphome-yaml
components: [axp192]
refresh: 1h
- source: github://mrk-its/esphome-canopen@canopen_entities
components: [canopen_entities]
refresh: 1h
- source: github://randybb/esphome-components
components: [m5stack_keyboard, m5stack_mini_joyc]
refresh: 1h
packages:
common: !include common/common.yaml
ipv6_only: !include common/ipv6_only.yaml
i2c:
- id: i2c_axp
sda: 21
scl: 22
- id: i2c_hat
sda: 0
scl: 26
axp192:
id: pmu_axp
i2c_id: i2c_axp
m5stack_keyboard:
- id: cardkb
i2c_id: i2c_hat
address: 0x5F
m5stack_mini_joyc:
- id: joyc
i2c_id: i2c_hat
# hat mounted turned 90° right of the display: X = hat Y, Y = -hat X
x:
id: joy_y
name: Joystick Y
filters:
- multiply: -1
- delta: 8
y:
id: joy_x
name: Joystick X
filters:
- delta: 8
button:
id: joy_button
name: Joystick Button
canbus:
- platform: esp32_can
id: can_bus
tx_pin: 32
rx_pin: 33
can_id: 4
bit_rate: 500kbps
canopen:
- id: can_open
canbus_id: can_bus
node_id: 4
# CardKB sends only the press: a consumer reacts to a change of key_count and reads the key from key_code (same
# frame), or watches key_event alone (count % 255 << 8 | key). Joystick -512..511 as UINT16 = value + 512.
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: key_event, index: 3, tpdo: 0, size: 2}
- {id: joy_button, index: 4, tpdo: 0}
- {id: button_a, index: 5, tpdo: 0}
- {id: button_b, index: 6, tpdo: 0}
- {id: joy_x, index: 7, tpdo: 1, size: 2, min_value: -512, max_value: 65022}
- {id: joy_y, index: 8, tpdo: 1, size: 2, min_value: -512, max_value: 65022}
- {id: led, index: 9, tpdo: 1}
- {id: joy_led, index: 10, tpdo: 1, caps: [on_off, brightness]}
time:
- platform: bm8563
id: rtc_time
i2c_id: i2c_axp
# keep the RTC in sync with Home Assistant, for the time without Wi-Fi
- id: !extend ha_time
on_time_sync:
- bm8563.write_time: rtc_time
sensor:
- platform: axp192
id: pmu_sensor
update_interval: 10s
battery_level:
id: battery_level
name: Battery Level
battery_voltage:
name: Battery Voltage
battery_charging:
id: battery_charging
name: Battery Charging
internal_temperature:
name: PMU Temperature
- platform: mpu6886
id: imu
i2c_id: i2c_axp
address: 0x68
update_interval: 1s
accel_x:
name: Accel X
accel_y:
name: Accel Y
accel_z:
name: Accel Z
gyro_x:
name: Gyro X
gyro_y:
name: Gyro Y
gyro_z:
name: Gyro Z
temperature:
name: IMU Temperature
- 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: button_a
name: Button A
pin:
number: 37
inverted: true
- platform: gpio
id: button_b
name: Button B
pin:
number: 39
inverted: true
output:
- platform: axp192
type: range
channel: DLDO2
id: lcd_backlight_out
min_voltage: 2500
max_voltage: 3300
- platform: axp192
channel: DLDO3
id: lcd_logic
voltage: 3000
- platform: gpio
id: led_out
pin:
number: 10
inverted: true
light:
- platform: monochromatic
id: lcd_backlight
name: LCD Backlight
entity_category: config
output: lcd_backlight_out
restore_mode: ALWAYS_ON
- platform: binary
id: led
name: LED
output: led_out
- platform: m5stack_mini_joyc
id: joy_led
name: Joystick LED
mini_joyc_id: joyc
script:
# AXP192 EXTEN (reg 0x10 bit 2): 5 V out on the hat header and Grove
- id: set_exten
parameters:
enable: bool
then:
- lambda: |-
uint8_t reg = 0x10, v;
if (id(i2c_axp).write_readv(0x34, &reg, 1, &v, 1) != i2c::ERROR_OK)
return;
uint8_t d[2] = {reg, (uint8_t) (enable ? v | 0x04 : v & ~0x04)};
id(i2c_axp).write(0x34, d, 2);
button:
# restarts what runs from the 5 V out (CardKB bridged to 5 V, the CAN unit); the Mini JoyC runs from 3V3
- platform: template
name: Peripheral Power Cycle
entity_category: config
on_press:
- script.execute: {id: set_exten, enable: false}
- delay: 500ms
- script.execute: {id: set_exten, enable: true}
spi:
clk_pin: 13
mosi_pin: 15
font:
- file: "gfonts://Roboto"
id: font_key
size: 48
- file: "gfonts://Roboto"
id: font_status
size: 14
display:
- platform: mipi_spi
model: ST7789V
id: display_tft
dimensions:
width: 135
height: 240
offset_width: 52
offset_height: 40
invert_colors: true
rotation: 270 # landscape, turned left
cs_pin: 5
dc_pin: 23
reset_pin: 18
update_interval: 200ms
lambda: |-
// status line top left: battery (+ = charging), Wi-Fi RSSI, API (green = a client is connected)
it.printf(0, 0, id(font_status), "%.0f%%%s", id(battery_level).state, id(battery_charging).state ? "+" : "");
it.printf(40, 0, id(font_status), "%.0f", id(wifi_signal_sensor).state);
it.print(75, 0, id(font_status), api::global_api_server->is_connected() ? Color(0, 255, 0) : Color(255, 0, 0), "API");
// landscape 240x135: joystick dot in a 135x135 square on the right
it.rectangle(105, 0, 135, 135, Color(64, 64, 64));
if (!std::isnan(id(joy_x).state) && !std::isnan(id(joy_y).state)) {
int x = 172 + id(joy_x).state * 60 / 512, y = 67 - id(joy_y).state * 60 / 512;
it.filled_circle(x, y, 6, id(joy_button).state ? Color(255, 0, 0) : Color(255, 255, 255));
}
// last key (CardKB: specials as names, unknown codes as hex)
if (std::isnan(id(key_code).state))
return;
uint8_t k = id(key_code).state;
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(52, 67, id(font_key), TextAlign::CENTER, s); // last key on the left