Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonBrandao committed Sep 22, 2023
0 parents commit 09daaff
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Gitignore settings for ESPHome
# This is an example and may include too much for your use-case.
# You can modify this file to suit your needs.
.esphome/
secrets.yaml
186 changes: 186 additions & 0 deletions desk_height_sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include "esphome.h"
#include <bitset>

class DeskHeightSensor : public Component, public UARTDevice, public Sensor
{
public:
DeskHeightSensor(UARTComponent *parent) : UARTDevice(parent) {}

float value = 0;
float lastPublished = -1;
unsigned long history[5];

int msg_len = 0;
unsigned long msg_type;
bool valid = false;

float get_setup_priority() const override { return esphome::setup_priority::DATA; }

int hex_to_int(byte s)
{
std::bitset<8> b(s);

if (b[0] && b[1] && b[2] && b[3] && b[4] && b[5] && !b[6])
{
return 0;
}
if (not b[0] && b[1] && b[2] && !b[3] && !b[4] && !b[5] && !b[6])
{
return 1;
}
if (b[0] && b[1] && !b[2] && b[3] && b[4] && !b[5] && b[6])
{
return 2;
}
if (b[0] && b[1] && b[2] && b[3] && !b[4] && !b[5] && b[6])
{
return 3;
}
if (not b[0] && b[1] && b[2] && !b[3] && !b[4] && b[5] && b[6])
{
return 4;
}
if (b[0] && !b[1] && b[2] && b[3] && !b[4] && b[5] && b[6])
{
return 5;
}
if (b[0] && !b[1] && b[2] && b[3] && b[4] && b[5] && b[6])
{
return 6;
}
if (b[0] && b[1] && b[2] && !b[3] && !b[4] && !b[5] && !b[6])
{
return 7;
}
if (b[0] && b[1] && b[2] && b[3] && b[4] && b[5] && b[6])
{
return 8;
}
if (b[0] && b[1] && b[2] && b[3] && !b[4] && b[5] && b[6])
{
return 9;
}
if (!b[0] && !b[1] && !b[2] && !b[3] && !b[4] && !b[5] && b[6])
{
return 10;
}
return 0;
}

bool is_decimal(byte b)
{
return (b & 0x80) == 0x80;
}

void setup() override
{
// nothing to do here
}

void loop() override
{
while (available() > 0)
{
byte incomingByte = read();
// ESP_LOGD("DEBUG", "Incoming byte is: %08x", incomingByte);

// First byte, start of a packet
if (incomingByte == 0x9b)
{
// Reset message length
msg_len = 0;
valid = false;
}

// Second byte defines the message length
if (history[0] == 0x9b)
{
msg_len = (int)incomingByte;
}

// Third byte is message type
if (history[1] == 0x9b)
{
msg_type = incomingByte;
}

// Fourth byte is first height digit, if msg type 0x12 & msg len 7
if (history[2] == 0x9b)
{

if (msg_type == 0x12 && msg_len == 7)
{
// Empty height
if (incomingByte == 0)
{
//ESP_LOGD("DEBUG", "Height 1 is EMPTY -> 0x%02x", incomingByte);
//deskSerial.write(command_wakeup, sizeof(command_wakeup));
}
else if (hex_to_int(incomingByte) == 0)
{
//ESP_LOGD("DEBUG", "Invalid height 1 -> 0x%02x", incomingByte);
//deskSerial.write(command_wakeup, sizeof(command_wakeup));
}
else
{
valid = true;
// ESP_LOGD("DEBUG", "Height 1 is: 0x%02x", incomingByte);
}
}
}

// Fifth byte is second height digit
if (history[3] == 0x9b)
{
if (valid == true)
{
//ESP_LOGD("DEBUG", "Height 2 is: 0x%02x", incomingByte);
}
}

// Sixth byte is third height digit
if (history[4] == 0x9b)
{
if (valid == true)
{
int height1 = hex_to_int(history[1]) * 100;
int height2 = hex_to_int(history[0]) * 10;
int height3 = hex_to_int(incomingByte);
if (height2 == 100) // check if 'number' is a hyphen, return value 10 multiplied by 10
{

}
else
{
float finalHeight = height1 + height2 + height3;
if (is_decimal(history[0]))
{
finalHeight = finalHeight / 10;
}
value = finalHeight;
// ESP_LOGD("DeskHeightSensor", "Current height is: %f", finalHeight);
}
}
}



// Save byte buffer to history arrary
history[4] = history[3];
history[3] = history[2];
history[2] = history[1];
history[1] = history[0];
history[0] = incomingByte;

// End byte
if (incomingByte == 0x9d)
{
if (value && value != lastPublished)
{
publish_state(value);
lastPublished = value;
}
}
}
}
};
98 changes: 98 additions & 0 deletions flexispot-e7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
esphome:
name: flexispot-e7
comment: Flexispot E7
# includes:
# - desk_height_sensor.h

esp32:
board: az-delivery-devkit-v4
framework:
type: arduino

logger:

ota:
password: ''

wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password

ap:
ssid: 'Flexispot-E7 Fallback Hotspot'
password: !secret ap_password

web_server:
port: 80

captive_portal:

uart:
id: desk_uart
baud_rate: 9600
tx_pin: GPIO17
rx_pin: GPIO16

sensor:
- platform: wifi_signal
name: 'WiFi Signal'
update_interval: 60s

- platform: uptime
name: Uptime

# - platform: custom
# lambda: |-
# auto desk_height_sensor = new DeskHeightSensor(id(desk_uart));
# App.register_component(desk_height_sensor);
# return {desk_height_sensor};
# sensors:
# id: 'desk_height'
# name: Desk Height
# unit_of_measurement: cm
# accuracy_decimals: 1
# icon: 'mdi:counter'

switch:
# - platform: gpio
# name: 'Virtual Screen'
# pin:
# number: GPIO23
# mode: OUTPUT
# restore_mode: ALWAYS_OFF
# internal: true

- platform: uart
name: 'Preset 1'
id: switch_preset1
icon: mdi:numeric-1-box
data: [0x9b, 0x06, 0x02, 0x04, 0x00, 0xac, 0xa3, 0x9d]
uart_id: desk_uart

- platform: uart
name: 'Preset 2'
id: switch_preset2
icon: mdi:numeric-2-box
data: [0x9b, 0x06, 0x02, 0x08, 0x00, 0xac, 0xa6, 0x9d]
uart_id: desk_uart

- platform: uart
name: 'Sit' # Preset 3 on some control panels
id: switch_sit
icon: mdi:chair-rolling
data: [0x9b, 0x06, 0x02, 0x00, 0x01, 0xac, 0x60, 0x9d]
uart_id: desk_uart

- platform: uart
name: 'Stand' # Not available for all control panels
id: switch_stand
icon: mdi:human-handsup
data: [0x9b, 0x06, 0x02, 0x10, 0x00, 0xac, 0xac, 0x9d]
uart_id: desk_uart

# - platform: uart
# name: '(wake up)' # Not available on all control panels
# id: switch_wake_up
# icon: mdi:gesture-tap-button
# data: [0x9b, 0x06, 0x02, 0x00, 0x00, 0x6c, 0xa1, 0x9d]
# uart_id: desk_uart

0 comments on commit 09daaff

Please sign in to comment.