diff --git a/Boards.h b/Boards.h index 1345081..1528345 100644 --- a/Boards.h +++ b/Boards.h @@ -152,6 +152,7 @@ #define HAS_DISPLAY false #define HAS_BLUETOOTH false #define HAS_BLE false + #define HAS_WIFI false #define HAS_TCXO false #define HAS_PMU false #define HAS_NP false @@ -211,6 +212,7 @@ #define EEPROM_SIZE 1024 #define EEPROM_OFFSET EEPROM_SIZE-EEPROM_RESERVED + #define CONFIG_OFFSET 0 #define GPS_BAUD_RATE 9600 #define PIN_GPS_TX 12 @@ -345,6 +347,7 @@ #elif BOARD_MODEL == BOARD_HELTEC32_V3 #define IS_ESP32S3 true #define HAS_DISPLAY true + #define HAS_WIFI true #define HAS_BLUETOOTH false #define HAS_BLE true #define HAS_PMU true @@ -386,6 +389,7 @@ #define HAS_DISPLAY true #define HAS_BLUETOOTH false #define HAS_BLE true + #define HAS_WIFI true #define HAS_PMU true #define HAS_CONSOLE true #define HAS_EEPROM true @@ -484,6 +488,7 @@ #define IS_ESP32S3 true #define HAS_DISPLAY true #define HAS_CONSOLE true + #define HAS_WIFI true #define HAS_BLUETOOTH false #define HAS_BLE true #define HAS_PMU true @@ -557,6 +562,7 @@ #define HAS_DISPLAY false #define HAS_CONSOLE false + #define HAS_WIFI true #define HAS_BLUETOOTH false #define HAS_BLE true #define HAS_PMU true @@ -613,6 +619,7 @@ #define HAS_DISPLAY true #define HAS_CONSOLE true + #define HAS_WIFI true #define HAS_BLUETOOTH false #define HAS_BLE true #define HAS_PMU true @@ -664,6 +671,7 @@ #define HAS_DISPLAY false #define HAS_CONSOLE true + #define HAS_WIFI true #define HAS_BLUETOOTH false #define HAS_BLE true #define HAS_NP false diff --git a/Config.h b/Config.h index ca102d0..8eab089 100644 --- a/Config.h +++ b/Config.h @@ -40,6 +40,17 @@ bool bt_enabled = false; bool bt_allow_pairing = false; + #define WR_CHANNEL_DEFAULT 1 + #define WR_WIFI_OFF 0x00 + #define WR_WIFI_STA 0x01 + #define WR_WIFI_AP 0x02 + #define WR_STATE_NA 0xff + #define WR_STATE_OFF 0x00 + #define WR_STATE_ON 0x01 + #define WR_STATE_CONNECTED 0x02 + uint8_t wr_state = WR_STATE_OFF; + uint8_t wr_channel = WR_CHANNEL_DEFAULT; + #define M_FRQ_S 27388122 #define M_FRQ_R 27388061 bool console_active = false; @@ -54,6 +65,7 @@ bool mw_radio_online = false; #define eeprom_addr(a) (a+EEPROM_OFFSET) + #define config_addr(a) (a+CONFIG_OFFSET) #if (MODEM == SX1262 || MODEM == SX1280) && defined(NRF52840_XXAA) SPIClass spiModem(NRF_SPIM2, pin_miso, pin_sclk, pin_mosi); diff --git a/Display.h b/Display.h index 83dd394..d359574 100644 --- a/Display.h +++ b/Display.h @@ -162,6 +162,25 @@ int p_as_y = 0; GFXcanvas1 stat_area(64, 64); GFXcanvas1 disp_area(64, 64); +static const uint8_t one_counts[256] = { + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, + 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, + 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 +}; + void fillRect(int16_t x, int16_t y, int16_t width, int16_t height, uint16_t colour); void update_area_positions() { @@ -520,12 +539,35 @@ void drawBitmap(int16_t startX, int16_t startY, const uint8_t* bitmap, int16_t b #endif } +extern uint8_t wifi_mode; +extern bool wifi_is_connected(); +extern bool wifi_host_is_connected(); void draw_cable_icon(int px, int py) { - if (cable_state == CABLE_STATE_DISCONNECTED) { - stat_area.drawBitmap(px, py, bm_cable+0*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); - } else if (cable_state == CABLE_STATE_CONNECTED) { - stat_area.drawBitmap(px, py, bm_cable+1*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); - } + #if HAS_WIFI + if (wifi_mode == WR_WIFI_OFF) { + if (cable_state == CABLE_STATE_DISCONNECTED) { stat_area.drawBitmap(px, py, bm_cable+0*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + else if (cable_state == CABLE_STATE_CONNECTED) { stat_area.drawBitmap(px, py, bm_cable+1*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + } else { + if (wifi_mode == WR_WIFI_STA) { + if (wifi_is_connected()) { + stat_area.drawBitmap(px, py, bm_wifi+3*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); + if (!wifi_host_is_connected()) { stat_area.fillRect(px+5, py+12, 6, 3, SSD1306_BLACK); } + } else { stat_area.drawBitmap(px, py, bm_wifi+2*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + + } else if (wifi_mode == WR_WIFI_AP) { + if (wifi_host_is_connected()) { stat_area.drawBitmap(px, py, bm_wifi+1*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + else { stat_area.drawBitmap(px, py, bm_wifi+0*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + + } else { + if (cable_state == CABLE_STATE_DISCONNECTED) { stat_area.drawBitmap(px, py, bm_cable+0*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + else if (cable_state == CABLE_STATE_CONNECTED) { stat_area.drawBitmap(px, py, bm_cable+1*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + } + } + + #else + if (cable_state == CABLE_STATE_DISCONNECTED) { stat_area.drawBitmap(px, py, bm_cable+0*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + else if (cable_state == CABLE_STATE_CONNECTED) { stat_area.drawBitmap(px, py, bm_cable+1*32, 16, 16, SSD1306_WHITE, SSD1306_BLACK); } + #endif } void draw_bt_icon(int px, int py) { @@ -760,6 +802,7 @@ const uint8_t pages = 3; uint8_t disp_page = START_PAGE; extern char bt_devname[11]; extern char bt_dh[16]; +extern IPAddress wr_device_ip; void draw_disp_area() { if (!device_init_done || firmware_update_mode) { uint8_t p_by = 37; @@ -837,13 +880,32 @@ void draw_disp_area() { if (device_signatures_ok()) { disp_area.drawBitmap(0, 0, bm_def_lc, disp_area.width(), 23, SSD1306_WHITE, SSD1306_BLACK); } else { disp_area.drawBitmap(0, 0, bm_def, disp_area.width(), 23, SSD1306_WHITE, SSD1306_BLACK); } - disp_area.setFont(SMALL_FONT); disp_area.setTextWrap(false); disp_area.setTextColor(SSD1306_WHITE); disp_area.setTextSize(2); - disp_area.fillRect(0, 20, disp_area.width(), 17, SSD1306_BLACK); uint8_t ofsc = 0; - if ((bt_dh[14] & 0b00001111) == 0x01) { ofsc += 8; } - if ((bt_dh[14] >> 4) == 0x01) { ofsc += 8; } - if ((bt_dh[15] & 0b00001111) == 0x01) { ofsc += 8; } - if ((bt_dh[15] >> 4) == 0x01) { ofsc += 8; } - disp_area.setCursor(17+ofsc, 32); disp_area.printf("%02X%02X", bt_dh[14], bt_dh[15]); + bool display_ip = false; + #if HAS_WIFI + if (wifi_is_connected() && disp_page%2 == 1) { display_ip = true; } + #endif + if (display_ip) { + #if HAS_WIFI + uint8_t ones = 3+one_counts[wr_device_ip[0]]+one_counts[wr_device_ip[1]]+one_counts[wr_device_ip[2]]+one_counts[wr_device_ip[3]]; + uint8_t chars = 7; + for (uint8_t i = 0; i<4; i++) { if (wr_device_ip[i] > 9) { chars++; } if (wr_device_ip[i] > 99) { chars++; } } + uint8_t width = chars*6-(ones*4); + int alignment_offset = disp_area.width()-width; + int ipxpos = alignment_offset; + disp_area.setFont(SMALL_FONT); disp_area.setTextWrap(false); disp_area.setTextColor(SSD1306_WHITE); disp_area.setTextSize(1); + disp_area.fillRect(0, 20, disp_area.width(), 17, SSD1306_BLACK); + disp_area.setCursor(3, 34-8); disp_area.print("WiFi IP:"); + disp_area.setCursor(ipxpos, 34); disp_area.print(wr_device_ip); + #endif + } else { + disp_area.setFont(SMALL_FONT); disp_area.setTextWrap(false); disp_area.setTextColor(SSD1306_WHITE); disp_area.setTextSize(2); + disp_area.fillRect(0, 20, disp_area.width(), 17, SSD1306_BLACK); uint8_t ofsc = 0; + if ((bt_dh[14] & 0b00001111) == 0x01) { ofsc += 8; } + if ((bt_dh[14] >> 4) == 0x01) { ofsc += 8; } + if ((bt_dh[15] & 0b00001111) == 0x01) { ofsc += 8; } + if ((bt_dh[15] >> 4) == 0x01) { ofsc += 8; } + disp_area.setCursor(17+ofsc, 32); disp_area.printf("%02X%02X", bt_dh[14], bt_dh[15]); + } } if (!hw_ready || radio_error || !device_firmware_ok()) { diff --git a/Framing.h b/Framing.h index 94f4e37..b05bb7e 100644 --- a/Framing.h +++ b/Framing.h @@ -65,11 +65,16 @@ #define CMD_BT_UNPAIR 0x70 #define CMD_BT_PIN 0x62 #define CMD_DIS_IA 0x69 + #define CMD_WIFI_MODE 0x6A + #define CMD_WIFI_SSID 0x6B + #define CMD_WIFI_PSK 0x6C + #define CMD_WIFI_CHN 0x6E #define CMD_BOARD 0x47 #define CMD_PLATFORM 0x48 #define CMD_MCU 0x49 #define CMD_FW_VERSION 0x50 + #define CMD_CFG_READ 0x6D #define CMD_ROM_READ 0x51 #define CMD_ROM_WRITE 0x52 #define CMD_CONF_SAVE 0x53 diff --git a/Graphics.h b/Graphics.h index 3da21f1..6eb00e0 100644 --- a/Graphics.h +++ b/Graphics.h @@ -31,6 +31,17 @@ const unsigned char bm_rf [] PROGMEM = { 0x31, 0x48, 0x61, 0xca, 0x74, 0x4e, 0x00, 0x00, 0x01, 0x80, 0x04, 0x20, 0x03, 0xc0, 0x00, 0x00 }; +const unsigned char bm_wifi [] PROGMEM = { + 0x00, 0x00, 0x07, 0xe0, 0x08, 0x10, 0x13, 0xc8, 0x14, 0x28, 0x01, 0x80, 0x00, 0x00, 0x04, 0x60, + 0x0a, 0x50, 0x0e, 0x60, 0x0a, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x07, 0xe0, 0x08, 0x10, 0x13, 0xc8, 0x14, 0x28, 0x01, 0x80, 0x00, 0x00, 0x04, 0x60, + 0x0a, 0x50, 0x0e, 0x60, 0x0a, 0x40, 0x00, 0x00, 0x01, 0x80, 0x04, 0x20, 0x03, 0xc0, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0d, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x74, + 0x54, 0x40, 0x55, 0x64, 0x29, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x07, 0xe0, 0x08, 0x10, 0x13, 0xc8, 0x14, 0x28, 0x01, 0x80, 0x00, 0x00, 0x45, 0x74, + 0x54, 0x40, 0x55, 0x64, 0x29, 0x44, 0x00, 0x00, 0x01, 0x80, 0x04, 0x20, 0x03, 0xc0, 0x00, 0x00 +}; + const unsigned char bm_bt [] PROGMEM = { 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x40, 0x00, 0x00, 0x05, 0x10, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x01, 0x40, 0x00, 0x00, 0x05, 0x10, 0x00, 0x00, 0x11, 0x40, 0x00, 0x00, 0x01, 0x00, diff --git a/Makefile b/Makefile index 758f664..6443600 100644 --- a/Makefile +++ b/Makefile @@ -204,15 +204,15 @@ upload-heltec32_v3: arduino-cli upload -p /dev/ttyUSB0 --fqbn esp32:esp32:heltec_wifi_lora_32_V3 @sleep 1 rnodeconf /dev/ttyUSB0 --firmware-hash $$(./partition_hashes ./build/esp32.esp32.heltec_wifi_lora_32_V3/RNode_Firmware.ino.bin) - @sleep 3 - python ./Release/esptool/esptool.py --chip esp32-s3 --port /dev/ttyUSB0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x210000 ./Release/console_image.bin + #@sleep 3 + #python ./Release/esptool/esptool.py --chip esp32-s3 --port /dev/ttyUSB0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x210000 ./Release/console_image.bin upload-heltec32_v4: arduino-cli upload -p /dev/ttyACM0 --fqbn esp32:esp32:esp32s3 @sleep 1 rnodeconf /dev/ttyACM0 --firmware-hash $$(./partition_hashes ./build/esp32.esp32.esp32s3/RNode_Firmware.ino.bin) - @sleep 3 - python ./Release/esptool/esptool.py --chip esp32-s3 --port /dev/ttyACM0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x210000 ./Release/console_image.bin + #@sleep 3 + #python ./Release/esptool/esptool.py --chip esp32-s3 --port /dev/ttyACM0 --baud 921600 --before default_reset --after hard_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size 4MB 0x210000 ./Release/console_image.bin upload-tdeck: arduino-cli upload -p /dev/ttyACM0 --fqbn esp32:esp32:esp32s3 diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 42177ba..0f50013 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -272,6 +272,10 @@ void setup() { kiss_indicate_reset(); #endif } else { + #if HAS_WIFI + wifi_mode = EEPROM.read(eeprom_addr(ADDR_CONF_WIFI)); + if (wifi_mode == WR_WIFI_STA || wifi_mode == WR_WIFI_AP) { wifi_remote_init(); } + #endif kiss_indicate_reset(); } #endif @@ -1012,6 +1016,8 @@ void serial_callback(uint8_t sbyte) { } } else if (command == CMD_ROM_READ) { kiss_dump_eeprom(); + } else if (command == CMD_CFG_READ) { + kiss_dump_config(); } else if (command == CMD_ROM_WRITE) { if (sbyte == FESC) { ESCAPE = true; @@ -1134,6 +1140,56 @@ void serial_callback(uint8_t sbyte) { device_save_firmware_hash(); } #endif + } else if (command == CMD_WIFI_CHN) { + #if HAS_WIFI + if (sbyte > 0 && sbyte < 14) { eeprom_update(eeprom_addr(ADDR_CONF_WCHN), sbyte); } + #endif + } else if (command == CMD_WIFI_MODE) { + #if HAS_WIFI + if (sbyte == WR_WIFI_OFF || sbyte == WR_WIFI_STA || sbyte == WR_WIFI_AP) { + wr_conf_save(sbyte); + wifi_mode = sbyte; + wifi_remote_init(); + } + #endif + } else if (command == CMD_WIFI_SSID) { + #if HAS_WIFI + if (sbyte == FESC) { ESCAPE = true; } + else { + if (ESCAPE) { + if (sbyte == TFEND) sbyte = FEND; + if (sbyte == TFESC) sbyte = FESC; + ESCAPE = false; + } + if (frame_len < CMD_L) cmdbuf[frame_len++] = sbyte; + } + + if (sbyte == 0x00) { + for (uint8_t i = 0; i<33; i++) { + if (i= WR_STATE_ON && wifi_remote_available()) ) + #else ( (bt_state != BT_STATE_CONNECTED && Serial.available()) || (bt_state == BT_STATE_CONNECTED && SerialBT.available()) ) + #endif ) #else while (c < MAX_CYCLES && Serial.available()) @@ -1763,23 +1827,15 @@ void buffer_serial() { c++; #if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52 - if (!fifo_isfull_locked(&serialFIFO)) { - fifo_push_locked(&serialFIFO, Serial.read()); - } - #elif HAS_BLUETOOTH || HAS_BLE == true - if (bt_state == BT_STATE_CONNECTED) { - if (!fifo_isfull(&serialFIFO)) { - fifo_push(&serialFIFO, SerialBT.read()); - } - } else { - if (!fifo_isfull(&serialFIFO)) { - fifo_push(&serialFIFO, Serial.read()); - } - } + if (!fifo_isfull_locked(&serialFIFO)) { fifo_push_locked(&serialFIFO, Serial.read()); } + #elif HAS_BLUETOOTH || HAS_BLE == true || HAS_WIFI + if (bt_state == BT_STATE_CONNECTED) { if (!fifo_isfull(&serialFIFO)) { fifo_push(&serialFIFO, SerialBT.read()); } } + #if HAS_WIFI + else if (wifi_host_is_connected()) { if (!fifo_isfull(&serialFIFO)) { fifo_push(&serialFIFO, wifi_remote_read()); } } + #endif + else { if (!fifo_isfull(&serialFIFO)) { fifo_push(&serialFIFO, Serial.read()); } } #else - if (!fifo_isfull(&serialFIFO)) { - fifo_push(&serialFIFO, Serial.read()); - } + if (!fifo_isfull(&serialFIFO)) { fifo_push(&serialFIFO, Serial.read()); } #endif } diff --git a/ROM.h b/ROM.h index 27c38bb..e1d6838 100644 --- a/ROM.h +++ b/ROM.h @@ -44,12 +44,18 @@ #define ADDR_CONF_PINT 0xB6 #define ADDR_CONF_BSET 0xB7 #define ADDR_CONF_DIA 0xB9 + #define ADDR_CONF_WIFI 0xBA + #define ADDR_CONF_WCHN 0xBB #define INFO_LOCK_BYTE 0x73 #define CONF_OK_BYTE 0x73 #define BT_ENABLE_BYTE 0x73 #define EEPROM_RESERVED 200 + + #define CONFIG_SIZE 256 + #define ADDR_CONF_SSID 0x00 + #define ADDR_CONF_PSK 0x21 ////////////////////////////////// #endif diff --git a/Remote.h b/Remote.h new file mode 100644 index 0000000..9721148 --- /dev/null +++ b/Remote.h @@ -0,0 +1,162 @@ +// Copyright (C) 2024, Mark Qvist + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include + +#if CONFIG_IDF_TARGET_ESP32 + #include "esp32/rom/rtc.h" +#elif CONFIG_IDF_TARGET_ESP32S2 + #include "esp32s2/rom/rtc.h" +#elif CONFIG_IDF_TARGET_ESP32C3 + #include "esp32c3/rom/rtc.h" +#elif CONFIG_IDF_TARGET_ESP32S3 + #include "esp32s3/rom/rtc.h" +#else + #error Target CONFIG_IDF_TARGET is not supported +#endif + +#define WIFI_UPDATE_INTERVAL_MS 500 + +uint32_t wifi_update_interval_ms = WIFI_UPDATE_INTERVAL_MS; +uint32_t last_wifi_update = 0; + +WiFiClient connection = NULL; +WiFiServer remote_listener(7633); +IPAddress ap_ip(10, 0, 0, 1); +IPAddress ap_nm(255, 255, 255, 0); +IPAddress wr_device_ip; +wl_status_t wr_wifi_status = WL_IDLE_STATUS; + +uint8_t wifi_mode = WIFI_OFF; +bool wifi_initialized = false; + +char wr_ssid[33]; +char wr_psk[33]; + +void wifi_dbg(String msg) { Serial.print("[WiFi] "); Serial.println(msg); } + +uint8_t wifi_remote_mode() { return wifi_mode; } + +void wifi_update_status() { + wr_wifi_status = WiFi.status(); + if (wr_wifi_status == WL_CONNECTED) { wr_device_ip = WiFi.localIP(); } + if (wifi_mode == WR_WIFI_AP && wifi_initialized) { wr_device_ip = WiFi.softAPIP(); wr_wifi_status = WL_CONNECTED; } +} + +bool wifi_is_connected() { return (wr_wifi_status == WL_CONNECTED); } +bool wifi_host_is_connected() { if (connection) { return true; } else { return false; } } + +void wifi_remote_start_ap() { + WiFi.mode(WIFI_AP); + if (wr_ssid[0] != 0x00) { + if (wr_psk[0] != 0x00) { Serial.printf("Starting Access Point: %s / %s\n", wr_ssid, wr_psk); WiFi.softAP(wr_ssid, wr_psk, wr_channel); } + else { Serial.printf("Starting Access Point: %s\n", wr_ssid); WiFi.softAP(wr_ssid, NULL, wr_channel); } + } else { + if (wr_psk[0] != 0x00) { Serial.printf("Starting Access Point: %s / %s\n", bt_devname, wr_psk); WiFi.softAP(bt_devname, wr_psk, wr_channel); } + else { Serial.printf("Starting Access Point: %s\n", bt_devname); WiFi.softAP(bt_devname, NULL, wr_channel); } + } + delay(150); + WiFi.softAPConfig(ap_ip, ap_ip, ap_nm); + wifi_initialized = true; +} + +void wifi_remote_start_sta() { + WiFi.mode(WIFI_STA); + delay(100); + if (wr_ssid[0] != 0x00) { + if (wr_psk[0] != 0x00) { WiFi.begin(wr_ssid, wr_psk); } + else { WiFi.begin(wr_ssid); } + } + + delay(500); + wr_wifi_status = WiFi.status(); + wifi_initialized = true; +} + +void wifi_remote_stop() { + WiFi.softAPdisconnect(true); + WiFi.disconnect(true, true); + WiFi.mode(WIFI_MODE_NULL); + wifi_initialized = false; +} + +void wifi_remote_start() { + if (wifi_mode == WR_WIFI_AP) { wifi_remote_start_ap(); } + else if (wifi_mode == WR_WIFI_STA) { wifi_remote_start_sta(); } + else { wifi_remote_stop(); } + + if (wifi_initialized == true) { + remote_listener.begin(); + wr_state = WR_STATE_ON; + } else { remote_listener.end(); wr_state = WR_STATE_OFF; } +} + +void wifi_remote_init() { + WiFi.softAPdisconnect(true); + WiFi.disconnect(true, true); + WiFi.mode(WIFI_MODE_NULL); + WiFi.setHostname(bt_devname); + wr_ssid[32] = 0x00; wr_psk[32] = 0x00; + for (uint8_t i = 0; i < 32; i++) { wr_ssid[i] = EEPROM.read(config_addr(ADDR_CONF_SSID+i)); if (wr_ssid[i] == 0xFF) { wr_ssid[i] = 0x00; } } + for (uint8_t i = 0; i < 32; i++) { wr_psk[i] = EEPROM.read(config_addr(ADDR_CONF_PSK+i)); if (wr_psk[i] == 0xFF) { wr_psk[i] = 0x00; } } + wr_channel = EEPROM.read(eeprom_addr(ADDR_CONF_WCHN)); if (wr_channel < 1 || wr_channel > 14) { wr_channel = WR_CHANNEL_DEFAULT; } + wifi_remote_start(); +} + +void wifi_remote_close_all() { + if (connection) { connection.stop(); } + WiFiClient client = remote_listener.available(); + while (client) { client.stop(); client = remote_listener.available(); } + connection = NULL; + wr_state = WR_STATE_ON; +} + +bool wifi_remote_available() { + if (connection) { + if (connection.connected()) { + if (connection.available()) { return true; } + else { return false; } + } else { + wifi_remote_close_all(); + return false; + } + } else { + WiFiClient client = remote_listener.available(); + if (!client) { return false; } + else { + connection = client; + wr_state = WR_STATE_CONNECTED; + if (connection.available()) { return true; } + else { return false; } + } + } +} + +uint8_t wifi_remote_read() { + if (connection && connection.available()) { return connection.read(); } + else { + if (connection) { wifi_remote_close_all(); } + wifi_dbg("Error: No data to read from TCP socket"); return 0x00; + } +} + +void wifi_remote_write(uint8_t byte) { if (connection) { connection.write(byte); } } + +void update_wifi() { + if (millis()-last_wifi_update >= wifi_update_interval_ms) { + wifi_update_status(); + last_wifi_update = millis(); + } +} \ No newline at end of file diff --git a/Utilities.h b/Utilities.h index b31ff90..785ef5f 100644 --- a/Utilities.h +++ b/Utilities.h @@ -60,6 +60,10 @@ uint8_t eeprom_read(uint32_t mapped_addr); #include "Bluetooth.h" #endif +#if HAS_WIFI == true + #include "Remote.h" +#endif + #if HAS_PMU == true #include "Power.h" #endif @@ -797,7 +801,12 @@ int8_t led_standby_direction = 0; void serial_write(uint8_t byte) { #if HAS_BLUETOOTH || HAS_BLE == true if (bt_state != BT_STATE_CONNECTED) { - Serial.write(byte); + #if HAS_WIFI + if (wifi_host_is_connected()) { wifi_remote_write(byte); } + else { Serial.write(byte); } + #else + Serial.write(byte); + #endif } else { SerialBT.write(byte); #if MCU_VARIANT == MCU_NRF52 && HAS_BLE @@ -1286,7 +1295,6 @@ int map_target_power_to_modem_output(int target_tx_power) { for (int i = 0; i < PA_GAIN_POINTS; i++) { int gain = tx_gain[i]; int effective_output_dbm = i + gain; - printf("At %d dBm modem output, gain is %d dBm, effective output is %d\n", i, gain, effective_output_dbm); if (effective_output_dbm > target_tx_power) { int diff = effective_output_dbm - target_tx_power; modem_output_dbm = -1*diff; @@ -1490,6 +1498,15 @@ void eeprom_dump_all() { } } +void eeprom_config_dump_all() { + #if MCU_VARIANT == MCU_ESP32 + for (int addr = 0; addr < CONFIG_SIZE; addr++) { + uint8_t byte = EEPROM.read(config_addr(addr)); + escaped_serial_write(byte); + } + #endif +} + void kiss_dump_eeprom() { serial_write(FEND); serial_write(CMD_ROM_READ); @@ -1497,6 +1514,13 @@ void kiss_dump_eeprom() { serial_write(FEND); } +void kiss_dump_config() { + serial_write(FEND); + serial_write(CMD_CFG_READ); + eeprom_config_dump_all(); + serial_write(FEND); +} + #if !HAS_EEPROM && MCU_VARIANT == MCU_NRF52 void eeprom_flush() { file.close(); @@ -1680,6 +1704,14 @@ bool eeprom_checksum_valid() { return checksum_valid; } +void wr_conf_save(uint8_t mode) { + eeprom_update(eeprom_addr(ADDR_CONF_WIFI), mode); + #if !HAS_EEPROM && MCU_VARIANT == MCU_NRF52 + // have to do a flush because we're only writing 1 byte and it syncs after 8 + eeprom_flush(); + #endif +} + void bt_conf_save(bool is_enabled) { if (is_enabled) { eeprom_update(eeprom_addr(ADDR_CONF_BT), BT_ENABLE_BYTE);