From dad89c422401aa241861e68a974d89632443c775 Mon Sep 17 00:00:00 2001 From: fvanroie <15969459+fvanroie@users.noreply.github.com> Date: Fri, 29 Jan 2021 21:00:19 +0100 Subject: [PATCH] Add device object --- src/dev/device.cpp | 9 +++++ src/dev/device.h | 35 +++++++++++++++++ src/dev/lanbonl8.cpp | 73 ++++++++++++++++++++++++++++++++++++ src/dev/lanbonl8.h | 28 ++++++++++++++ src/dev/m5stackcore2.cpp | 53 ++++++++++++++++++++++++++ src/dev/m5stackcore2.h | 27 +++++++++++++ src/drv/hasp_drv_display.cpp | 42 --------------------- src/main.cpp | 11 ++++++ 8 files changed, 236 insertions(+), 42 deletions(-) create mode 100644 src/dev/device.cpp create mode 100644 src/dev/device.h create mode 100644 src/dev/lanbonl8.cpp create mode 100644 src/dev/lanbonl8.h create mode 100644 src/dev/m5stackcore2.cpp create mode 100644 src/dev/m5stackcore2.h diff --git a/src/dev/device.cpp b/src/dev/device.cpp new file mode 100644 index 00000000..99e49f86 --- /dev/null +++ b/src/dev/device.cpp @@ -0,0 +1,9 @@ +#include "device.h" + +#if defined(LANBONL8) + #warning Lanbon L8 +#elif defined(M5STACK) + #warning M5 Stack +#else +dev::BaseDevice haspDevice; +#endif \ No newline at end of file diff --git a/src/dev/device.h b/src/dev/device.h new file mode 100644 index 00000000..98250540 --- /dev/null +++ b/src/dev/device.h @@ -0,0 +1,35 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_DEVICE_H +#define HASP_DEVICE_H + +namespace dev { + +class BaseDevice { + public: + virtual void pre_setup() + {} + virtual void post_setup() + {} + virtual void loop() + {} + virtual void loop_5s() + {} +}; + +} // namespace dev + +using dev::BaseDevice; + +#include "lanbonl8.h" +#include "m5stackcore2.h" + +#if defined(LANBONL8) + #warning Lanbon L8 +#elif defined(M5STACK) + #warning M5 Stack +#else +extern dev::BaseDevice haspDevice; +#endif +#endif \ No newline at end of file diff --git a/src/dev/lanbonl8.cpp b/src/dev/lanbonl8.cpp new file mode 100644 index 00000000..71f92851 --- /dev/null +++ b/src/dev/lanbonl8.cpp @@ -0,0 +1,73 @@ +#include "Arduino.h" +#include "lanbonl8.h" + +#if defined(LANBONL8) + + #include "driver/adc.h" + #include "esp_adc_cal.h" + + #define REF_VOLTAGE 1100 +esp_adc_cal_characteristics_t * adc_chars = + new esp_adc_cal_characteristics_t; // adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); + +namespace dev { + +static void check_efuse(void) +{ + + // Check TP is burned into eFuse + if(esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) { + printf("eFuse Two Point: Supported\n"); + } else { + printf("eFuse Two Point: NOT supported\n"); + } + + // Check Vref is burned into eFuse + if(esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) { + printf("eFuse Vref: Supported\n"); + } else { + printf("eFuse Vref: NOT supported\n"); + } +} + +static void print_char_val_type(esp_adc_cal_value_t val_type) +{ + if(val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { + Serial.print("Characterized using Two Point Value\n"); + } else if(val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) { + Serial.print("Characterized using eFuse Vref\n"); + } else { + Serial.print("Characterized using Default Vref\n"); + } +} + +void LanbonL8::pre_setup() +{ + // Check if Two Point or Vref are burned into eFuse + check_efuse(); + + // Characterize ADC + adc1_config_width(ADC_WIDTH_12Bit); + adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_0db); + esp_adc_cal_value_t val_type = + esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_0db, ADC_WIDTH_12Bit, REF_VOLTAGE, adc_chars); + print_char_val_type(val_type); +} + +void LanbonL8::post_setup() +{} + +void LanbonL8::loop() +{} + +void LanbonL8::loop_5s() +{ + double voltage = esp_adc_cal_raw_to_voltage(analogRead(39), adc_chars); + Serial.print(adc1_get_raw(ADC1_CHANNEL_3)); + Serial.print(" - "); + Serial.println(voltage); +} +} // namespace dev + +dev::LanbonL8 haspDevice; +#endif diff --git a/src/dev/lanbonl8.h b/src/dev/lanbonl8.h new file mode 100644 index 00000000..445a2204 --- /dev/null +++ b/src/dev/lanbonl8.h @@ -0,0 +1,28 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_DEVICE_LANBONL8_H +#define HASP_DEVICE_LANBONL8_H + +#include "device.h" + +#if defined(LANBONL8) + +namespace dev { + +class LanbonL8 : public BaseDevice { + public: + void pre_setup() override; + + void post_setup() override; + + void loop() override; + + void loop_5s() override; +}; +} // namespace dev + +extern dev::LanbonL8 haspDevice; + +#endif +#endif \ No newline at end of file diff --git a/src/dev/m5stackcore2.cpp b/src/dev/m5stackcore2.cpp new file mode 100644 index 00000000..d208931d --- /dev/null +++ b/src/dev/m5stackcore2.cpp @@ -0,0 +1,53 @@ +#include "device.h" +#include "m5stackcore2.h" + +#if defined(M5STACK) + #include "AXP192.h" // Power Mgmt + +// AXP192 Axp; +namespace dev { + +void M5StackCore2::pre_setup(void) +{ + AXP192 Axp; + Wire.begin(TOUCH_SDA, TOUCH_SCL); + Axp.begin(); + + Axp.SetCHGCurrent(AXP192::kCHG_100mA); + Axp.SetLcdVoltage(2800); + + Axp.SetBusPowerMode(0); + Axp.SetCHGCurrent(AXP192::kCHG_190mA); + + Axp.SetLDOEnable(3, true); + // CoverScrollText("Motor Test", M5.Lcd.color565(SUCCE_COLOR)); + delay(150); + Axp.SetLDOEnable(3, false); + + Axp.SetLed(1); + // CoverScrollText("LED Test", M5.Lcd.color565(SUCCE_COLOR)); + delay(100); + Axp.SetLed(0); + + // FastLED.addLeds(ledsBuff, LEDS_NUM); + // for(int i = 0; i < LEDS_NUM; i++) { + // ledsBuff[i].setRGB(20, 20, 20); + // } + // FastLED.show(); + + Axp.SetLDOVoltage(3, 3300); + Axp.SetLed(1); +} + +void M5StackCore2::post_setup(void) +{} + +void M5StackCore2::loop(void) +{} + +void M5StackCore2::loop_5s(void) +{} +} // namespace dev + +dev::M5StackCore2 haspDevice; +#endif diff --git a/src/dev/m5stackcore2.h b/src/dev/m5stackcore2.h new file mode 100644 index 00000000..39539fff --- /dev/null +++ b/src/dev/m5stackcore2.h @@ -0,0 +1,27 @@ +/* MIT License - Copyright (c) 2020 Francis Van Roie + For full license information read the LICENSE file in the project folder */ + +#ifndef HASP_DEVICE_M5STACKCORE2_H +#define HASP_DEVICE_M5STACKCORE2_H + +#if defined(M5STACK) + + #include "device.h" + +namespace dev { + +class M5StackCore2 : public BaseDevice { + public: + void pre_setup() override; + void post_setup() override; + void loop() override; + void loop_5s() override; +}; + +} // namespace dev + +using dev::M5StackCore2; +extern dev::M5StackCore2 haspDevice; + +#endif +#endif \ No newline at end of file diff --git a/src/drv/hasp_drv_display.cpp b/src/drv/hasp_drv_display.cpp index 9517a0f7..ad80fdf8 100644 --- a/src/drv/hasp_drv_display.cpp +++ b/src/drv/hasp_drv_display.cpp @@ -1,49 +1,7 @@ #include "hasp_drv_display.h" -#if defined(M5STACK) - #include "AXP192.h" // Power Mgmt - -AXP192 Axp; - -void m5stack_init() -{ - AXP192 Axp; - Wire.begin(TOUCH_SDA, TOUCH_SCL); - Axp.begin(); - - Axp.SetCHGCurrent(AXP192::kCHG_100mA); - Axp.SetLcdVoltage(2800); - - Axp.SetBusPowerMode(0); - Axp.SetCHGCurrent(AXP192::kCHG_190mA); - - Axp.SetLDOEnable(3, true); - // CoverScrollText("Motor Test", M5.Lcd.color565(SUCCE_COLOR)); - delay(150); - Axp.SetLDOEnable(3, false); - - Axp.SetLed(1); - // CoverScrollText("LED Test", M5.Lcd.color565(SUCCE_COLOR)); - delay(100); - Axp.SetLed(0); - - // FastLED.addLeds(ledsBuff, LEDS_NUM); - // for(int i = 0; i < LEDS_NUM; i++) { - // ledsBuff[i].setRGB(20, 20, 20); - // } - // FastLED.show(); - - Axp.SetLDOVoltage(3, 3300); - Axp.SetLed(1); -} -#endif - void drv_display_init(lv_disp_drv_t * disp_drv, uint8_t rotation, bool invert_display) { -#if defined(M5STACK) - m5stack_init(); // Set LCD power first -#endif - /* TFT init */ #if defined(USE_FSMC) fsmc_ili9341_init(rotation, invert_display); diff --git a/src/main.cpp b/src/main.cpp index 32b53671..818f0919 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,12 +14,16 @@ #include "net/hasp_network.h" +#include "dev/device.h" + bool isConnected; uint8_t mainLoopCounter = 0; unsigned long mainLastLoopTime = 0; void setup() { + haspDevice.pre_setup(); + /**************************** * Storage initializations ***************************/ @@ -128,6 +132,7 @@ void loop() #endif // TELNET debugLoop(); // Console + haspDevice.loop(); /* Timer Loop */ if(millis() - mainLastLoopTime >= 1000) { @@ -150,6 +155,12 @@ void loop() #if HASP_USE_MQTT > 0 mqttEvery5Seconds(isConnected); #endif + +#if HASP_USE_GPIO > 0 + // gpioEvery5Seconds(); +#endif + + haspDevice.loop_5s(); } /* Reset loop counter every 10 seconds */