Skip to content

Commit

Permalink
Bugfixes (#15)
Browse files Browse the repository at this point in the history
* Add Heltec module support

* Fix issue #14 HW Capture not working on Mega

* Fix issue #12 Compile errors

Update code to work with ESP32 support v2.0.4 (current on Arduino and on PlatformIO).

* Get rid of 'undefined LED_BUILTIN' errors.

* Fix compilation errors on Heltek Dev Kit 32

* Add yet another Heltec board category

* Update .gitignore

* Remove excluded files
  • Loading branch information
Neil-McK authored Mar 23, 2023
1 parent 40ea1b2 commit 3d90b61
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 317 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
.gcc-flags.json
.pio
.vscode
.vscode/*
**/hardware/*backups/
239 changes: 0 additions & 239 deletions .vscode/c_cpp_properties.json

This file was deleted.

7 changes: 0 additions & 7 deletions .vscode/extensions.json

This file was deleted.

34 changes: 0 additions & 34 deletions .vscode/launch.json

This file was deleted.

8 changes: 6 additions & 2 deletions Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
#define config_h

// The following logic should work for all supported platforms, hopefully.
#if defined(ARDUINO_heltec_wifi_kit_32) || defined(ARDUINO_WIFI_KIT_32)
#define ARDUINO_HELTEC_WIFI_KIT_32
#endif

#if defined(ESP_PLATFORM)
#if !defined(ESP32)
#define ESP32
#endif
#elif defined(ESP8266)
// nothing needs done here.
// nothing needs done here.
#elif defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO)
#define ARDUINO_UNO_NANO
#elif defined(ARDUINO_AVR_MEGA2560)
Expand Down Expand Up @@ -89,7 +93,7 @@
#define OLED_I2CADDRESS 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#elif defined(ESP32) // Heltec Kit 32 has pins 4/15 predefined for I2C.
#elif defined(ARDUINO_HELTEC_WIFI_KIT_32) // Heltec Kit 32 has pins 4/15 predefined for I2C.
// #define SDA_OLED 4
// #define SCL_OLED 15
#define OLED_RESET 16
Expand Down
25 changes: 24 additions & 1 deletion DCCInspector-EX.ino
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,25 @@ void setup() {
" **"));
}

// Start OLED display (if required).
#if defined(USE_OLED)
// Start OLED display (if required).
OledDisplay.begin(SDA_OLED, SCL_OLED);
#if defined(ARDUINO_HELTEC_WIFI_KIT_32)
// Read battery voltage from pin GPIO37
// The battery measurement is enabled via pin GPIO21
digitalWrite(21, 0);
analogSetWidth(12); // 12 bits = 0-4095
analogSetPinAttenuation(37, ADC_11db);
adcAttachPin(37);

uint32_t batValue = analogRead(37);
// An input value of around 2600 is obtained for a
// a measured battery voltage of 4100mV.
uint16_t batMV = batValue * 41 / 26;
if (batMV < 3400) OledDisplay.append("Battery Low");
digitalWrite(21, 1); // Disable battery monitor

#endif
OledDisplay.append("Initialising..");
#endif

Expand Down Expand Up @@ -325,6 +341,13 @@ void loop() {
#endif
Serial.println(F("*** Inactivity detected -- going to sleep ***"));
delay(5000);
#if defined(ARDUINO_HELTEC_WIFI_KIT_32)
// Turn off WiFi
WiFi.disconnect(true);
// Turn off Vext power to screen on Heltec Kit 32 V2
pinMode(21, OUTPUT);
digitalWrite(21, 1);
#endif
esp_deep_sleep_start();
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion EventTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
*/

#if defined(ARDUINO_UNO_NANO) || defined(ARDUINO_AVR_MEGA)
#if defined(ARDUINO_UNO_NANO) || defined(ARDUINO_AVR_MEGA2560)
#include "EventTimer_AtMega.h"
#elif defined(ESP32)
#include "EventTimer_ESP32.h"
Expand Down
40 changes: 17 additions & 23 deletions EventTimer_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*/
typedef bool EventHandler(unsigned long eventInterval);

static void IRAM_ATTR isr_handler(void *);
static bool IRAM_ATTR captureCallbackFunction(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_channel, const cap_event_data_t *edata, void *user_data);

class EventTimerClass {
public:
Expand All @@ -60,13 +60,19 @@ class EventTimerClass {
// is scheduled. The interrupt response code is responsible for retrieving the captured value.
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, INPUTPIN);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_1, INPUTPIN);
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_POS_EDGE, 0);
//capture signal on positive edge, prescale = 0 i.e. 800,000,000 counts is equal to one second
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP1, MCPWM_NEG_EDGE, 0);
//capture signal on negative edge, prescale = 0 i.e. 800,000,000 counts is equal to one second
mcpwm_capture_config_t cap_conf;
cap_conf.capture_cb = captureCallbackFunction;
cap_conf.user_data = NULL;
cap_conf.cap_prescale = 1; //no prescale, i.e. 800,000,000 counts equals one second.
cap_conf.cap_edge = MCPWM_POS_EDGE;
mcpwm_capture_enable_channel(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, &cap_conf);
//capture signal on positive edge
cap_conf.cap_edge = MCPWM_NEG_EDGE;
mcpwm_capture_enable_channel(MCPWM_UNIT_0, MCPWM_SELECT_CAP1, &cap_conf);
//capture signal on negative edge
MCPWM0.int_ena.cap0_int_ena = 1; // Enable interrupt on CAP0 signal
MCPWM0.int_ena.cap1_int_ena = 1; // Enable interrupt on CAP1 signal
mcpwm_isr_register(MCPWM_UNIT_0, isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL); // Set ISR Handler
//mcpwm_isr_register(MCPWM_UNIT_0, isr_handler, NULL, ESP_INTR_FLAG_IRAM, NULL); // Set ISR Handler
return true;
};

