--- # Hardware: Seeed XIAO STM32C5 #1, CANopen node 0x20 (see common/xiao_stm32c5_canopen.yaml); its LED is light # entity 1 (0x2011:1 / 0x2012:1), the server for mcu-stm32-xiao-c5-sdo-client.yaml (esphome#22410 test) # USB serial: 3838333031365105001F005D (TinyUF2: 5D001F000551363130333838) # Keypad prototype (Blink Marine PKP compatible) until the MCP23017 board exists, M5 units on Grove: # D4/D5 (i2c1, PB7 SDA, PB6 SCL) -> i2c_grove; two i2c buses: esphomeplatformzephyr/esphome#247, merged in framework-zephyr # I2C: # 0x41 enc (m5stack_8encoder), Unit 8Encoder # 0x47 byte_button (m5stack_byte), Unit ByteButton -> PKP 2400SI (2x4) emulation, node 0x14 substitutions: node_id: "0x20" esphome: name: mcu-stm32-xiao-c5 friendly_name: XIAO STM32C5 on_boot: - lambda: |- // PKP RPDOs: 0x200 LED on, 0x300 LED blink (bytes 0-2: R, G, B masks of keys 1-8), 0x400 brightness 0-3F static canopen::FrameListener pkp_rx{0x014, 0x0FF, [](void *, uint32_t can_id, const uint8_t *d, uint8_t len) { if (id(pkp).get_nmt_state() != canopen::NMTState::NMT_STATE_OPERATIONAL || len == 0) return; uint32_t bits = d[0] | (len > 1 ? d[1] << 8 : 0) | (len > 2 ? d[2] << 16 : 0); if (can_id == 0x214) id(pkp_led_on) = bits; else if (can_id == 0x314) id(pkp_led_blink) = bits; else if (can_id == 0x414) id(pkp_brightness) = d[0] > 0x3F ? 0x3F : d[0]; }, nullptr}; id(pkp).add_frame_listener(pkp_rx); packages: node: !include common/xiao_stm32c5_canopen.yaml external_components: - source: github://randybb/esphome-components components: [m5stack_byte, m5stack_8encoder] refresh: 1h i2c: - id: i2c_grove dts_node_override: i2c1 # Blink Marine PKP 2400SI emulation (docs: home-gitops canopen-nodes.adoc): like the real one, not active until # NMT Start (000#0114); 0x14, because the AtomS3 emulates a PKP 2600SI at 0x15 canopen: - id: pkp canbus_id: can_bus node_id: 0x14 nmt_startup: pre_operational device_name: PKP2400SI m5stack_byte: - id: byte_button i2c_id: i2c_grove address: 0x47 # encoders: position 0..8 (min/max here) or free turning (bit set in Encoder Relative Modes, for the map and menus); # both go to CAN as one byte: the position, or the counter mod 256 (the consumer takes the difference) m5stack_8encoder: - id: enc i2c_id: i2c_grove encoders: - id: encoder_1 min_value: 0 max_value: 8 filters: &enc_filters - lambda: return (int32_t) x & 0xFF; - id: encoder_2 min_value: 0 max_value: 8 filters: *enc_filters - id: encoder_3 min_value: 0 max_value: 8 filters: *enc_filters - id: encoder_4 min_value: 0 max_value: 8 filters: *enc_filters - id: encoder_5 min_value: 0 max_value: 8 filters: *enc_filters - id: encoder_6 min_value: 0 max_value: 8 filters: *enc_filters - id: encoder_7 min_value: 0 max_value: 8 filters: *enc_filters - id: encoder_8 min_value: 0 max_value: 8 filters: *enc_filters buttons: - id: encoder_button_1 - id: encoder_button_2 - id: encoder_button_3 - id: encoder_button_4 - id: encoder_button_5 - id: encoder_button_6 - id: encoder_button_7 - id: encoder_button_8 switch: id: encoder_switch number: # 8Encoder LED colours 0xRRGGBB (1-8 at the encoders, 9 at the switch), set over CAN (SDO), kept over reboots - &led_color platform: template id: led_color_1 name: LED 1 Color entity_category: config optimistic: true restore_value: true initial_value: 0 min_value: 0 max_value: 0xFFFFFF step: 1 - {<<: *led_color, id: led_color_2, name: LED 2 Color} - {<<: *led_color, id: led_color_3, name: LED 3 Color} - {<<: *led_color, id: led_color_4, name: LED 4 Color} - {<<: *led_color, id: led_color_5, name: LED 5 Color} - {<<: *led_color, id: led_color_6, name: LED 6 Color} - {<<: *led_color, id: led_color_7, name: LED 7 Color} - {<<: *led_color, id: led_color_8, name: LED 8 Color} - {<<: *led_color, id: led_color_9, name: LED 9 Color} # bit i = encoder i turns freely (navigation), else a position 0..8; set over CAN by the consumer (SDO) - platform: template id: encoder_relative_modes name: Encoder Relative Modes entity_category: config optimistic: true restore_value: true initial_value: 0 min_value: 0 max_value: 255 step: 1 on_value: - lambda: |- for (int i = 0; i < 8; i++) { if ((int) x >> i & 1) id(enc).set_range(i, INT32_MIN, INT32_MAX); else id(enc).set_range(i, 0, 8); } globals: - id: pkp_keys_sent type: uint8_t initial_value: '0' - id: pkp_led_on type: uint32_t initial_value: '0' - id: pkp_led_blink type: uint32_t initial_value: '0' - id: pkp_brightness type: uint8_t initial_value: '0x3F' interval: # keys TPDO 0x194 (byte 0: keys 1-8) on every change, as the real one (event-driven) # TODO on arrival: ByteButton bit polarity is not documented; here a pressed key reads 0 (drop the ~ if not) - interval: 20ms then: - lambda: |- uint8_t keys = ~id(byte_button).get_state(); if (keys == id(pkp_keys_sent) || id(pkp).get_nmt_state() != canopen::NMTState::NMT_STATE_OPERATIONAL) return; if (id(pkp).send_frame(0x194, &keys, 1)) id(pkp_keys_sent) = keys; # LEDs: the on colour; with blink, alternating with the blink colour every 500 ms (off if not on) - interval: 500ms then: - lambda: |- static bool phase = false; phase = !phase; uint8_t lvl = 255 * id(pkp_brightness) / 0x3F; uint8_t rgb[8 * 3]; for (int i = 0; i < 8; i++) { auto col = [i](uint32_t b) { return (b >> i & 1) | (b >> (i + 8) & 1) << 1 | (b >> (i + 16) & 1) << 2; }; int on = col(id(pkp_led_on)), bl = col(id(pkp_led_blink)); int c = phase && bl ? bl : (bl && !on ? 0 : on); rgb[i * 3] = c & 1 ? lvl : 0; rgb[i * 3 + 1] = c & 2 ? lvl : 0; rgb[i * 3 + 2] = c & 4 ? lvl : 0; } id(byte_button).set_leds(rgb, 8); # 8Encoder LEDs: the colours from led_color_1..9 (set remotely); turning an encoder shows its position 0..8 on the # 8 encoder LEDs for ENC_SHOW_MS (bar on dark, none in free turning), its own LED blinks white, then the colours # come back; the switch LED (9th) is green while the switch is on. Written only when something changed. - interval: 50ms then: - lambda: |- static const uint32_t ENC_SHOW_MS = 3000; static const uint8_t BAR[3] = {0, 64, 255}, ACTIVE[3] = {255, 255, 255}, SWITCH_ON[3] = {0, 255, 0}; sensor::Sensor *encs[8] = {id(encoder_1), id(encoder_2), id(encoder_3), id(encoder_4), id(encoder_5), id(encoder_6), id(encoder_7), id(encoder_8)}; number::Number *colors[9] = {id(led_color_1), id(led_color_2), id(led_color_3), id(led_color_4), id(led_color_5), id(led_color_6), id(led_color_7), id(led_color_8), id(led_color_9)}; static float last[8] = {NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN}; static int active = -1; static uint32_t changed_at = 0; for (int i = 0; i < 8; i++) { float v = encs[i]->state; if (std::isnan(v) || v == last[i]) continue; if (!std::isnan(last[i])) { // not the first read after boot active = i; changed_at = millis(); } last[i] = v; } uint8_t rgb[9 * 3]; for (int i = 0; i < 9; i++) { uint32_t c = std::isnan(colors[i]->state) ? 0 : (uint32_t) colors[i]->state; rgb[i * 3] = c >> 16; rgb[i * 3 + 1] = c >> 8; rgb[i * 3 + 2] = c; } if (active >= 0 && millis() - changed_at < ENC_SHOW_MS) { // free turning: only its own LED int pos = (int) id(encoder_relative_modes).state >> active & 1 ? 0 : encs[active]->state; memset(rgb, 0, 8 * 3); for (int led = 0; led < 8 && led < pos; led++) memcpy(&rgb[led * 3], BAR, 3); if (millis() / 250 & 1) memcpy(&rgb[active * 3], ACTIVE, 3); } if (id(encoder_switch).state) memcpy(&rgb[8 * 3], SWITCH_ON, 3); static uint8_t sent[9 * 3]; static bool written = false; if (written && memcmp(rgb, sent, sizeof(rgb)) == 0) return; // no I2C traffic if (id(enc).set_leds(rgb, 9)) { memcpy(sent, rgb, sizeof(rgb)); written = true; } # 8Encoder as entities of node 0x20 (the package uses 1-5, 16, 17); LED colours 50-58: command 0x2322:1 + 0x10 per LED (REAL32) # encoders: one byte each, the position 0..8 (as the PKP3500-MT with its default TOP position 8) or the counter mod 256 canopen_entities: entities: - {id: encoder_switch, index: 6, tpdo: 1} - {id: encoder_relative_modes, index: 7} - {id: led_color_1, index: 50} - {id: led_color_2, index: 51} - {id: led_color_3, index: 52} - {id: led_color_4, index: 53} - {id: led_color_5, index: 54} - {id: led_color_6, index: 55} - {id: led_color_7, index: 56} - {id: led_color_8, index: 57} - {id: led_color_9, index: 58} - {id: encoder_button_1, index: 30, tpdo: 3} - {id: encoder_button_2, index: 31, tpdo: 3} - {id: encoder_button_3, index: 32, tpdo: 3} - {id: encoder_button_4, index: 33, tpdo: 3} - {id: encoder_button_5, index: 34, tpdo: 3} - {id: encoder_button_6, index: 35, tpdo: 3} - {id: encoder_button_7, index: 36, tpdo: 3} - {id: encoder_button_8, index: 37, tpdo: 3} - {id: encoder_1, index: 40, tpdo: 4, size: 1} - {id: encoder_2, index: 41, tpdo: 4, size: 1} - {id: encoder_3, index: 42, tpdo: 4, size: 1} - {id: encoder_4, index: 43, tpdo: 4, size: 1} - {id: encoder_5, index: 44, tpdo: 4, size: 1} - {id: encoder_6, index: 45, tpdo: 4, size: 1} - {id: encoder_7, index: 46, tpdo: 4, size: 1} - {id: encoder_8, index: 47, tpdo: 4, size: 1}