airy10 / esphome-m5stickC

esphome components for the M5StickC
81 stars 75 forks source link

Break up AXP into esphome components (WIP) #11

Open geiseri opened 3 years ago

geiseri commented 3 years ago

First I apologize for the tons of changes here, and will break it up if you need me to. I just went buckwild last night and wanted a bit of feedback before it got much further out of control:

  1. I updated the APX192 code from upstream to get a few fixes. I am still struggling with a better way to keep this in sync.
  2. I broke out the backlight into a light component, and the battery level into a sensor component. This indirectly somewhat addresses #7. I think I can put the LDO voltage get/set in there to get the full power off. I did leave the brightness on the main interface so current configurations would not break.
  3. I made binary sensors for the AXP states related to the battery, power source and low battery alert. This indirectly addresses #8.
  4. Moved the battery level to the coulomb counter. This is still a bit janky because I do not have a sane way to calibrate it. My current thinking is that I would wait until the first time the AXP goes into a full charge state and set that as the top level of the counter. Then after the next full discharge reset the counter, and use that max as the 100%. Then just repeat that process on every full charge/discharge.

Todo:

  1. Make the LOW_BAT and LOW_BAT_CRITICAL triggers. I want to have it be able to hook in an alert to battery critical and it seems a bit funny to make a binary sensor just for that.
  2. Some sort of autocalibration of the coulomb counter. I think ESPHome gives us some sort of NV area that we can store a high and low level marker.
  3. Integrate the AXP sleep into the ESPHome sleep. I want this so I can completely power off the display during sleep.
Josverl commented 3 years ago

@geiseri , I tried using your AXP integration but I am always running into an error that 'Component sensor.axp192 requires component axp192.'

This is using the axp192 component copied into the custom_components folder, and using the provided m5stick.yaml

INFO Reading configuration m5stickc.yaml...
WARNING ESP32: Pin 10 (9-10) might already be used by the flash interface in QUAD IO flash mode.
WARNING ESP32: Pin 10 (9-10) might already be used by the flash interface in QUAD IO flash mode.
WARNING ESP32: Pin 9 (9-10) might already be used by the flash interface in QUAD IO flash mode.
INFO Detected timezone 'CET' with UTC offset 1 and daylight savings time from 28 March 02:00:00 to 31 October 03:00:00
INFO Detected timezone 'CET' with UTC offset 1 and daylight savings time from 28 March 02:00:00 to 31 October 03:00:00
Failed config

sensor.axp192: [source m5stickc.yaml:53]

  Component sensor.axp192 requires component axp192.
  platform: axp192
  address: 52
  i2c_id: bus_a
  update_interval: 30s
  battery_level:
    name: M5Stick Battery Level
    id: m5stick_batterylevel

any suggestions as to what could be the cause ?

cromulus commented 3 years ago

Would love to be able to turn off the display! Thanks for all of your work!

hideosasaki commented 2 years ago

[FYI]Works fine by this yaml:

substitutions:
  devicename: m5stickc
  upper_devicename: M5Stick-C

esphome:
  name: $devicename
  platform: ESP32
  board: m5stick-c
  platformio_options:
    upload_speed: 115200

.......

axp192:
  - address: 0x34
    brightness: 1.0
    battery_capacity: 80

binary_sensor:

  - platform: gpio
    pin:
      number: GPIO37
      inverted: true
    name: ${upper_devicename} Button A
    on_press:
      then:
        - light.turn_on: led1
    on_release:
      then:
        - light.turn_off: led1

  - platform: gpio
    pin:
      number: GPIO39
      inverted: true
    name: ${upper_devicename} Button B
    on_press:
      then:
        - light.turn_on: led1
    on_release:
      then:
        - light.turn_off: led1

  - platform: axp192
    axp192_id:
    type: PLUGGED
    id: axp192_binary_sensor_plugged
    name: ${upper_devicename} plugged in

  - platform: axp192
    axp192_id:
    type: CHARGING
    id: axp192_binary_sensor_charging
    name: ${upper_devicename} charging

  - platform: axp192
    axp192_id:
    type: OVERTEMP
    id: axp192_binary_sensor_overtemp
    name: ${upper_devicename} overtemp

  - platform: axp192
    axp192_id:
    type: LOW_BATTERY
    id: axp192_binary_sensor_low_battery
    name: ${upper_devicename} low battery

  - platform: axp192
    axp192_id:
    type: CRITICAL_BATTERY
    id: axp192_binary_sensor_critical_battery
    name: ${upper_devicename} critical battery

  - platform: axp192
    axp192_id:
    type: CHARGED
    id: axp192_binary_sensor_charged
    name: ${upper_devicename} charged

sensor:
  - platform: axp192
    axp192_id: 
    id: axp192_sensor
    name: ${upper_devicename} Battery

  - platform: wifi_signal
    name: ${upper_devicename} WiFi Signal
    id: wifi_dbm

  - platform: uptime
    name: ${upper_devicename} Uptime

# internal LED
light:
  - platform: monochromatic
    output:  builtin_led
    name: ${upper_devicename} Led
    id: led1

  - platform: axp192
    axp192_id: 
    id: axp192_light
    name: ${upper_devicename} Backlight

output:
  - platform: ledc
    pin: 10
    inverted: true
    id: builtin_led

remote_transmitter:
  - pin:
      number: GPIO9
    carrier_duty_percent: 50%
    id: internal

spi:
  clk_pin: GPIO13
  mosi_pin: GPIO15

i2c:
   - id: bus_a
     sda: GPIO21
     scl: GPIO22
     scan: True

And you have to update this class axp192component.cpp to :

light::LightTraits AXP192Backlight::get_traits()
{
    auto traits = light::LightTraits();
    traits.set_supported_color_modes({
         light::ColorMode::BRIGHTNESS
    });
    // traits.set_supports_brightness(true);
    return traits;
}
geiseri commented 2 years ago

I have moved this development to: https://gitlab.com/geiseri/esphome_extras with various other fixes.