Expand All @@ -84,24 +90,11 @@ class EventTimerClass {
// and to invoke the user program's handler. The user's handler is passed the
// number of ticks elapsed since the last valid interrupt. It returns true/false to
// indicate if this interrupt is deemed to be 'valid' or not.
void IRAM_ATTR processInterrupt() {
void IRAM_ATTR processInterrupt(mcpwm_capture_channel_id_t cap_channel) {
// Get current micros() value to support elapsedTicksSinceLastEvent().
thisEventMicros = micros();

// Check which interrupt is pending.
mcpwm_capture_signal_t capture = MCPWM_SELECT_CAP0;
if (MCPWM0.int_st.cap0_int_st) {
capture = MCPWM_SELECT_CAP0;
MCPWM0.int_clr.cap0_int_clr = 1; // Clear capture 0 interrupt
} else if (MCPWM0.int_st.cap1_int_st) {
capture = MCPWM_SELECT_CAP1;
MCPWM0.int_clr.cap1_int_clr = 1; // Clear capture 1 interrupt
} else
return; // No interrupt pending.

//unsigned long edge = mcpwm_capture_signal_get_edge(MCPWM_UNIT_0, capture); // 1=positive edge, 2=negative edge

unsigned long thisEventTicks = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, capture); //get capture signal counter value
unsigned long thisEventTicks = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, cap_channel); //get capture signal counter value
unsigned long eventInterval = thisEventTicks - lastValidEventTicks;
bool accepted = callUserHandler(eventInterval);
if (accepted) {
Expand All @@ -128,8 +121,9 @@ class EventTimerClass {

EventTimerClass EventTimer;

static void IRAM_ATTR isr_handler(void *) {
EventTimer.processInterrupt();
static bool IRAM_ATTR captureCallbackFunction(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_channel, const cap_event_data_t *edata, void *user_data) {
EventTimer.processInterrupt(cap_channel);
return false;
}

#endif
15 changes: 13 additions & 2 deletions HttpManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ WebServer HttpManagerClass::server;
// It sends a static web page that includes an updating IFRAME where the dynamic data
// will be displayed.
void HttpManagerClass::handleRoot() {
#if defined(LED_BUILTIN)
digitalWrite(LED_BUILTIN, 1);
#endif
processArguments();
int refreshTime = DCCStatistics.getRefreshTime();
String temp =
Expand Down Expand Up @@ -106,19 +108,27 @@ void HttpManagerClass::handleRoot() {
server.send(200, "text/html", temp);
temp = ""; // release space held

#if defined(LED_BUILTIN)
digitalWrite(LED_BUILTIN, 0);
#endif
}

// Function to handle the request for dynamic data (http://server/data).
void HttpManagerClass::handleData() {
#if defined(LED_BUILTIN)
digitalWrite(LED_BUILTIN, 1);
#endif
server.send(200, "text/html", HttpManager.getHtmlString());
#if defined(LED_BUILTIN)
digitalWrite(LED_BUILTIN, 0);
#endif
}

// Function to handle any other requests. Returns "404 Not Found".
void HttpManagerClass::handleNotFound() {
#if defined(LED_BUILTIN)
digitalWrite(LED_BUILTIN, 1);
#endif
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
Expand All @@ -133,7 +143,9 @@ void HttpManagerClass::handleNotFound() {
}

server.send(404, "text/plain", message);
#if defined(LED_BUILTIN)
digitalWrite(LED_BUILTIN, 0);
#endif
}

void HttpManagerClass::processArguments() {
Expand All @@ -146,7 +158,7 @@ void HttpManagerClass::processArguments() {
}

#if defined(ESP32)
void HttpManagerClass::WiFiEvent (WiFiEvent_t event, system_event_info_t info) {
void HttpManagerClass::WiFiEvent (arduino_event_id_t event) {
switch (event) {
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
Serial.print(F("[WPS Successful]"));
Expand Down Expand Up @@ -184,7 +196,6 @@ bool HttpManagerClass::begin(const char *ssid, const char *password, const char
#if defined(ESP32)
esp_wps_config_t config;
memset(&config, 0, sizeof(config));
config.crypto_funcs = &g_wifi_default_wps_crypto_funcs;
config.wps_type = WPS_TYPE_PBC;

WiFi.onEvent (WiFiEvent);
Expand Down
2 changes: 1 addition & 1 deletion HttpManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class HttpManagerClass {
static void handleNotFound();
static void handleData();
#if defined(ESP32)
static void WiFiEvent (WiFiEvent_t event, system_event_info_t info);
static void WiFiEvent (arduino_event_id_t event);
#endif
// Flag whether connected or not.
bool connected = false;
Expand Down
4 changes: 2 additions & 2 deletions OledDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
* is used for scrolling the display.
*/

#include "OledDisplay.h"

#include "Config.h"

#ifdef USE_OLED

#include "OledDisplay.h"

// Function called to initialise the object instance.
// Connects to the OLED display and puts it into a
// suitable mode.
Expand Down
4 changes: 2 additions & 2 deletions OledDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#ifndef oleddisplay_h
#define oleddisplay_h

#include <Adafruit_SSD1306.h>

#include "Config.h"

// Only compile if USE_OLED is defined.
Expand All @@ -45,8 +47,6 @@
#include "DCCStatistics.h"
#include "StringBuilder.h"

#include <Adafruit_SSD1306.h>

class OledDisplayClass {
public:
// Function called to initialise the object instance.
Expand Down
14 changes: 11 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

[platformio]
default_envs =
esp32
esp32_heltec
esp32_generic
esp8266
nano
nanoNew
Expand All @@ -26,9 +27,16 @@ lib_deps =
adafruit/Adafruit SSD1306
build_flags = -Wall -Wextra

[env:esp32]
[env:esp32_heltec]
platform = espressif32
board = heltec_wifi_kit_32
board = heltec_wifi_kit_32_v2
framework = arduino
lib_deps =
${env.lib_deps}

[env:esp32_generic]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps =
${env.lib_deps}
Expand Down

0 comments on commit 3d90b61

Please sign in to comment.