Skip to content

Commit

Permalink
add WiFi feature which is connected via serial port including telnet …
Browse files Browse the repository at this point in the history
…logging
  • Loading branch information
Lee Seungcheol committed Nov 26, 2021
1 parent f35ae45 commit d42a6de
Show file tree
Hide file tree
Showing 14 changed files with 702 additions and 119 deletions.
3 changes: 3 additions & 0 deletions libraries/STPD01/STPD01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ uint8_t STPD01::setVoltage(uint16_t volt)
val = 0xc4 + (uint16_t)(volt - 11000)/200;
} else if (volt >= 20000) {
val = 0xf1;
} else {
val = 0x00;
}

write8(STPD01_REGISTER_0, val);
tmp = read8(STPD01_REGISTER_0);
if (tmp != val) {
Expand Down
2 changes: 2 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ lib_extra_dirs = libraries
board_build.partitions = odroid.csv
monitor_speed = 115200
upload_speed = 921600
build_type = debug
monitor_filters = esp32_exception_decoder
22 changes: 14 additions & 8 deletions smartpower3/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ void Channel::setIntFlag(void)
flag_int = true;
}

void Channel::isr(void)
void Channel::isr(uint8_t onoff)
{
if (flag_int || !digitalRead(int_stpd01[channel])) {
if (stpd01->available()) {
Expand Down Expand Up @@ -489,16 +489,22 @@ uint8_t Channel::checkInterruptStat(uint8_t onoff)

if (onoff == 0)
reg_stat = 0x00;
icon_op->setInt(reg_stat & INT_OVP);
icon_sp->setInt(reg_stat & INT_SCP);
icon_tp->setInt(reg_stat & INT_OTP);
icon_tw->setInt(reg_stat & INT_OTW);
icon_ip->setInt(reg_stat & INT_IPCP);

if (flag_clear_debug == 1) {
flag_clear_debug++;
if (!hide) {
icon_op->update(channel);
icon_sp->update(channel);
icon_tp->update(channel);
icon_tw->update(channel);
icon_ip->update(channel);
}
}

if (!hide) {
icon_op->update(channel);
icon_sp->update(channel);
icon_tp->update(channel);
icon_tw->update(channel);
icon_ip->update(channel);
}

return reg_stat;
Expand Down
3 changes: 2 additions & 1 deletion smartpower3/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Channel
void clearHide();
void disabled();
void enable();
void isr(void);
void isr(uint8_t onoff);
void setIntFlag(void);
void drawInterrupt(void);
void drawPolarity(void);
Expand Down Expand Up @@ -93,6 +93,7 @@ class Channel
#endif
bool flag_int = 0;
uint8_t latch = 0;
uint8_t stat = 0;
uint16_t low_volt = 0;
uint8_t count_intr[8] = {0,};
uint8_t flag_clear_debug = 0;
Expand Down
31 changes: 27 additions & 4 deletions smartpower3/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void Header::init(uint16_t x, uint16_t y)


icon_input->init(9, x, y, FG_COLOR, BG_COLOR);
//icon_wifi->icon_init(9, x + 440, y-3, FG_COLOR, BG_COLOR);
icon_wifi->init(9, x + 400, y, FG_COLOR, BG_COLOR);
icon_log->init(9, x + 440, y, FG_COLOR, BG_COLOR);

display_mode->init(TFT_BLACK, TFT_YELLOW, 1, TL_DATUM);
Expand All @@ -44,8 +44,9 @@ void Header::init(uint16_t x, uint16_t y)
mode->init(TFT_YELLOW, TFT_BLACK, 1, TL_DATUM);
mode->setCoordinate(x + 200, y);

//icon_wifi->setIconColor(TFT_GREEN, BG_ENABLED_INT);
//icon_wifi->icon_wifi_write();
icon_wifi->setIconColor(TFT_GREEN, BG_ENABLED_INT);
icon_wifi->setIconColor(TFT_DARKGREY, BG_ENABLED_INT);
icon_wifi->wifiWrite();
icon_log->setIconColor(TFT_DARKGREY, BG_ENABLED_INT);
icon_log->logWrite();
}
Expand All @@ -62,6 +63,18 @@ void Header::offLogging(void)
update_logging_icon = true;
}

void Header::onWiFi(void)
{
flag_wifi = true;
update_wifi_icon = true;
}

void Header::offWiFi(void)
{
flag_wifi = false;
update_wifi_icon = true;
}

void Header::activate(void)
{
mode->activate();
Expand All @@ -74,7 +87,6 @@ void Header::deActivate(void)

void Header::drawMode(String str)
{

display_mode->clear();
display_mode->draw(str);
}
Expand Down Expand Up @@ -140,6 +152,17 @@ void Header::draw(void)
icon_log->logWrite();
}
}

