--- # Hardware: M5StickC Plus + Hat CardKB + Hat Mini JoyC on the hat header, TJA1050 CAN module 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 rx_pin), TJA1050 module RX # GPIO33 Grove -> can_bus (canbus esp32_can tx_pin), TJA1050 module TX # 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: 33 rx_pin: 32 can_id: 4 bit_rate: 500kbps mode: LISTENONLY # TEMP: the node does not see the bus on RX (GPIO32), keep it off the bus 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, ®, 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