Skip to content

Commit c1c9848

Browse files
authored
Merge pull request #1596 from jbrazio/2026/t114-sensors
Refactor Heltec T114 sensor management
2 parents 6502067 + 1847333 commit c1c9848

5 files changed

Lines changed: 52 additions & 125 deletions

File tree

examples/simple_repeater/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ void setup() {
2929

3030
board.begin();
3131

32+
#if defined(MESH_DEBUG) && defined(NRF52_PLATFORM)
33+
// give some extra time for serial to settle so
34+
// boot debug messages can be seen on terminal
35+
delay(5000);
36+
#endif
37+
3238
// For power saving
3339
lastActive = millis(); // mark last active time since boot
3440

@@ -42,6 +48,7 @@ void setup() {
4248
#endif
4349

4450
if (!radio_init()) {
51+
MESH_DEBUG_PRINTLN("Radio init failed!");
4552
halt();
4653
}
4754

variants/heltec_t114/platformio.ini

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extends = nrf52_base
66
board = heltec_t114
77
board_build.ldscript = boards/nrf52840_s140_v6.ld
88
build_flags = ${nrf52_base.build_flags}
9+
${sensor_base.build_flags}
910
-I lib/nrf52/s140_nrf52_6.1.1_API/include
1011
-I lib/nrf52/s140_nrf52_6.1.1_API/include/nrf52
1112
-I variants/heltec_t114
@@ -28,20 +29,20 @@ build_flags = ${nrf52_base.build_flags}
2829
-D SX126X_DIO3_TCXO_VOLTAGE=1.8
2930
-D SX126X_CURRENT_LIMIT=140
3031
-D SX126X_RX_BOOSTED_GAIN=1
31-
-D DISPLAY_CLASS=NullDisplayDriver
32-
-D ST7789
3332
-D PIN_GPS_RX=39
3433
-D PIN_GPS_TX=37
3534
-D PIN_GPS_EN=21
3635
-D PIN_GPS_RESET=38
3736
-D PIN_GPS_RESET_ACTIVE=LOW
37+
-D ENV_PIN_SDA=PIN_WIRE1_SDA
38+
-D ENV_PIN_SCL=PIN_WIRE1_SCL
3839
build_src_filter = ${nrf52_base.build_src_filter}
3940
+<helpers/*.cpp>
41+
+<helpers/sensors>
4042
+<../variants/heltec_t114>
4143
lib_deps =
4244
${nrf52_base.lib_deps}
43-
stevemarple/MicroNMEA @ ^2.0.6
44-
adafruit/Adafruit GFX Library @ ^1.12.1
45+
${sensor_base.lib_deps}
4546
debug_tool = jlink
4647
upload_protocol = nrfutil
4748

@@ -100,6 +101,7 @@ board_upload.maximum_size = 712704
100101
build_flags =
101102
${Heltec_t114.build_flags}
102103
-I examples/companion_radio/ui-new
104+
-D DISPLAY_CLASS=NullDisplayDriver
103105
-D MAX_CONTACTS=350
104106
-D MAX_GROUP_CHANNELS=40
105107
-D BLE_PIN_CODE=123456
@@ -122,6 +124,7 @@ board_upload.maximum_size = 712704
122124
build_flags =
123125
${Heltec_t114.build_flags}
124126
-I examples/companion_radio/ui-new
127+
-D DISPLAY_CLASS=NullDisplayDriver
125128
-D MAX_CONTACTS=350
126129
-D MAX_GROUP_CHANNELS=40
127130
; -D BLE_PIN_CODE=123456
@@ -144,6 +147,7 @@ extends = Heltec_t114
144147
board = heltec_t114
145148
board_build.ldscript = boards/nrf52840_s140_v6.ld
146149
build_flags = ${Heltec_t114.build_flags}
150+
-D ST7789
147151
-D HELTEC_T114_WITH_DISPLAY
148152
-D DISPLAY_CLASS=ST7789Display
149153
build_src_filter = ${Heltec_t114.build_src_filter}
@@ -153,6 +157,7 @@ build_src_filter = ${Heltec_t114.build_src_filter}
153157
+<helpers/ui/OLEDDisplayFonts.cpp>
154158
lib_deps =
155159
${Heltec_t114.lib_deps}
160+
adafruit/Adafruit SSD1306 @ ^2.5.13
156161
debug_tool = jlink
157162
upload_protocol = nrfutil
158163

variants/heltec_t114/target.cpp

Lines changed: 24 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
1-
#include <Arduino.h>
21
#include "target.h"
2+
3+
#include <Arduino.h>
34
#include <helpers/ArduinoHelpers.h>
5+
6+
#ifdef ENV_INCLUDE_GPS
47
#include <helpers/sensors/MicroNMEALocationProvider.h>
8+
#endif
59

610
T114Board board;
711

12+
#if defined(P_LORA_SCLK)
813
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI);
14+
#else
15+
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY);
16+
#endif
917

1018
WRAPPER_CLASS radio_driver(radio, board);
1119

1220
VolatileRTCClock fallback_clock;
1321
AutoDiscoverRTCClock rtc_clock(fallback_clock);
14-
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock);
15-
T114SensorManager sensors = T114SensorManager(nmea);
22+
23+
#if ENV_INCLUDE_GPS
24+
#include <helpers/sensors/MicroNMEALocationProvider.h>
25+
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
26+
EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea);
27+
#else
28+
EnvironmentSensorManager sensors;
29+
#endif
1630

1731
#ifdef DISPLAY_CLASS
18-
DISPLAY_CLASS display;
19-
MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
32+
DISPLAY_CLASS display;
33+
MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
2034
#endif
2135

2236
bool radio_init() {
2337
rtc_clock.begin(Wire);
2438

39+
#if defined(P_LORA_SCLK)
2540
return radio.std_init(&SPI);
41+
#else
42+
return radio.std_init();
43+
#endif
2644
}
2745

2846
uint32_t radio_get_rng_seed() {
@@ -42,92 +60,5 @@ void radio_set_tx_power(uint8_t dbm) {
4260

4361
mesh::LocalIdentity radio_new_identity() {
4462
RadioNoiseListener rng(radio);
45-
return mesh::LocalIdentity(&rng); // create new random identity
46-
}
47-
48-
void T114SensorManager::start_gps() {
49-
if (!gps_active) {
50-
gps_active = true;
51-
_location->begin();
52-
}
53-
}
54-
55-
void T114SensorManager::stop_gps() {
56-
if (gps_active) {
57-
gps_active = false;
58-
_location->stop();
59-
}
60-
}
61-
62-
bool T114SensorManager::begin() {
63-
Serial1.begin(9600);
64-
65-
// Try to detect if GPS is physically connected to determine if we should expose the setting
66-
pinMode(GPS_EN, OUTPUT);
67-
digitalWrite(GPS_EN, HIGH); // Power on GPS
68-
69-
// Give GPS a moment to power up and send data
70-
delay(1500);
71-
72-
// We'll consider GPS detected if we see any data on Serial1
73-
gps_detected = (Serial1.available() > 0);
74-
75-
if (gps_detected) {
76-
MESH_DEBUG_PRINTLN("GPS detected");
77-
} else {
78-
MESH_DEBUG_PRINTLN("No GPS detected");
79-
}
80-
digitalWrite(GPS_EN, LOW); // Power off GPS until the setting is changed
81-
82-
return true;
83-
}
84-
85-
bool T114SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
86-
if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
87-
telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
88-
}
89-
return true;
90-
}
91-
92-
void T114SensorManager::loop() {
93-
static long next_gps_update = 0;
94-
95-
_location->loop();
96-
97-
if (millis() > next_gps_update) {
98-
if (_location->isValid()) {
99-
node_lat = ((double)_location->getLatitude())/1000000.;
100-
node_lon = ((double)_location->getLongitude())/1000000.;
101-
node_altitude = ((double)_location->getAltitude()) / 1000.0;
102-
MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
103-
}
104-
next_gps_update = millis() + 1000;
105-
}
106-
}
107-
108-
int T114SensorManager::getNumSettings() const {
109-
return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
110-
}
111-
112-
const char* T114SensorManager::getSettingName(int i) const {
113-
return (gps_detected && i == 0) ? "gps" : NULL;
114-
}
115-
116-
const char* T114SensorManager::getSettingValue(int i) const {
117-
if (gps_detected && i == 0) {
118-
return gps_active ? "1" : "0";
119-
}
120-
return NULL;
121-
}
122-
123-
bool T114SensorManager::setSettingValue(const char* name, const char* value) {
124-
if (gps_detected && strcmp(name, "gps") == 0) {
125-
if (strcmp(value, "0") == 0) {
126-
stop_gps();
127-
} else {
128-
start_gps();
129-
}
130-
return true;
131-
}
132-
return false; // not supported
63+
return mesh::LocalIdentity(&rng); // create new random identity
13364
}

variants/heltec_t114/target.h

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
#define RADIOLIB_STATIC_ONLY 1
44
#include <RadioLib.h>
5-
#include <helpers/radiolib/RadioLibWrappers.h>
65
#include <T114Board.h>
7-
#include <helpers/radiolib/CustomSX1262Wrapper.h>
86
#include <helpers/AutoDiscoverRTCClock.h>
9-
#include <helpers/SensorManager.h>
7+
#include <helpers/radiolib/CustomSX1262Wrapper.h>
8+
#include <helpers/radiolib/RadioLibWrappers.h>
9+
#include <helpers/sensors/EnvironmentSensorManager.h>
1010
#include <helpers/sensors/LocationProvider.h>
1111

1212
#ifdef DISPLAY_CLASS
@@ -18,33 +18,14 @@
1818
#endif
1919
#endif
2020

21-
class T114SensorManager : public SensorManager {
22-
bool gps_active = false;
23-
bool gps_detected = false;
24-
LocationProvider* _location;
25-
26-
void start_gps();
27-
void stop_gps();
28-
public:
29-
T114SensorManager(LocationProvider &location): _location(&location) { }
30-
bool begin() override;
31-
bool querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) override;
32-
void loop() override;
33-
LocationProvider* getLocationProvider() override { return gps_detected ? _location : NULL; }
34-
int getNumSettings() const override;
35-
const char* getSettingName(int i) const override;
36-
const char* getSettingValue(int i) const override;
37-
bool setSettingValue(const char* name, const char* value) override;
38-
};
39-
4021
extern T114Board board;
4122
extern WRAPPER_CLASS radio_driver;
4223
extern AutoDiscoverRTCClock rtc_clock;
43-
extern T114SensorManager sensors;
24+
extern EnvironmentSensorManager sensors;
4425

4526
#ifdef DISPLAY_CLASS
46-
extern DISPLAY_CLASS display;
47-
extern MomentaryButton user_btn;
27+
extern DISPLAY_CLASS display;
28+
extern MomentaryButton user_btn;
4829
#endif
4930

5031
bool radio_init();

variants/heltec_t114/variant.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#define USE_LFXO // 32.768 kHz crystal oscillator
1515
#define VARIANT_MCK (64000000ul)
1616

17-
#define WIRE_INTERFACES_COUNT (1)
17+
#define WIRE_INTERFACES_COUNT (2)
1818

1919
////////////////////////////////////////////////////////////////////////////////
2020
// Power
@@ -58,8 +58,11 @@
5858
////////////////////////////////////////////////////////////////////////////////
5959
// I2C pin definition
6060

61-
#define PIN_WIRE_SDA (26) // P0.26
62-
#define PIN_WIRE_SCL (27) // P0.27
61+
#define PIN_WIRE_SDA (26) // P0.26
62+
#define PIN_WIRE_SCL (27) // P0.27
63+
64+
#define PIN_WIRE1_SDA (7) // P0.8
65+
#define PIN_WIRE1_SCL (8) // P0.7
6366

6467
////////////////////////////////////////////////////////////////////////////////
6568
// SPI pin definition

0 commit comments

Comments
 (0)