if (update_wifi_icon) {
update_wifi_icon = false;
if (flag_wifi) {
icon_wifi->setIconColor(TFT_GREEN, BG_ENABLED_INT);
icon_wifi->wifiWrite();
} else {
icon_wifi->setIconColor(TFT_DARKGREY, BG_ENABLED_INT);
icon_wifi->wifiWrite();
}
}
}

void Header::pushPower(uint16_t volt, uint16_t ampere, uint16_t watt)
Expand Down
4 changes: 4 additions & 0 deletions smartpower3/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Header : public Component
void setDebug(void);
void onLogging(void);
void offLogging(void);
void onWiFi(void);
void offWiFi(void);
Component *display_mode;

private:
Expand All @@ -42,6 +44,8 @@ class Header : public Component
uint16_t debug = 0;
bool flag_logging = false;
bool update_logging_icon = false;
bool flag_wifi = false;
bool update_wifi_icon = false;
};

#endif
80 changes: 69 additions & 11 deletions smartpower3/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

bool Screen::_int = false;

uint8_t baud_idx;

Screen::Screen()
{
tft.init();
Expand Down Expand Up @@ -34,6 +36,13 @@ void Screen::begin(TwoWire *theWire)

setting = new Setting(&tft);

WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
server = WiFiServer(WIFI_SERVER_PORT);
server.begin();
wifiManager = new WiFiManager(server, client);

fsInit();
delay(2000);
tft.setRotation(3);
Expand All @@ -55,7 +64,7 @@ void Screen::initLED()
ledcWrite(1, 50);
}

int8_t* Screen::getOnOff()
uint8_t* Screen::getOnOff()
{
return onoff;
}
Expand All @@ -70,7 +79,7 @@ void Screen::run()
for (int i = 0; i < 2; i++) {
if (!enabled_stpd01[i])
continue;
channel[i]->isr();
channel[i]->isr(onoff[i]);
}
}
};
Expand Down Expand Up @@ -176,10 +185,12 @@ void Screen::checkOnOff()

header->init(5, 8);
if (mode >= SETTING) {
setting->init(10, 100);
setting->init(10, 80);
for (int i = 0; i < 2; i++)
tft.drawLine(0, 50 + i, 480, 50 + i, TFT_DARKGREY);
activated = dial_cnt = dial_cnt_old = STATE_NONE;
updated_wifi_info = true;
updated_wifi_icon = true;
} else {
initScreen();
}
Expand Down Expand Up @@ -283,7 +294,9 @@ void Screen::drawBase()
channel[0]->setHide();
channel[1]->setHide();
mode = SETTING;
setting->init(10, 100);
setting->init(10, 80);
updated_wifi_info = true;
updated_wifi_icon = true;
activated = dial_cnt = dial_cnt_old = STATE_NONE;
}

Expand Down Expand Up @@ -527,6 +540,14 @@ uint16_t Screen::getLogInterval(void)
return tmp;
}

void Screen::setWiFiIcon(bool onoff)
{
if (onoff)
header->onWiFi();
else
header->offWiFi();
}

void Screen::drawScreen()
{
switch (mode) {
Expand Down Expand Up @@ -557,11 +578,29 @@ void Screen::drawScreen()
if (onoff[1])
channel[1]->drawChannel();
isrSTPD01();
} else {
if (updated_wifi_info) {
updated_wifi_info = false;
if (WiFi.status() == WL_CONNECTED) {
setting->drawSSID(WiFi.SSID());
setting->drawIpaddr(WiFi.localIP().toString());
} else {
setting->drawSSID("WiFi not connected");
setting->drawIpaddr("configure via serial");
}
}
}
if (updated_wifi_icon) {
updated_wifi_icon = false;
if (WiFi.status() == 3)
setWiFiIcon(true);
else
setWiFiIcon(false);
}
header->draw();
}
channel[0]->drawVoltSet();
channel[1]->drawVoltSet();
channel[0]->drawVoltSet();
channel[1]->drawVoltSet();
}

void Screen::changeVolt(screen_mode_t mode)
Expand Down Expand Up @@ -771,8 +810,6 @@ void Screen::drawBmp(const char *filename, int16_t x, int16_t y)
uint16_t w, h, row, col;
uint8_t r, g, b;

