# Hardware: M5Stack CoreS3 + Stack-chan kit # Pinout: # GPIO0 -> i2s_bus (i2s_audio i2s_mclk_pin) # GPIO2 -> Camera (esp32_camera) # GPIO3 -> display_tft (display mipi_spi cs_pin) # GPIO5 -> ir_tx (remote_transmitter) # GPIO6 -> uart_servo (uart tx_pin) # GPIO7 -> uart_servo (uart rx_pin) # GPIO10 -> ir_rx (remote_receiver) # GPIO11 -> i2c_bsp (i2c scl) # GPIO12 -> i2c_bsp (i2c sda) # GPIO13 -> speaker_id (speaker i2s_audio i2s_dout_pin) # GPIO14 -> stackchan_microphone (microphone i2s_audio i2s_din_pin) # GPIO15 -> Camera (esp32_camera data_pins) # GPIO16 -> Camera (esp32_camera data_pins) # GPIO33 -> i2s_bus (i2s_audio i2s_lrclk_pin) # GPIO34 -> i2s_bus (i2s_audio i2s_bclk_pin) # GPIO35 -> display_tft (display mipi_spi dc_pin) # GPIO36 -> spi_bus (spi clk_pin) # GPIO37 -> spi_bus (spi mosi_pin) # GPIO38 -> Camera (esp32_camera href_pin) # GPIO39 -> Camera (esp32_camera data_pins) # GPIO40 -> Camera (esp32_camera data_pins) # GPIO41 -> Camera (esp32_camera data_pins) # GPIO42 -> Camera (esp32_camera data_pins) # GPIO45 -> Camera (esp32_camera pixel_clock_pin) # GPIO46 -> Camera (esp32_camera vsync_pin) # GPIO47 -> Camera (esp32_camera data_pins) # GPIO48 -> Camera (esp32_camera data_pins) # I2C: # 0x23 sensor ltr_als_ps (sensor ltr_als_ps) # 0x34 axp2101_pmu (axp2101) # 0x36 aw88298_dac (audio_dac aw88298) # 0x38 touch_lcd (touchscreen ft63x6) # 0x40 es7210_adc (audio_adc es7210) # 0x41 sensor ina226 (sensor ina226) # 0x51 rtc_time (time bm8563) # 0x50 nfc_reader (st25r_i2c) # 0x58 aw9523b_hub (aw9523b) # 0x68 touch_hub (si12t) # 0x69 imu (motion bmi270) # 0x6F m5ioe1_hub (m5ioe1) substitutions: device: stackchan name: StackChan area: DEV comment: StackChan esphome: name: mcu-${device} friendly_name: ${name} area: ${area} comment: ${comment} on_boot: # the 5 V BUS after the expander parked its pins low (HARDWARE) and before si12t/st25r probe it (DATA) - priority: 700 then: - script.execute: bus_power_on # pitch end stops into the servo's own angle limits (no EEPROM unlock: until the next power cycle) - priority: -100 then: - delay: 2s - lambda: |- auto *bus = id(y_servo).get_parent(); bus->write_word(2, SCSCL_MIN_ANGLE_LIMIT_L, 645); bus->write_word(2, SCSCL_MAX_ANGLE_LIMIT_L, 912); ESP_LOGI("pitch", "Angle limits %d..%d", bus->read_word(2, SCSCL_MIN_ANGLE_LIMIT_L), bus->read_word(2, SCSCL_MAX_ANGLE_LIMIT_L)); - priority: -100 then: - light.turn_on: id: light_bar brightness: 20% red: 0% green: 0% blue: 100% - component.update: display_tft esp32: variant: esp32s3 flash_size: 16MB flash_frequency: 40MHz # the IDF 5.5.5 bootloader reads garbage from this flash (mfr 0x46) at 80MHz toolchain: esp-idf framework: advanced: flash_chip: generic type: esp-idf sdkconfig_options: # per-task CPU time for the task_stats interval CONFIG_FREERTOS_USE_TRACE_FACILITY: y CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS: y CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID: y psram: mode: quad speed: 80MHz packages: common: !include common/common.yaml ipv6_only: !include common/ipv6_only.yaml external_components: - source: github://m5stack/esphome-yaml components: [axp2101, aw88298, aw9523b, si12t, ftservo, scs9009] refresh: 0s - source: github://randybb/esphome-components components: [audio_spectrum, m5ioe1, robot_face] refresh: 1h # st25r upstream has SPI only, st25r_i2c is still on this branch - source: github://m5stack/esphome@feat-st25r3916 components: [st25r, st25r_i2c] refresh: 1d globals: - id: show_status type: bool initial_value: 'true' - id: listening type: bool initial_value: 'false' - id: speech type: std::string - id: spin_limits # yaw servo angle limits while spinning: max << 16 | min type: int initial_value: '0' i2c: - id: i2c_bsp sda: 12 scl: 11 frequency: 100kHz axp2101: id: axp2101_pmu i2c_id: i2c_bsp aw9523b: id: aw9523b_hub i2c_id: i2c_bsp p0_drive_mode: PUSH_PULL m5ioe1: id: m5ioe1_hub i2c_id: i2c_bsp reset: true output: - platform: axp2101 id: lcd_backlight_output type: range channel: DLDO1 min_voltage: 2600 max_voltage: 3300 - platform: axp2101 channel: ALDO1 voltage: 1800 - platform: axp2101 channel: ALDO2 voltage: 3300 - platform: axp2101 channel: ALDO3 voltage: 3300 - platform: axp2101 channel: ALDO4 voltage: 3300 - platform: axp2101 channel: BLDO1 voltage: 2800 - platform: axp2101 channel: BLDO2 voltage: 1500 - platform: axp2101 channel: DCDC1 voltage: 3300 - platform: axp2101 channel: DCDC4 voltage: 1800 - platform: axp2101 channel: DCDC3 voltage: 3300 - platform: axp2101 channel: DCDC5 voltage: 3300 switch: - platform: gpio id: boost_en name: Boost entity_category: config pin: aw9523b_id: aw9523b_hub number: 15 restore_mode: ALWAYS_OFF # switched on by bus_power_on - platform: gpio id: usb_otg_en name: USB OTG entity_category: config pin: aw9523b_id: aw9523b_hub number: 5 restore_mode: RESTORE_DEFAULT_OFF - platform: gpio id: bus_out_en name: BUS Out entity_category: config pin: aw9523b_id: aw9523b_hub number: 1 restore_mode: ALWAYS_OFF # switched on by bus_power_on - platform: gpio id: light_bar_en internal: true pin: m5ioe1_id: m5ioe1_hub number: 13 mode: output: true pullup: true restore_mode: ALWAYS_ON - platform: gpio id: aw_rst internal: true pin: aw9523b_id: aw9523b_hub number: 2 restore_mode: ALWAYS_ON - platform: gpio id: cam_rst internal: true pin: aw9523b_id: aw9523b_hub number: 8 restore_mode: ALWAYS_ON - platform: gpio id: servo_power internal: true pin: m5ioe1_id: m5ioe1_hub number: 0 mode: output: true pullup: true restore_mode: ALWAYS_ON i2s_audio: - id: i2s_bus i2s_lrclk_pin: 33 i2s_bclk_pin: 34 i2s_mclk_pin: number: 0 ignore_strapping_warning: true audio_adc: - platform: es7210 id: es7210_adc i2c_id: i2c_bsp bits_per_sample: 16bit sample_rate: 16000 mic_gain: 24 audio_dac: - platform: aw88298 id: aw88298_dac i2c_id: i2c_bsp sample_rate: 48000 microphone: - platform: i2s_audio id: stackchan_microphone i2s_audio_id: i2s_bus i2s_din_pin: 14 adc_type: external sample_rate: 16000 bits_per_sample: 16bit speaker: - platform: i2s_audio id: speaker_id i2s_audio_id: i2s_bus dac_type: external i2s_dout_pin: 13 audio_dac: aw88298_dac sample_rate: 48000 buffer_duration: 100ms # pass-through that computes the spectrum shown on the display while music plays - platform: audio_spectrum id: spectrum output_speaker: speaker_id bands: 20 - platform: mixer id: mixer_speaker_id output_speaker: spectrum num_channels: 1 source_speakers: - id: announcement_spk_mixer_input - id: media_spk_mixer_input - platform: resampler id: media_spk_resampling_input output_speaker: media_spk_mixer_input - platform: resampler id: announcement_spk_resampling_input output_speaker: announcement_spk_mixer_input media_player: - platform: speaker id: media_out name: Player # AW88298 maps volume linearly to -96..0 dB, the lower half is inaudible volume_min: 0.5 volume_max: 1.0 media_pipeline: speaker: media_spk_resampling_input format: NONE announcement_pipeline: speaker: announcement_spk_resampling_input format: NONE on_announcement: - script.execute: release_i2s_bus on_play: - script.execute: release_i2s_bus on_idle: - if: condition: not: voice_assistant.is_running: then: - micro_wake_word.start: micro_wake_word: id: mww microphone: stackchan_microphone models: - model: okay_nabu on_wake_word_detected: - voice_assistant.start: wake_word: !lambda return wake_word; voice_assistant: id: va microphone: stackchan_microphone media_player: media_out micro_wake_word: mww noise_suppression_level: 2 # no extra gain: a raised noise floor keeps the HA VAD from hearing the end of speech auto_gain: 0dBFS on_client_connected: - micro_wake_word.start: - delay: 2s - lambda: id(show_status) = false; - component.update: display_tft on_client_disconnected: - micro_wake_word.stop: - lambda: id(show_status) = true; - component.update: display_tft on_listening: # someone talks: a look with one eye, then with both - lambda: |- id(listening) = true; id(face).peek(0); id(face).look(0, 0); - component.update: display_tft - light.turn_on: {id: light_bar, brightness: 60%, red: 0%, green: 60%, blue: 100%} on_stt_end: # thinking: looks up to the side - lambda: |- id(listening) = false; id(face).look(0.6f, -0.8f); - component.update: display_tft - light.turn_on: {id: light_bar, brightness: 40%, red: 80%, green: 50%, blue: 0%} on_tts_start: - lambda: |- id(face).look(0, 0); id(face).set_speaking(true); - light.turn_on: {id: light_bar, brightness: 60%, red: 0%, green: 100%, blue: 20%} on_end: - wait_until: not: media_player.is_announcing: media_out - lambda: |- id(listening) = false; id(face).set_speaking(false); id(face).set_idle(true); - component.update: display_tft - light.turn_on: {id: light_bar, brightness: 20%, red: 0%, green: 0%, blue: 100%} - wait_until: not: voice_assistant.is_running - micro_wake_word.start: on_error: - light.turn_on: {id: light_bar, brightness: 60%, red: 100%, green: 0%, blue: 0%} - script.execute: id: show_mood mood: sad seconds: 3 script: # Powers the 5 V BUS from the battery (as in m5stack/esphome-yaml stackchan base). # The BUS is a capacitive load: switched on at once it pulls a converter under its # UVP threshold and the AXP2101 powers itself off (red LED, off on battery only). - id: bus_power_on then: - lambda: |- // REG 23H bits 4:0: DCDC1-5 under-voltage power-off, POR value all set uint8_t reg; if (id(axp2101_pmu)->read_byte(0x23, ®)) id(axp2101_pmu)->write_byte(0x23, reg & ~0x1F); id(boost_en).turn_on(); delay(50); // eight pulses of growing on-time bring the cold BUS up to a few volts for (int pulse = 0; pulse < 8; ++pulse) { id(bus_out_en).turn_on(); delayMicroseconds(pulse * 16); id(bus_out_en).turn_off(); delayMicroseconds(1000); } id(bus_out_en).turn_on(); delay(100); # a mood for a while, then back to neutral (stroke: love, shake: dizzy, card: surprised) - id: show_mood mode: restart parameters: mood: string seconds: int then: - lambda: |- if (id(face).is_sleeping()) id(face).peek(0); id(face).set_mood(mood); - delay: !lambda return seconds * 1000; - lambda: id(face).set_mood(robot_face::MOOD_NEUTRAL); # spins the head around (yaw servo, address 1) in wheel mode: angle limits 0/0 turn the SCS9009 # into a motor driven by PWM. The limits are written without unlocking the EEPROM, so they are # only changed until the next power cycle, and they are put back once the spin is over. - id: spin mode: single parameters: seconds: float power: int # -1000..1000, the sign is the direction then: - lambda: |- auto *bus = id(x_servo).get_parent(); const int min = bus->read_word(1, SCSCL_MIN_ANGLE_LIMIT_L); const int max = bus->read_word(1, SCSCL_MAX_ANGLE_LIMIT_L); if (min < 0 || max <= 0) { ESP_LOGW("spin", "Angle limits not readable (%d/%d), not spinning", min, max); return; } id(spin_limits) = (max << 16) | min; ESP_LOGI("spin", "Spinning %.1f s at %d, limits %d..%d", seconds, power, min, max); bus->enable_torque(1, 1); bus->pwm_mode(1); bus->write_pwm(1, clamp(power, -1000, 1000)); id(face).set_mood(robot_face::MOOD_DIZZY); - delay: !lambda return (uint32_t) (seconds * 1000); - lambda: |- auto *bus = id(x_servo).get_parent(); bus->write_pwm(1, 0); bus->enable_torque(1, 0); // position mode again; torque is off, so the old goal position does not pull the head back bus->write_word(1, SCSCL_MIN_ANGLE_LIMIT_L, id(spin_limits) & 0xFFFF); bus->write_word(1, SCSCL_MAX_ANGLE_LIMIT_L, id(spin_limits) >> 16); ESP_LOGI("spin", "Stopped, limits back to %d..%d", id(spin_limits) & 0xFFFF, id(spin_limits) >> 16); - delay: 2s - lambda: id(face).set_mood(robot_face::MOOD_NEUTRAL); # text under the face with the mouth moving - id: say mode: restart parameters: message: string then: - lambda: |- id(speech) = message; id(face).set_speaking(true); - delay: 8s - lambda: |- id(speech).clear(); id(face).set_speaking(false); # mic and speaker share one I2S bus - the mic must be released before playback - id: release_i2s_bus mode: single then: - if: condition: microphone.is_capturing: then: - micro_wake_word.stop: # stop VA only while it is listening (e.g. continued conversation), not during its own response - if: condition: lambda: return id(listening); then: - voice_assistant.stop: - wait_until: condition: not: microphone.is_capturing: timeout: 2s interval: # CPU time of every task over the last 10 s (100 % = one core), busiest first - interval: 10s then: - lambda: |- struct Prev { std::string name; uint64_t run; }; static std::vector prev; static uint64_t prev_total = 0; std::vector tasks(uxTaskGetNumberOfTasks() + 4); configRUN_TIME_COUNTER_TYPE total; tasks.resize(uxTaskGetSystemState(tasks.data(), tasks.size(), &total)); const uint64_t span = total - prev_total; struct Row { const TaskStatus_t *t; uint64_t delta; }; std::vector rows; std::vector now; for (auto &t : tasks) { uint64_t before = 0; for (auto &p : prev) if (p.name == t.pcTaskName) before = p.run; rows.push_back({&t, t.ulRunTimeCounter - before}); now.push_back({t.pcTaskName, t.ulRunTimeCounter}); } const bool first = prev_total == 0; prev = std::move(now); prev_total = total; if (first || span == 0) return; std::sort(rows.begin(), rows.end(), [](const Row &a, const Row &b) { return a.delta > b.delta; }); for (auto &r : rows) { const float pct = 100.0f * r.delta / span; if (pct < 0.5f) break; ESP_LOGI("task_stats", "%-16s core %2d prio %2u %5.1f %%", r.t->pcTaskName, r.t->xCoreID == tskNO_AFFINITY ? -1 : (int) r.t->xCoreID, (unsigned) r.t->uxCurrentPriority, pct); } - interval: 80ms # spectrum analyzer frame rate while music plays then: - if: condition: and: - media_player.is_playing: media_out - not: voice_assistant.is_running then: - component.update: display_tft # head touch gestures: a touch episode ends after 250 ms without contact; # stroke = longer episode that reaches the pads one after another (taps do nothing). # Each ST25R scan switches its RF field on and the pads report a 50-350 ms phantom touch, at any # si12t sensitivity, so the NFC reader is scanned from here: every 2 s between episodes, with the # pads ignored for 400 ms afterwards, and at once when an episode starts (a card being laid on). - interval: 50ms then: - lambda: |- static bool active = false; static uint32_t start = 0, last_on = 0, last_scan = 0; static uint32_t first_on[3]; const uint32_t now = millis(); if (!active) { if (now - last_scan >= 2000 && id(nfc_reader).is_ready()) { last_scan = now; id(nfc_reader).update(); return; } if (now - last_scan < 400) return; } const bool on[3] = {id(head_touch_1).state != "No touch", id(head_touch_2).state != "No touch", id(head_touch_3).state != "No touch"}; if (on[0] || on[1] || on[2]) { if (!active) { active = true; start = now; if (id(nfc_reader).is_ready()) { last_scan = now; id(nfc_reader).update(); } first_on[0] = first_on[1] = first_on[2] = 0; } for (int i = 0; i < 3; i++) if (on[i] && first_on[i] == 0) first_on[i] = now; last_on = now; return; } if (!active || now - last_on < 250) return; active = false; const uint32_t duration = last_on - start; int pads = 0; uint32_t earliest = UINT32_MAX, latest = 0; for (int i = 0; i < 3; i++) { if (first_on[i] == 0) continue; pads++; earliest = std::min(earliest, first_on[i]); latest = std::max(latest, first_on[i]); } const uint32_t spread = latest - earliest; ESP_LOGD("head_touch", "duration %u ms, pads %d, spread %u ms, order 1:%d 2:%d 3:%d", (unsigned) duration, pads, (unsigned) spread, first_on[0] ? (int) (first_on[0] - earliest) : -1, first_on[1] ? (int) (first_on[1] - earliest) : -1, first_on[2] ? (int) (first_on[2] - earliest) : -1); if (pads >= 2 && spread >= 150 && duration >= 400) { id(show_mood).execute(std::string("love"), 5); } - interval: 1s # status screen refresh until Home Assistant connects then: - if: condition: lambda: return id(show_status); then: - component.update: display_tft spi: - id: spi_bus clk_pin: 36 mosi_pin: 37 image: - platform: file file: assets/images/made-for-esphome-white-on-black.svg id: esphome_logo resize: 230x68 type: RGB565 transparency: alpha_channel - platform: file file: mdi:ear-hearing id: icon_listening resize: 28x28 type: BINARY font: - file: "gfonts://Roboto" id: font_title size: 24 - file: "gfonts://Roboto" id: font_status size: 16 color: - id: status_ok red: 30% green: 85% blue: 40% - id: status_wait red: 100% green: 70% blue: 0% - id: love_red red: 100% green: 25% blue: 45% - id: status_low red: 100% green: 20% blue: 20% - id: status_dim red: 55% green: 55% blue: 55% - id: face_fg red: 100% green: 100% blue: 100% - id: face_bg red: 0% green: 0% blue: 0% display: - platform: mipi_spi id: display_tft model: M5CORE dc_pin: 35 reset_pin: aw9523b_id: aw9523b_hub number: 9 cs_pin: number: 3 ignore_strapping_warning: true data_rate: 40MHz invert_colors: true update_interval: never auto_clear_enabled: false lambda: |- static bool first_draw = true; static bool dot_shown = false; static bool bar_drawn = false; static int last_bars = -1, last_batt = -2; static bool last_charging = false; const bool wifi_ok = wifi::global_wifi_component->is_connected(); const int8_t rssi = wifi_ok ? wifi::global_wifi_component->wifi_rssi() : -127; const int wifi_bars = !wifi_ok ? 0 : rssi > -55 ? 4 : rssi > -65 ? 3 : rssi > -75 ? 2 : 1; const int batt = std::isnan(id(battery_level).state) ? -1 : (int) roundf(id(battery_level).state); const bool charging = id(battery_charging).state; // status screen until Home Assistant connects if (id(show_status)) { first_draw = true; it.fill(id(face_bg)); it.image(160, 46, id(esphome_logo), ImageAlign::CENTER); it.print(160, 95, id(font_title), id(face_fg), TextAlign::CENTER, "${name}"); if (wifi_ok) { it.printf(20, 114, id(font_status), id(status_ok), "WiFi: connected (%d dBm)", rssi); // one global IPv6 (skip link-local) and one IPv4 address int y = 133; char buf[network::IP_ADDRESS_BUFFER_SIZE]; for (bool v6 : {true, false}) { for (auto &ip : wifi::global_wifi_component->get_ip_addresses()) { if (!ip.is_set() || ip.is_ip6() != v6) continue; ip.str_to(buf); if (v6 && strncmp(buf, "fe80", 4) == 0) continue; it.print(30, y, id(font_status), id(status_dim), buf); y += 19; break; } } } else { it.print(20, 114, id(font_status), id(status_wait), "WiFi: connecting..."); } const bool api_ok = api::global_api_server->is_connected(); it.print(20, 172, id(font_status), api_ok ? id(status_ok) : id(status_wait), api_ok ? "Home Assistant: connected" : "Home Assistant: waiting..."); if (batt >= 0) { it.printf(20, 191, id(font_status), batt <= 20 && !charging ? id(status_low) : id(status_dim), "Battery: %d%%%s", batt, charging ? " (charging)" : ""); } it.printf(20, 212, id(font_status), id(status_dim), "ESPHome %s", ESPHOME_VERSION); it.printf(300, 212, id(font_status), id(status_dim), TextAlign::TOP_RIGHT, "up %us", (unsigned) (millis() / 1000)); return; } // stack-chan face (robot_face), 320x240 // the whole screen is cleared only once, then just the face areas, so that only the // changed region is sent over SPI instead of the whole 150 kB buffer const bool music = id(media_out).state == media_player::MEDIA_PLAYER_STATE_PLAYING && !id(va).is_running(); // switching between face and spectrum repaints everything static bool last_music = false; if (music != last_music) { last_music = music; first_draw = true; } static bool spectrum_reset = true; if (first_draw) { it.fill(id(face_bg)); id(face).invalidate(); first_draw = false; bar_drawn = false; spectrum_reset = true; } else if (!music) { it.filled_rectangle(0, 216, 320, 22, id(face_bg)); // speech text if (dot_shown && !id(listening)) { it.filled_rectangle(254, 2, 28, 28, id(face_bg)); } } dot_shown = id(listening); // status bar (battery top-left, wifi top-right), redrawn only when something changes if (!bar_drawn || wifi_bars != last_bars || batt != last_batt || charging != last_charging) { bar_drawn = true; last_bars = wifi_bars; last_batt = batt; last_charging = charging; it.filled_rectangle(4, 4, 90, 22, id(face_bg)); it.filled_rectangle(288, 4, 28, 22, id(face_bg)); // battery: outline, terminal nub and fill proportional to the charge if (batt >= 0) { const Color c = charging ? id(status_ok) : batt <= 20 ? id(status_low) : id(status_dim); it.rectangle(8, 8, 26, 13, c); it.filled_rectangle(34, 12, 2, 5, c); it.filled_rectangle(10, 10, 22 * batt / 100, 9, c); it.printf(40, 6, id(font_status), c, "%d%%", batt); } // wifi: 4 bars, unused ones dimmed; red when disconnected for (int i = 0; i < 4; i++) { const int h = 4 + 3 * i; const Color c = !wifi_ok ? id(status_low) : i < wifi_bars ? id(status_dim) : Color(40, 40, 40); it.filled_rectangle(290 + i * 6, 21 - h, 4, h, c); } } // Winamp style spectrum analyzer instead of the face while music plays. // Only the segments that changed since the last frame are drawn or cleared, which keeps both the // pixel work and the SPI transfer small. if (music) { static float peak[32]; static uint32_t peak_time[32]; static int prev_segs[32], prev_cap[32]; const uint32_t now = millis(); const int bands = std::min(id(spectrum).get_bands(), 32); const int x0 = 12, x1 = 308, top = 40, bottom = 228; const int slot = (x1 - x0) / bands, bar_w = slot - 3; const int seg_h = 5; // 4 px LED segment + 1 px gap const int max_segs = (bottom - top) / seg_h; if (spectrum_reset) { spectrum_reset = false; for (int b = 0; b < 32; b++) { peak[b] = 0.0f; prev_segs[b] = 0; prev_cap[b] = -1; } } auto seg_y = [&](int s) { return bottom - (s + 1) * seg_h + 1; }; for (int b = 0; b < bands; b++) { const float level = id(spectrum).get_level(b); const int x = x0 + b * slot + 1; const int segs = std::min(max_segs, (int) (level * max_segs)); // peak caps hold for a moment, then fall slowly if (level >= peak[b]) { peak[b] = level; peak_time[b] = now; } else if (now - peak_time[b] > 400) { peak[b] = std::max(0.0f, peak[b] - 0.03f); } const int cap = peak[b] > 0.0f ? std::min(max_segs - 1, std::max(segs, (int) (peak[b] * max_segs))) : -1; // the old cap is cleared first; bar segments drawn below repaint anything it overlapped if (prev_cap[b] >= 0 && prev_cap[b] != cap) it.filled_rectangle(x, seg_y(prev_cap[b]), bar_w, seg_h - 1, id(face_bg)); if (segs > prev_segs[b]) { for (int s = prev_segs[b]; s < segs; s++) { // green at the bottom, through yellow to red at the top const float f = float(s) / float(max_segs - 1); const Color c = f < 0.5f ? Color((uint8_t) (510 * f), 230, 0) : Color(255, (uint8_t) (460 * (1.0f - f)), 0); it.filled_rectangle(x, seg_y(s), bar_w, seg_h - 1, c); } } else if (segs < prev_segs[b]) { it.filled_rectangle(x, seg_y(prev_segs[b] - 1), bar_w, (prev_segs[b] - segs) * seg_h, id(face_bg)); } prev_segs[b] = segs; if (cap >= segs && cap >= 0 && (cap != prev_cap[b] || cap == segs)) { const int cy = seg_y(cap) + seg_h - 3; it.filled_rectangle(x, cy, bar_w, 2, id(status_dim)); } prev_cap[b] = cap; } return; } id(face).draw(it); if (!id(speech).empty()) { it.print(160, 218, id(font_status), id(face_fg), TextAlign::TOP_CENTER, id(speech).c_str()); } if (id(listening)) { it.image(254, 2, id(icon_listening), id(face_fg), id(face_bg)); // left of the wifi bars } robot_face: id: face display_id: display_tft foreground: face_fg background: face_bg love_color: love_red devil_color: status_low # eyes only, centered between the status bar and the speech line eyes_y: 120 eye_width: 72 eye_height: 84 eye_radius: 20 eye_spacing: 50 gaze_x: 34 gaze_y: 22 sleep_after: 5min motion: - platform: bmi270 id: imu i2c_id: i2c_bsp address: 0x69 update_interval: 50ms accelerometer_range: 4G accelerometer_odr: 100HZ event: - platform: motion id: imu_shake name: Shake threshold: 1.5 cooldown: 500ms on_event: - script.execute: id: show_mood mood: dizzy seconds: 3 st25r_i2c: id: nfc_reader i2c_id: i2c_bsp update_interval: never # polled from the head touch interval, see there on_tag: - lambda: id(speech) = x; // UID under the face while the card lies there - script.execute: id: show_mood mood: surprised seconds: 3 - text_sensor.template.publish: id: nfc_tag state: !lambda return x; on_tag_removed: - lambda: id(speech).clear(); - component.update: display_tft - text_sensor.template.publish: id: nfc_tag state: "" # an update needs the network to itself: playback, wake word and the assistant stop first ota: - id: !extend ota_esphome on_begin: - media_player.stop: media_out - voice_assistant.stop: - micro_wake_word.stop: # the StackChan base right under the Core spoils the WiFi link (ping 160-250 ms, stuttering audio, # m5stack/StackChan#119); 11 dB brings it to ~20 ms. A ~1 cm gap from the base fixes it fully. wifi: output_power: 11dB api: actions: - action: stackchan_say variables: message: string then: - script.execute: id: say message: !lambda return message; - action: stackchan_pet then: - script.execute: id: show_mood mood: love seconds: 5 # spins the head around: seconds, power -1000..1000 (sign = direction) - action: stackchan_spin variables: seconds: float power: int then: - script.execute: id: spin seconds: !lambda return seconds; power: !lambda return power; - action: stackchan_shake then: - script.execute: id: show_mood mood: dizzy seconds: 3 # neutral, happy, sad, angry, tired, surprised, love, dizzy, puppy, devil, curious, suspicious; # laugh and confused are short animations, sleep/wake close and open the eyes, wink winks - action: stackchan_set_emotion variables: emotion: string then: - lambda: |- if (emotion == "laugh") id(face).laugh(); else if (emotion == "confused") id(face).confused(); else if (emotion == "sleep") id(face).set_eyes_open(false); else if (emotion == "wake") id(face).peek(0); else if (emotion == "wink") id(face).wink(1); else { id(show_mood).stop(); id(face).set_mood(emotion); } touchscreen: - platform: ft63x6 id: touch_lcd i2c_id: i2c_bsp reset_pin: aw9523b_id: aw9523b_hub number: 0 on_touch: - voice_assistant.start: esp32_camera: id: camera_id name: Camera i2c_id: i2c_bsp setup_priority: -200 # after power rails and IO expanders pixel_format: RGB565 max_framerate: 15 fps frame_buffer_count: 1 # GC0308 has no JPEG, convert RGB565 -> JPEG for HA (as in BSP); runs in the main loop, so no periodic frames jpeg_quality: 12 idle_framerate: 0 fps vsync_pin: number: 46 ignore_strapping_warning: true href_pin: 38 external_clock: pin: 2 frequency: 20MHz pixel_clock_pin: number: 45 ignore_strapping_warning: true data_pins: [39, 40, 41, 42, 15, 16, 48, 47] resolution: 320x240 frame_buffer_location: PSRAM sensor: # raw servo positions, 1024 steps = 360 degrees (2.844 steps per degree, as in ftservo) - platform: ftservo id: servo_x_feedback ftservo_id: x_servo update_interval: 500ms position: name: Servo X Position entity_category: diagnostic filters: - delta: 2 - platform: ftservo id: servo_y_feedback ftservo_id: y_servo update_interval: 500ms position: name: Servo Y Position entity_category: diagnostic filters: - delta: 2 - platform: axp2101 id: axp2101_sensor battery_level: id: battery_level name: Battery Level filters: - delta: 1.0 on_value: - component.update: display_tft battery_charging: id: battery_charging name: Battery Charging on_state: - component.update: display_tft - platform: ltr_als_ps i2c_id: i2c_bsp address: 0x23 type: ALS_PS ambient_light: id: ambient_light name: Ambient Light ps_counts: id: proximity_counts name: Proximity Counts - platform: ina226 i2c_id: i2c_bsp address: 0x41 shunt_resistance: 0.01 ohm max_current: 3.2A current: id: ina226_current name: Current power: id: ina226_power name: Power bus_voltage: id: ina226_bus_voltage name: Bus Voltage si12t: id: touch_hub i2c_id: i2c_bsp # report low-strength touches too, not only medium/high intr_level: OUTPUT_ALL text_sensor: - platform: template id: nfc_tag name: NFC Tag icon: mdi:nfc - platform: si12t id: head_touch_1 name: Head Touch 1 channel: CH_1 # lower level = lower touch threshold: SENS_LEVEL_1 = 0.7 % capacitance change (default level 3 is 1.2 %), # SENS_LEVEL_0 made neighbouring pads trigger together sensitivity: SENS_LEVEL_1 extend_threshold: false update_interval: 100ms internal: true - platform: si12t id: head_touch_2 name: Head Touch 2 channel: CH_2 sensitivity: SENS_LEVEL_1 extend_threshold: false update_interval: 100ms internal: true - platform: si12t id: head_touch_3 name: Head Touch 3 channel: CH_3 sensitivity: SENS_LEVEL_1 extend_threshold: false update_interval: 100ms internal: true uart: - id: uart_servo tx_pin: 6 rx_pin: 7 baud_rate: 1000000 scs9009: ftservo: - platform: scs9009 id: x_servo address: 1 - platform: scs9009 id: y_servo address: 2 button: - platform: template name: Spin icon: mdi:rotate-360 on_press: - script.execute: id: spin seconds: 3 power: 600 number: - platform: ftservo ftservo_id: x_servo # yaw: 0 = straight ahead = raw 461 (measured), 2.844 steps per degree angle: id: servo_x_angle name: Servo X Angle min_value: -160 max_value: 160 use_raw_angle: false angle_offset: 162.1 step: 5 # pitch: 0 = all the way down = raw 642, the top stop is raw 928 (~100 degrees, measured) - platform: ftservo ftservo_id: y_servo angle: id: servo_y_angle name: Servo Y Angle min_value: 0 max_value: 95 use_raw_angle: false angle_offset: 225.7 step: 5 light: - platform: m5ioe1 id: light_bar name: Light Bar num_led: 12 - platform: monochromatic id: lcd_backlight name: LCD Backlight output: lcd_backlight_output restore_mode: ALWAYS_ON remote_transmitter: id: ir_tx pin: 5 carrier_duty_percent: 50% non_blocking: true remote_receiver: id: ir_rx pin: 10 time: - platform: bm8563 id: rtc_time i2c_id: i2c_bsp update_interval: never