uint32_t startTime = millis();

if (read16(bmpFS) == 0x4D42)
{
read32(bmpFS);
Expand Down Expand Up @@ -811,8 +848,6 @@ void Screen::drawBmp(const char *filename, int16_t x, int16_t y)
tft.pushImage(x, y--, w, 1, (uint16_t*)lineBuffer);
}
tft.setSwapBytes(oldSwapBytes);
Serial.print("Loaded in "); Serial.print(millis() - startTime);
Serial.println(" ms");
}
else Serial.println("BMP format not recognized.");
}
Expand Down Expand Up @@ -850,7 +885,7 @@ void Screen::fsInit(void)
Serial.println("First boot!!!");
NVS.setInt("autorun", 0);
NVS.setInt("bl_level", 3);
NVS.setInt("serial_baud", 115200);
NVS.setInt("serial_baud", (uint32_t)115200, true);
NVS.setInt("log_interval", 0);
NVS.setInt("firstboot", 1);
NVS.setString("voltage0", "5.0");
Expand All @@ -866,6 +901,9 @@ void Screen::fsInit(void)
serial_baud = NVS.getInt("serial_baud");
log_interval = NVS.getInt("log_interval");

if (NVS.getString("wifi_conn_ok") != "true")
wifiManager->state = 1;

channel[0]->setVolt(volt_set0, 1);
channel[0]->setCurrentLimit(current_limit0, 1);
channel[1]->setVolt(volt_set1, 1);
Expand Down Expand Up @@ -924,3 +962,23 @@ void Screen::writePowerLED(uint8_t val)
{
ledcWrite(1, val);
}

void Screen::runWiFiLogging(const char *buf0, const char *buf1, const char *buf2)
{
if (!client.connected()) {
client = server.available();
} else {
if (client.available()) {
client.read();
}
client.write(buf0, SIZE_LOG_BUFFER0);
client.write(buf1, SIZE_LOG_BUFFER1);
client.write(buf2, SIZE_LOG_BUFFER2);
}
}

void Screen::updateWiFiInfo(void)
{
this->updated_wifi_info = true;
this->updated_wifi_icon = true;
}
19 changes: 17 additions & 2 deletions smartpower3/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
#include "setting.h"
#include <STPD01.h>
#include <ArduinoNvs.h>
#include "wifimanager.h"

#define LED2 13
#define LED1 2

#define SIZE_LOG_BUFFER0 31
#define SIZE_LOG_BUFFER1 23
#define SIZE_LOG_BUFFER2 24

enum screen_mode_t {
BASE = 0,
BASE_MOVE,
Expand All @@ -33,14 +38,16 @@ enum state_setting {
STATE_NONE
};

#define WIFI_SERVER_PORT 23

class Screen
{
public:
Screen();
void begin(TwoWire *theWire = &Wire);
void pushPower(uint16_t volt, uint16_t ampere, uint16_t watt, uint8_t ch);
void pushInputPower(uint16_t volt, uint16_t ampere, uint16_t watt);
int8_t* getOnOff(void);
uint8_t* getOnOff(void);
void run(void);
void drawScreen(void);
void activate();
Expand Down Expand Up @@ -76,6 +83,12 @@ class Screen
void writePowerLED(uint8_t val);
void initLED(void);
void dimmingLED(uint8_t led);
void setWiFiIcon(bool onoff);
WiFiManager *wifiManager;
void runWiFiLogging(const char *buf0, const char *buf1, const char *buf2);
void updateWiFiInfo(void);
WiFiServer server;
WiFiClient client;
private:
TFT_eSPI tft = TFT_eSPI();
screen_mode_t mode = BASE;
Expand All @@ -88,7 +101,7 @@ class Screen
uint32_t cur_time = 0;
uint32_t task_time = 0;
uint32_t time_print[2] = {0, 0};
int8_t onoff[2] = {2, 2};
uint8_t onoff[2] = {2, 2};
bool btn_pressed[5] = {false,};
int16_t dial_cnt = 0;
int16_t dial_cnt_old;
Expand Down Expand Up @@ -123,4 +136,6 @@ class Screen
uint8_t flag_long_press = false;
uint32_t count_long_press = 0;
bool shutdown = false;
bool updated_wifi_icon = false;
bool updated_wifi_info = false;
};
Loading

0 comments on commit d42a6de

Please sign in to comment.