mirror of https://github.com/meshcore-dev/MeshCore
122 changed files with 4575 additions and 1059 deletions
@ -0,0 +1,59 @@ |
|||||
|
{ |
||||
|
"build": { |
||||
|
"arduino": { |
||||
|
"ldscript": "nrf52840_s140_v7.ld" |
||||
|
}, |
||||
|
"core": "nRF5", |
||||
|
"cpu": "cortex-m4", |
||||
|
"extra_flags": "-DARDUINO_WIO_WM1110 -DNRF52840_XXAA", |
||||
|
"f_cpu": "64000000L", |
||||
|
"hwids": [ |
||||
|
["0x239A", "0x8029"], |
||||
|
["0x239A", "0x0029"], |
||||
|
["0x239A", "0x002A"], |
||||
|
["0x239A", "0x802A"] |
||||
|
], |
||||
|
"usb_product": "me25ls01-BOOT", |
||||
|
"mcu": "nrf52840", |
||||
|
"variant": "minewsemi_me25ls01", |
||||
|
"bsp": { |
||||
|
"name": "adafruit" |
||||
|
}, |
||||
|
"softdevice": { |
||||
|
"sd_flags": "-DS140", |
||||
|
"sd_name": "s140", |
||||
|
"sd_version": "7.3.0", |
||||
|
"sd_fwid": "0x0123" |
||||
|
}, |
||||
|
"bootloader": { |
||||
|
"settings_addr": "0xFF000" |
||||
|
} |
||||
|
}, |
||||
|
"connectivity": ["bluetooth"], |
||||
|
"debug": { |
||||
|
"jlink_device": "nRF52840_xxAA", |
||||
|
"svd_path": "nrf52840.svd", |
||||
|
"openocd_target": "nrf52.cfg" |
||||
|
}, |
||||
|
"frameworks": ["arduino"], |
||||
|
"name": "Minewsemi ME25LS01", |
||||
|
"upload": { |
||||
|
"maximum_ram_size": 248832, |
||||
|
"maximum_size": 815104, |
||||
|
"speed": 115200, |
||||
|
"protocol": "nrfutil", |
||||
|
"protocols": [ |
||||
|
"jlink", |
||||
|
"nrfjprog", |
||||
|
"nrfutil", |
||||
|
"stlink", |
||||
|
"cmsis-dap", |
||||
|
"blackmagic" |
||||
|
], |
||||
|
"use_1200bps_touch": true, |
||||
|
"require_upload_port": true, |
||||
|
"wait_for_upload_port": true |
||||
|
}, |
||||
|
"url": "https://en.minewsemi.com/lora-module/lr1110-nrf52840-me25LS01", |
||||
|
"vendor": "MINEWSEMI" |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
{ |
||||
|
"build": { |
||||
|
"arduino": { |
||||
|
"ldscript": "nrf52840_s140_v7.ld" |
||||
|
}, |
||||
|
"core": "nRF5", |
||||
|
"cpu": "cortex-m4", |
||||
|
"extra_flags": "-DARDUINO_SEEED_WIO_TRACKER_L1 -DNRF52840_XXAA -DSEEED_WIO_TRACKER_L1 ", |
||||
|
"f_cpu": "64000000L", |
||||
|
"hwids": [ |
||||
|
[ "0x2886", "0x1667" ], |
||||
|
[ "0x2886", "0x1668" ] |
||||
|
], |
||||
|
"mcu": "nrf52840", |
||||
|
"variant": "Seeed_Wio_Tracker_L1", |
||||
|
"softdevice": { |
||||
|
"sd_flags": "-DS140", |
||||
|
"sd_name": "s140", |
||||
|
"sd_version": "7.3.0", |
||||
|
"sd_fwid": "0x0123" |
||||
|
}, |
||||
|
"bsp": { |
||||
|
"name": "adafruit" |
||||
|
}, |
||||
|
"bootloader": { |
||||
|
"settings_addr": "0xFF000" |
||||
|
}, |
||||
|
"usb_product": "Seeed Wio Tracker L1" |
||||
|
}, |
||||
|
"connectivity": [ |
||||
|
"bluetooth" |
||||
|
], |
||||
|
"debug": { |
||||
|
"jlink_device": "nRF52840_xxAA", |
||||
|
"openocd_target": "nrf52.cfg", |
||||
|
"svd_path": "nrf52840.svd" |
||||
|
}, |
||||
|
"frameworks": [ |
||||
|
"arduino" |
||||
|
], |
||||
|
"name": "Seeed Wio Tracker L1", |
||||
|
"upload": { |
||||
|
"maximum_ram_size": 248832, |
||||
|
"maximum_size": 815104, |
||||
|
"protocol": "nrfutil", |
||||
|
"speed": 115200, |
||||
|
"protocols": [ |
||||
|
"jlink", |
||||
|
"nrfjprog", |
||||
|
"nrfutil", |
||||
|
"cmsis-dap", |
||||
|
"sam-ba", |
||||
|
"blackmagic" |
||||
|
], |
||||
|
"use_1200bps_touch": true, |
||||
|
"require_upload_port": true, |
||||
|
"wait_for_upload_port": true |
||||
|
}, |
||||
|
"url": "https://wiki.seeedstudio.com/wio_tracker_l1_node/", |
||||
|
"vendor": "Seeed Studio" |
||||
|
} |
||||
@ -0,0 +1,60 @@ |
|||||
|
{ |
||||
|
"build": { |
||||
|
"arduino": { |
||||
|
"ldscript": "nrf52840_s140_v7.ld" |
||||
|
}, |
||||
|
"core": "nRF5", |
||||
|
"cpu": "cortex-m4", |
||||
|
"extra_flags": "-DARDUINO_Seeed_XIAO_nRF52840 -DNRF52840_XXAA -DSEEED_XIAO_NRF52840 ", |
||||
|
"f_cpu": "64000000L", |
||||
|
"hwids": [ |
||||
|
[ "0x2886", "0x0059" ] |
||||
|
], |
||||
|
"mcu": "nrf52840", |
||||
|
"variant": "Seeed_XIAO_nRF52840", |
||||
|
"softdevice": { |
||||
|
"sd_flags": "-DS140", |
||||
|
"sd_name": "s140", |
||||
|
"sd_version": "7.3.0", |
||||
|
"sd_fwid": "0x0123" |
||||
|
}, |
||||
|
"bsp": { |
||||
|
"name": "adafruit" |
||||
|
}, |
||||
|
"bootloader": { |
||||
|
"settings_addr": "0xFF000" |
||||
|
}, |
||||
|
"usb_product": "XIAO nRF52840" |
||||
|
}, |
||||
|
"connectivity": [ |
||||
|
"bluetooth" |
||||
|
], |
||||
|
"debug": { |
||||
|
"jlink_device": "nRF52840_xxAA", |
||||
|
"openocd_target": "nrf52.cfg", |
||||
|
"svd_path": "nrf52840.svd" |
||||
|
}, |
||||
|
"frameworks": [ |
||||
|
"arduino" |
||||
|
], |
||||
|
"name": "Seeed Studio XIAO nRF52840", |
||||
|
"upload": { |
||||
|
"maximum_ram_size": 248832, |
||||
|
"maximum_size": 815104, |
||||
|
"protocol": "nrfutil", |
||||
|
"speed": 115200, |
||||
|
"protocols": [ |
||||
|
"jlink", |
||||
|
"nrfjprog", |
||||
|
"nrfutil", |
||||
|
"cmsis-dap", |
||||
|
"sam-ba", |
||||
|
"blackmagic" |
||||
|
], |
||||
|
"use_1200bps_touch": true, |
||||
|
"require_upload_port": true, |
||||
|
"wait_for_upload_port": true |
||||
|
}, |
||||
|
"url": "https://wiki.seeedstudio.com/meshtastic_solar_node/", |
||||
|
"vendor": "Seeed Studio" |
||||
|
} |
||||
@ -0,0 +1,926 @@ |
|||||
|
#include "SensorMesh.h" |
||||
|
|
||||
|
/* ------------------------------ Config -------------------------------- */ |
||||
|
|
||||
|
#ifndef LORA_FREQ |
||||
|
#define LORA_FREQ 915.0 |
||||
|
#endif |
||||
|
#ifndef LORA_BW |
||||
|
#define LORA_BW 250 |
||||
|
#endif |
||||
|
#ifndef LORA_SF |
||||
|
#define LORA_SF 10 |
||||
|
#endif |
||||
|
#ifndef LORA_CR |
||||
|
#define LORA_CR 5 |
||||
|
#endif |
||||
|
#ifndef LORA_TX_POWER |
||||
|
#define LORA_TX_POWER 20 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ADVERT_NAME |
||||
|
#define ADVERT_NAME "sensor" |
||||
|
#endif |
||||
|
#ifndef ADVERT_LAT |
||||
|
#define ADVERT_LAT 0.0 |
||||
|
#endif |
||||
|
#ifndef ADVERT_LON |
||||
|
#define ADVERT_LON 0.0 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef ADMIN_PASSWORD |
||||
|
#define ADMIN_PASSWORD "password" |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SERVER_RESPONSE_DELAY |
||||
|
#define SERVER_RESPONSE_DELAY 300 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef TXT_ACK_DELAY |
||||
|
#define TXT_ACK_DELAY 200 |
||||
|
#endif |
||||
|
|
||||
|
#ifndef SENSOR_READ_INTERVAL_SECS |
||||
|
#define SENSOR_READ_INTERVAL_SECS 60 |
||||
|
#endif |
||||
|
|
||||
|
/* ------------------------------ Code -------------------------------- */ |
||||
|
|
||||
|
#define REQ_TYPE_LOGIN 0x00 |
||||
|
#define REQ_TYPE_GET_STATUS 0x01 |
||||
|
#define REQ_TYPE_KEEP_ALIVE 0x02 |
||||
|
#define REQ_TYPE_GET_TELEMETRY_DATA 0x03 |
||||
|
#define REQ_TYPE_GET_AVG_MIN_MAX 0x04 |
||||
|
#define REQ_TYPE_GET_ACCESS_LIST 0x05 |
||||
|
|
||||
|
#define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
|
||||
|
|
||||
|
#define CLI_REPLY_DELAY_MILLIS 1000 |
||||
|
|
||||
|
#define LAZY_CONTACTS_WRITE_DELAY 5000 |
||||
|
|
||||
|
#define ALERT_ACK_EXPIRY_MILLIS 8000 // wait 8 secs for ACKs to alert messages
|
||||
|
|
||||
|
static File openAppend(FILESYSTEM* _fs, const char* fname) { |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
return _fs->open(fname, FILE_O_WRITE); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return _fs->open(fname, "a"); |
||||
|
#else |
||||
|
return _fs->open(fname, "a", true); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
static File openWrite(FILESYSTEM* _fs, const char* filename) { |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
_fs->remove(filename); |
||||
|
return _fs->open(filename, FILE_O_WRITE); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return _fs->open(filename, "w"); |
||||
|
#else |
||||
|
return _fs->open(filename, "w", true); |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::loadContacts() { |
||||
|
num_contacts = 0; |
||||
|
if (_fs->exists("/s_contacts")) { |
||||
|
#if defined(RP2040_PLATFORM) |
||||
|
File file = _fs->open("/s_contacts", "r"); |
||||
|
#else |
||||
|
File file = _fs->open("/s_contacts"); |
||||
|
#endif |
||||
|
if (file) { |
||||
|
bool full = false; |
||||
|
while (!full) { |
||||
|
ContactInfo c; |
||||
|
uint8_t pub_key[32]; |
||||
|
uint8_t unused[6]; |
||||
|
|
||||
|
bool success = (file.read(pub_key, 32) == 32); |
||||
|
success = success && (file.read((uint8_t *) &c.permissions, 1) == 1); |
||||
|
success = success && (file.read(unused, 6) == 6); |
||||
|
success = success && (file.read((uint8_t *)&c.out_path_len, 1) == 1); |
||||
|
success = success && (file.read(c.out_path, 64) == 64); |
||||
|
success = success && (file.read(c.shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE); |
||||
|
c.last_timestamp = 0; // transient
|
||||
|
c.last_activity = 0; |
||||
|
|
||||
|
if (!success) break; // EOF
|
||||
|
|
||||
|
c.id = mesh::Identity(pub_key); |
||||
|
if (num_contacts < MAX_CONTACTS) { |
||||
|
contacts[num_contacts++] = c; |
||||
|
} else { |
||||
|
full = true; |
||||
|
} |
||||
|
} |
||||
|
file.close(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::saveContacts() { |
||||
|
File file = openWrite(_fs, "/s_contacts"); |
||||
|
if (file) { |
||||
|
uint8_t unused[5]; |
||||
|
memset(unused, 0, sizeof(unused)); |
||||
|
|
||||
|
for (int i = 0; i < num_contacts; i++) { |
||||
|
auto c = &contacts[i]; |
||||
|
if (c->permissions == 0) continue; // skip deleted entries
|
||||
|
|
||||
|
bool success = (file.write(c->id.pub_key, 32) == 32); |
||||
|
success = success && (file.write((uint8_t *) &c->permissions, 1) == 1); |
||||
|
success = success && (file.write(unused, 6) == 6); |
||||
|
success = success && (file.write((uint8_t *)&c->out_path_len, 1) == 1); |
||||
|
success = success && (file.write(c->out_path, 64) == 64); |
||||
|
success = success && (file.write(c->shared_secret, PUB_KEY_SIZE) == PUB_KEY_SIZE); |
||||
|
|
||||
|
if (!success) break; // write failed
|
||||
|
} |
||||
|
file.close(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
static uint8_t getDataSize(uint8_t type) { |
||||
|
switch (type) { |
||||
|
case LPP_GPS: |
||||
|
return 9; |
||||
|
case LPP_POLYLINE: |
||||
|
return 8; // TODO: this is MINIMIUM
|
||||
|
case LPP_GYROMETER: |
||||
|
case LPP_ACCELEROMETER: |
||||
|
return 6; |
||||
|
case LPP_GENERIC_SENSOR: |
||||
|
case LPP_FREQUENCY: |
||||
|
case LPP_DISTANCE: |
||||
|
case LPP_ENERGY: |
||||
|
case LPP_UNIXTIME: |
||||
|
return 4; |
||||
|
case LPP_COLOUR: |
||||
|
return 3; |
||||
|
case LPP_ANALOG_INPUT: |
||||
|
case LPP_ANALOG_OUTPUT: |
||||
|
case LPP_LUMINOSITY: |
||||
|
case LPP_TEMPERATURE: |
||||
|
case LPP_CONCENTRATION: |
||||
|
case LPP_BAROMETRIC_PRESSURE: |
||||
|
case LPP_RELATIVE_HUMIDITY: |
||||
|
case LPP_ALTITUDE: |
||||
|
case LPP_VOLTAGE: |
||||
|
case LPP_CURRENT: |
||||
|
case LPP_DIRECTION: |
||||
|
case LPP_POWER: |
||||
|
return 2; |
||||
|
} |
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
static uint32_t getMultiplier(uint8_t type) { |
||||
|
switch (type) { |
||||
|
case LPP_CURRENT: |
||||
|
case LPP_DISTANCE: |
||||
|
case LPP_ENERGY: |
||||
|
return 1000; |
||||
|
case LPP_VOLTAGE: |
||||
|
case LPP_ANALOG_INPUT: |
||||
|
case LPP_ANALOG_OUTPUT: |
||||
|
return 100; |
||||
|
case LPP_TEMPERATURE: |
||||
|
case LPP_BAROMETRIC_PRESSURE: |
||||
|
case LPP_RELATIVE_HUMIDITY: |
||||
|
return 10; |
||||
|
} |
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
static bool isSigned(uint8_t type) { |
||||
|
return type == LPP_ALTITUDE || type == LPP_TEMPERATURE || type == LPP_GYROMETER || |
||||
|
type == LPP_ANALOG_INPUT || type == LPP_ANALOG_OUTPUT || type == LPP_GPS || type == LPP_ACCELEROMETER; |
||||
|
} |
||||
|
|
||||
|
static float getFloat(const uint8_t * buffer, uint8_t size, uint32_t multiplier, bool is_signed) { |
||||
|
uint32_t value = 0; |
||||
|
for (uint8_t i = 0; i < size; i++) { |
||||
|
value = (value << 8) + buffer[i]; |
||||
|
} |
||||
|
|
||||
|
int sign = 1; |
||||
|
if (is_signed) { |
||||
|
uint32_t bit = 1ul << ((size * 8) - 1); |
||||
|
if ((value & bit) == bit) { |
||||
|
value = (bit << 1) - value; |
||||
|
sign = -1; |
||||
|
} |
||||
|
} |
||||
|
return sign * ((float) value / multiplier); |
||||
|
} |
||||
|
|
||||
|
static uint8_t putFloat(uint8_t * dest, float value, uint8_t size, uint32_t multiplier, bool is_signed) { |
||||
|
// check sign
|
||||
|
bool sign = value < 0; |
||||
|
if (sign) value = -value; |
||||
|
|
||||
|
// get value to store
|
||||
|
uint32_t v = value * multiplier; |
||||
|
|
||||
|
// format an uint32_t as if it was an int32_t
|
||||
|
if (is_signed & sign) { |
||||
|
uint32_t mask = (1 << (size * 8)) - 1; |
||||
|
v = v & mask; |
||||
|
if (sign) v = mask - v + 1; |
||||
|
} |
||||
|
|
||||
|
// add bytes (MSB first)
|
||||
|
for (uint8_t i=1; i<=size; i++) { |
||||
|
dest[size - i] = (v & 0xFF); |
||||
|
v >>= 8; |
||||
|
} |
||||
|
return size; |
||||
|
} |
||||
|
|
||||
|
uint8_t SensorMesh::handleRequest(uint8_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len) { |
||||
|
memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
|
||||
|
|
||||
|
if (req_type == REQ_TYPE_GET_TELEMETRY_DATA) { // allow all
|
||||
|
telemetry.reset(); |
||||
|
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f); |
||||
|
// query other sensors -- target specific
|
||||
|
sensors.querySensors(0xFF, telemetry); // allow all telemetry permissions for admin or guest
|
||||
|
// TODO: let requester know permissions they have: telemetry.addPresence(TELEM_CHANNEL_SELF, perms);
|
||||
|
|
||||
|
uint8_t tlen = telemetry.getSize(); |
||||
|
memcpy(&reply_data[4], telemetry.getBuffer(), tlen); |
||||
|
return 4 + tlen; // reply_len
|
||||
|
} |
||||
|
if (req_type == REQ_TYPE_GET_AVG_MIN_MAX && (perms & PERM_ACL_ROLE_MASK) >= PERM_ACL_READ_ONLY) { |
||||
|
uint32_t start_secs_ago, end_secs_ago; |
||||
|
memcpy(&start_secs_ago, &payload[0], 4); |
||||
|
memcpy(&end_secs_ago, &payload[4], 4); |
||||
|
uint8_t res1 = payload[8]; // reserved for future (extra query params)
|
||||
|
uint8_t res2 = payload[9]; |
||||
|
|
||||
|
MinMaxAvg data[8]; |
||||
|
int n; |
||||
|
if (res1 == 0 && res2 == 0) { |
||||
|
n = querySeriesData(start_secs_ago, end_secs_ago, data, 8); |
||||
|
} else { |
||||
|
n = 0; |
||||
|
} |
||||
|
|
||||
|
uint8_t ofs = 4; |
||||
|
{ |
||||
|
uint32_t now = getRTCClock()->getCurrentTime(); |
||||
|
memcpy(&reply_data[ofs], &now, 4); ofs += 4; |
||||
|
} |
||||
|
|
||||
|
for (int i = 0; i < n; i++) { |
||||
|
auto d = &data[i]; |
||||
|
reply_data[ofs++] = d->_channel; |
||||
|
reply_data[ofs++] = d->_lpp_type; |
||||
|
uint8_t sz = getDataSize(d->_lpp_type); |
||||
|
uint32_t mult = getMultiplier(d->_lpp_type); |
||||
|
bool is_signed = isSigned(d->_lpp_type); |
||||
|
ofs += putFloat(&reply_data[ofs], d->_min, sz, mult, is_signed); |
||||
|
ofs += putFloat(&reply_data[ofs], d->_max, sz, mult, is_signed); |
||||
|
ofs += putFloat(&reply_data[ofs], d->_avg, sz, mult, is_signed); |
||||
|
} |
||||
|
return ofs; |
||||
|
} |
||||
|
if (req_type == REQ_TYPE_GET_ACCESS_LIST && (perms & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN) { |
||||
|
uint8_t res1 = payload[0]; // reserved for future (extra query params)
|
||||
|
uint8_t res2 = payload[1]; |
||||
|
if (res1 == 0 && res2 == 0) { |
||||
|
uint8_t ofs = 4; |
||||
|
for (int i = 0; i < num_contacts && ofs + 7 <= sizeof(reply_data) - 4; i++) { |
||||
|
auto c = &contacts[i]; |
||||
|
if (c->permissions == 0) continue; // skip deleted entries
|
||||
|
memcpy(&reply_data[ofs], c->id.pub_key, 6); ofs += 6; // just 6-byte pub_key prefix
|
||||
|
reply_data[ofs++] = c->permissions; |
||||
|
} |
||||
|
return ofs; |
||||
|
} |
||||
|
} |
||||
|
return 0; // unknown command
|
||||
|
} |
||||
|
|
||||
|
mesh::Packet* SensorMesh::createSelfAdvert() { |
||||
|
uint8_t app_data[MAX_ADVERT_DATA_SIZE]; |
||||
|
uint8_t app_data_len; |
||||
|
{ |
||||
|
AdvertDataBuilder builder(ADV_TYPE_SENSOR, _prefs.node_name, _prefs.node_lat, _prefs.node_lon); |
||||
|
app_data_len = builder.encodeTo(app_data); |
||||
|
} |
||||
|
|
||||
|
return createAdvert(self_id, app_data, app_data_len); |
||||
|
} |
||||
|
|
||||
|
ContactInfo* SensorMesh::getContact(const uint8_t* pubkey, int key_len) { |
||||
|
for (int i = 0; i < num_contacts; i++) { |
||||
|
if (memcmp(pubkey, contacts[i].id.pub_key, key_len) == 0) return &contacts[i]; // already known
|
||||
|
} |
||||
|
return NULL; // not found
|
||||
|
} |
||||
|
|
||||
|
ContactInfo* SensorMesh::putContact(const mesh::Identity& id, uint8_t init_perms) { |
||||
|
uint32_t min_time = 0xFFFFFFFF; |
||||
|
ContactInfo* oldest = &contacts[MAX_CONTACTS - 1]; |
||||
|
for (int i = 0; i < num_contacts; i++) { |
||||
|
if (id.matches(contacts[i].id)) return &contacts[i]; // already known
|
||||
|
if (!contacts[i].isAdmin() && contacts[i].last_activity < min_time) { |
||||
|
oldest = &contacts[i]; |
||||
|
min_time = oldest->last_activity; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
ContactInfo* c; |
||||
|
if (num_contacts < MAX_CONTACTS) { |
||||
|
c = &contacts[num_contacts++]; |
||||
|
} else { |
||||
|
c = oldest; // evict least active contact
|
||||
|
} |
||||
|
memset(c, 0, sizeof(*c)); |
||||
|
c->permissions = init_perms; |
||||
|
c->id = id; |
||||
|
c->out_path_len = -1; // initially out_path is unknown
|
||||
|
return c; |
||||
|
} |
||||
|
|
||||
|
bool SensorMesh::applyContactPermissions(const uint8_t* pubkey, int key_len, uint8_t perms) { |
||||
|
ContactInfo* c; |
||||
|
if ((perms & PERM_ACL_ROLE_MASK) == PERM_ACL_GUEST) { // guest role is not persisted in contacts
|
||||
|
c = getContact(pubkey, key_len); |
||||
|
if (c == NULL) return false; // partial pubkey not found
|
||||
|
|
||||
|
num_contacts--; // delete from contacts[]
|
||||
|
int i = c - contacts; |
||||
|
while (i < num_contacts) { |
||||
|
contacts[i] = contacts[i + 1]; |
||||
|
i++; |
||||
|
} |
||||
|
} else { |
||||
|
if (key_len < PUB_KEY_SIZE) return false; // need complete pubkey when adding/modifying
|
||||
|
|
||||
|
mesh::Identity id(pubkey); |
||||
|
c = putContact(id, 0); |
||||
|
|
||||
|
c->permissions = perms; // update their permissions
|
||||
|
self_id.calcSharedSecret(c->shared_secret, pubkey); |
||||
|
} |
||||
|
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); // trigger saveContacts()
|
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::sendAlert(ContactInfo* c, Trigger* t) { |
||||
|
int text_len = strlen(t->text); |
||||
|
|
||||
|
uint8_t data[MAX_PACKET_PAYLOAD]; |
||||
|
memcpy(data, &t->timestamp, 4); |
||||
|
data[4] = (TXT_TYPE_PLAIN << 2) | t->attempt; // attempt and flags
|
||||
|
memcpy(&data[5], t->text, text_len); |
||||
|
|
||||
|
// calc expected ACK reply
|
||||
|
mesh::Utils::sha256((uint8_t *)&t->expected_acks[t->attempt], 4, data, 5 + text_len, self_id.pub_key, PUB_KEY_SIZE); |
||||
|
t->attempt++; |
||||
|
|
||||
|
auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, c->id, c->shared_secret, data, 5 + text_len); |
||||
|
if (pkt) { |
||||
|
if (c->out_path_len >= 0) { // we have an out_path, so send DIRECT
|
||||
|
sendDirect(pkt, c->out_path, c->out_path_len); |
||||
|
} else { |
||||
|
sendFlood(pkt); |
||||
|
} |
||||
|
} |
||||
|
t->send_expiry = futureMillis(ALERT_ACK_EXPIRY_MILLIS); |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::alertIf(bool condition, Trigger& t, AlertPriority pri, const char* text) { |
||||
|
if (condition) { |
||||
|
if (!t.isTriggered() && num_alert_tasks < MAX_CONCURRENT_ALERTS) { |
||||
|
StrHelper::strncpy(t.text, text, sizeof(t.text)); |
||||
|
t.pri = pri; |
||||
|
t.send_expiry = 0; // signal that initial send is needed
|
||||
|
t.attempt = 4; |
||||
|
t.curr_contact_idx = -1; // start iterating thru contacts[]
|
||||
|
|
||||
|
alert_tasks[num_alert_tasks++] = &t; // add to queue
|
||||
|
} |
||||
|
} else { |
||||
|
if (t.isTriggered()) { |
||||
|
t.text[0] = 0; |
||||
|
// remove 't' from alert queue
|
||||
|
int i = 0; |
||||
|
while (i < num_alert_tasks && alert_tasks[i] != &t) i++; |
||||
|
|
||||
|
if (i < num_alert_tasks) { // found, now delete from array
|
||||
|
num_alert_tasks--; |
||||
|
while (i < num_alert_tasks) { |
||||
|
alert_tasks[i] = alert_tasks[i + 1]; |
||||
|
i++; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
float SensorMesh::getAirtimeBudgetFactor() const { |
||||
|
return _prefs.airtime_factor; |
||||
|
} |
||||
|
|
||||
|
bool SensorMesh::allowPacketForward(const mesh::Packet* packet) { |
||||
|
if (_prefs.disable_fwd) return false; |
||||
|
if (packet->isRouteFlood() && packet->path_len >= _prefs.flood_max) return false; |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
int SensorMesh::calcRxDelay(float score, uint32_t air_time) const { |
||||
|
if (_prefs.rx_delay_base <= 0.0f) return 0; |
||||
|
return (int) ((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time); |
||||
|
} |
||||
|
|
||||
|
uint32_t SensorMesh::getRetransmitDelay(const mesh::Packet* packet) { |
||||
|
uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.tx_delay_factor); |
||||
|
return getRNG()->nextInt(0, 6)*t; |
||||
|
} |
||||
|
uint32_t SensorMesh::getDirectRetransmitDelay(const mesh::Packet* packet) { |
||||
|
uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.direct_tx_delay_factor); |
||||
|
return getRNG()->nextInt(0, 6)*t; |
||||
|
} |
||||
|
int SensorMesh::getInterferenceThreshold() const { |
||||
|
return _prefs.interference_threshold; |
||||
|
} |
||||
|
int SensorMesh::getAGCResetInterval() const { |
||||
|
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
|
||||
|
} |
||||
|
|
||||
|
uint8_t SensorMesh::handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data) { |
||||
|
ContactInfo* client; |
||||
|
if (data[0] == 0) { // blank password, just check if sender is in ACL
|
||||
|
client = getContact(sender.pub_key, PUB_KEY_SIZE); |
||||
|
if (client == NULL) { |
||||
|
#if MESH_DEBUG |
||||
|
MESH_DEBUG_PRINTLN("Login, sender not in ACL"); |
||||
|
#endif |
||||
|
return 0; |
||||
|
} |
||||
|
} else { |
||||
|
if (strcmp((char *) data, _prefs.password) != 0) { // check for valid admin password
|
||||
|
#if MESH_DEBUG |
||||
|
MESH_DEBUG_PRINTLN("Invalid password: %s", &data[4]); |
||||
|
#endif |
||||
|
return 0; |
||||
|
} |
||||
|
|
||||
|
client = putContact(sender, PERM_RECV_ALERTS_HI | PERM_RECV_ALERTS_LO); // add to contacts (if not already known)
|
||||
|
if (sender_timestamp <= client->last_timestamp) { |
||||
|
MESH_DEBUG_PRINTLN("Possible login replay attack!"); |
||||
|
return 0; // FATAL: client table is full -OR- replay attack
|
||||
|
} |
||||
|
|
||||
|
MESH_DEBUG_PRINTLN("Login success!"); |
||||
|
client->last_timestamp = sender_timestamp; |
||||
|
client->last_activity = getRTCClock()->getCurrentTime(); |
||||
|
client->permissions |= PERM_ACL_ADMIN; |
||||
|
memcpy(client->shared_secret, secret, PUB_KEY_SIZE); |
||||
|
|
||||
|
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); |
||||
|
} |
||||
|
|
||||
|
uint32_t now = getRTCClock()->getCurrentTimeUnique(); |
||||
|
memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
|
||||
|
reply_data[4] = RESP_SERVER_LOGIN_OK; |
||||
|
reply_data[5] = 0; // NEW: recommended keep-alive interval (secs / 16)
|
||||
|
reply_data[6] = client->isAdmin() ? 1 : 0; |
||||
|
reply_data[7] = client->permissions; |
||||
|
getRNG()->random(&reply_data[8], 4); // random blob to help packet-hash uniqueness
|
||||
|
|
||||
|
return 12; // reply length
|
||||
|
} |
||||
|
|
||||
|
void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* reply) { |
||||
|
while (*command == ' ') command++; // skip leading spaces
|
||||
|
|
||||
|
if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
|
||||
|
memcpy(reply, command, 3); // reflect the prefix back
|
||||
|
reply += 3; |
||||
|
command += 3; |
||||
|
} |
||||
|
|
||||
|
// first, see if this is a custom-handled CLI command (ie. in main.cpp)
|
||||
|
if (handleCustomCommand(sender_timestamp, command, reply)) { |
||||
|
return; // command has been handled
|
||||
|
} |
||||
|
|
||||
|
// handle sensor-specific CLI commands
|
||||
|
if (memcmp(command, "setperm ", 8) == 0) { // format: setperm {pubkey-hex} {permissions-int8}
|
||||
|
char* hex = &command[8]; |
||||
|
char* sp = strchr(hex, ' '); // look for separator char
|
||||
|
if (sp == NULL) { |
||||
|
strcpy(reply, "Err - bad params"); |
||||
|
} else { |
||||
|
*sp++ = 0; // replace space with null terminator
|
||||
|
|
||||
|
uint8_t pubkey[PUB_KEY_SIZE]; |
||||
|
int hex_len = min(sp - hex, PUB_KEY_SIZE*2); |
||||
|
if (mesh::Utils::fromHex(pubkey, hex_len / 2, hex)) { |
||||
|
uint8_t perms = atoi(sp); |
||||
|
if (applyContactPermissions(pubkey, hex_len / 2, perms)) { |
||||
|
strcpy(reply, "OK"); |
||||
|
} else { |
||||
|
strcpy(reply, "Err - invalid params"); |
||||
|
} |
||||
|
} else { |
||||
|
strcpy(reply, "Err - bad pubkey"); |
||||
|
} |
||||
|
} |
||||
|
} else if (sender_timestamp == 0 && strcmp(command, "get acl") == 0) { |
||||
|
Serial.println("ACL:"); |
||||
|
for (int i = 0; i < num_contacts; i++) { |
||||
|
auto c = &contacts[i]; |
||||
|
if (c->permissions == 0) continue; // skip deleted entries
|
||||
|
|
||||
|
Serial.printf("%02X ", c->permissions); |
||||
|
mesh::Utils::printHex(Serial, c->id.pub_key, PUB_KEY_SIZE); |
||||
|
Serial.printf("\n"); |
||||
|
} |
||||
|
reply[0] = 0; |
||||
|
} else { |
||||
|
_cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) { |
||||
|
if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
|
||||
|
uint32_t timestamp; |
||||
|
memcpy(×tamp, data, 4); |
||||
|
|
||||
|
data[len] = 0; // ensure null terminator
|
||||
|
uint8_t reply_len = handleLoginReq(sender, secret, timestamp, &data[4]); |
||||
|
|
||||
|
if (reply_len == 0) return; // invalid request
|
||||
|
|
||||
|
if (packet->isRouteFlood()) { |
||||
|
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
|
||||
|
mesh::Packet* path = createPathReturn(sender, secret, packet->path, packet->path_len, |
||||
|
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len); |
||||
|
if (path) sendFlood(path, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, secret, reply_data, reply_len); |
||||
|
if (reply) sendFlood(reply, SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
int SensorMesh::searchPeersByHash(const uint8_t* hash) { |
||||
|
int n = 0; |
||||
|
for (int i = 0; i < num_contacts && n < MAX_SEARCH_RESULTS; i++) { |
||||
|
if (contacts[i].id.isHashMatch(hash)) { |
||||
|
matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
|
||||
|
} |
||||
|
} |
||||
|
return n; |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) { |
||||
|
int i = matching_peer_indexes[peer_idx]; |
||||
|
if (i >= 0 && i < num_contacts) { |
||||
|
// lookup pre-calculated shared_secret
|
||||
|
memcpy(dest_secret, contacts[i].shared_secret, PUB_KEY_SIZE); |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) { |
||||
|
int i = matching_peer_indexes[sender_idx]; |
||||
|
if (i < 0 || i >= num_contacts) { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: Invalid sender idx: %d", i); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
ContactInfo& from = contacts[i]; |
||||
|
|
||||
|
if (type == PAYLOAD_TYPE_REQ) { // request (from a known contact)
|
||||
|
uint32_t timestamp; |
||||
|
memcpy(×tamp, data, 4); |
||||
|
|
||||
|
if (timestamp > from.last_timestamp) { // prevent replay attacks
|
||||
|
uint8_t reply_len = handleRequest(from.isAdmin() ? 0xFF : from.permissions, timestamp, data[4], &data[5], len - 5); |
||||
|
if (reply_len == 0) return; // invalid command
|
||||
|
|
||||
|
from.last_timestamp = timestamp; |
||||
|
from.last_activity = getRTCClock()->getCurrentTime(); |
||||
|
|
||||
|
if (packet->isRouteFlood()) { |
||||
|
// let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
|
||||
|
mesh::Packet* path = createPathReturn(from.id, secret, packet->path, packet->path_len, |
||||
|
PAYLOAD_TYPE_RESPONSE, reply_data, reply_len); |
||||
|
if (path) sendFlood(path, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, from.id, secret, reply_data, reply_len); |
||||
|
if (reply) { |
||||
|
if (from.out_path_len >= 0) { // we have an out_path, so send DIRECT
|
||||
|
sendDirect(reply, from.out_path, from.out_path_len, SERVER_RESPONSE_DELAY); |
||||
|
} else { |
||||
|
sendFlood(reply, SERVER_RESPONSE_DELAY); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); |
||||
|
} |
||||
|
} else if (type == PAYLOAD_TYPE_TXT_MSG && len > 5 && from.isAdmin()) { // a CLI command
|
||||
|
uint32_t sender_timestamp; |
||||
|
memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
|
||||
|
uint flags = (data[4] >> 2); // message attempt number, and other flags
|
||||
|
|
||||
|
if (!(flags == TXT_TYPE_CLI_DATA)) { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported text type received: flags=%02x", (uint32_t)flags); |
||||
|
} else if (sender_timestamp > from.last_timestamp) { // prevent replay attacks
|
||||
|
from.last_timestamp = sender_timestamp; |
||||
|
from.last_activity = getRTCClock()->getCurrentTime(); |
||||
|
|
||||
|
// len can be > original length, but 'text' will be padded with zeroes
|
||||
|
data[len] = 0; // need to make a C string again, with null terminator
|
||||
|
|
||||
|
uint8_t temp[166]; |
||||
|
char *command = (char *) &data[5]; |
||||
|
char *reply = (char *) &temp[5]; |
||||
|
handleCommand(sender_timestamp, command, reply); |
||||
|
|
||||
|
int text_len = strlen(reply); |
||||
|
if (text_len > 0) { |
||||
|
uint32_t timestamp = getRTCClock()->getCurrentTimeUnique(); |
||||
|
if (timestamp == sender_timestamp) { |
||||
|
// WORKAROUND: the two timestamps need to be different, in the CLI view
|
||||
|
timestamp++; |
||||
|
} |
||||
|
memcpy(temp, ×tamp, 4); // mostly an extra blob to help make packet_hash unique
|
||||
|
temp[4] = (TXT_TYPE_CLI_DATA << 2); |
||||
|
|
||||
|
auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, from.id, secret, temp, 5 + text_len); |
||||
|
if (reply) { |
||||
|
if (from.out_path_len < 0) { |
||||
|
sendFlood(reply, CLI_REPLY_DELAY_MILLIS); |
||||
|
} else { |
||||
|
sendDirect(reply, from.out_path, from.out_path_len, CLI_REPLY_DELAY_MILLIS); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
bool SensorMesh::onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) { |
||||
|
int i = matching_peer_indexes[sender_idx]; |
||||
|
if (i < 0 || i >= num_contacts) { |
||||
|
MESH_DEBUG_PRINTLN("onPeerPathRecv: Invalid sender idx: %d", i); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
ContactInfo& from = contacts[i]; |
||||
|
|
||||
|
MESH_DEBUG_PRINTLN("PATH to contact, path_len=%d", (uint32_t) path_len); |
||||
|
// NOTE: for this impl, we just replace the current 'out_path' regardless, whenever sender sends us a new out_path.
|
||||
|
// FUTURE: could store multiple out_paths per contact, and try to find which is the 'best'(?)
|
||||
|
memcpy(from.out_path, path, from.out_path_len = path_len); // store a copy of path, for sendDirect()
|
||||
|
from.last_activity = getRTCClock()->getCurrentTime(); |
||||
|
|
||||
|
// REVISIT: maybe make ALL out_paths non-persisted to minimise flash writes??
|
||||
|
if (from.isAdmin()) { |
||||
|
// only do saveContacts() (of this out_path change) if this is an admin
|
||||
|
dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); |
||||
|
} |
||||
|
|
||||
|
// NOTE: no reciprocal path send!!
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::onAckRecv(mesh::Packet* packet, uint32_t ack_crc) { |
||||
|
if (num_alert_tasks > 0) { |
||||
|
auto t = alert_tasks[0]; // check current alert task
|
||||
|
for (int i = 0; i < t->attempt; i++) { |
||||
|
if (ack_crc == t->expected_acks[i]) { // matching ACK!
|
||||
|
t->attempt = 4; // signal to move to next contact
|
||||
|
t->send_expiry = 0; |
||||
|
packet->markDoNotRetransmit(); // ACK was for this node, so don't retransmit
|
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
SensorMesh::SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables) |
||||
|
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables), |
||||
|
_cli(board, rtc, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) |
||||
|
{ |
||||
|
num_contacts = 0; |
||||
|
next_local_advert = next_flood_advert = 0; |
||||
|
dirty_contacts_expiry = 0; |
||||
|
last_read_time = 0; |
||||
|
num_alert_tasks = 0; |
||||
|
set_radio_at = revert_radio_at = 0; |
||||
|
|
||||
|
// defaults
|
||||
|
memset(&_prefs, 0, sizeof(_prefs)); |
||||
|
_prefs.airtime_factor = 1.0; // one half
|
||||
|
_prefs.rx_delay_base = 0.0f; // turn off by default, was 10.0;
|
||||
|
_prefs.tx_delay_factor = 0.5f; // was 0.25f
|
||||
|
StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name)); |
||||
|
_prefs.node_lat = ADVERT_LAT; |
||||
|
_prefs.node_lon = ADVERT_LON; |
||||
|
StrHelper::strncpy(_prefs.password, ADMIN_PASSWORD, sizeof(_prefs.password)); |
||||
|
_prefs.freq = LORA_FREQ; |
||||
|
_prefs.sf = LORA_SF; |
||||
|
_prefs.bw = LORA_BW; |
||||
|
_prefs.cr = LORA_CR; |
||||
|
_prefs.tx_power_dbm = LORA_TX_POWER; |
||||
|
_prefs.advert_interval = 1; // default to 2 minutes for NEW installs
|
||||
|
_prefs.flood_advert_interval = 0; // disabled
|
||||
|
_prefs.disable_fwd = true; |
||||
|
_prefs.flood_max = 64; |
||||
|
_prefs.interference_threshold = 0; // disabled
|
||||
|
} |
||||
|
|
||||
|
void SensorMesh::begin(FILESYSTEM* fs) { |
||||
|
mesh::Mesh::begin(); |
||||
|
_fs = fs; |
||||
|
// load persisted prefs
|
||||
|
_cli.loadPrefs(_fs); |
||||
|
|
||||
|
loadContacts(); |
||||
|
|
||||
|
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); |
||||
|
radio_set_tx_power(_prefs.tx_power_dbm); |
||||
|
|
||||
|
updateAdvertTimer(); |
||||
|
updateFloodAdvertTimer(); |
||||
|
} |
||||
|
|
||||
|
bool SensorMesh::formatFileSystem() { |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
return InternalFS.format(); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
return LittleFS.format(); |
||||
|
#elif defined(ESP32) |
||||
|
return SPIFFS.format(); |
||||
|
#else |
||||
|
#error "need to implement file system erase" |
||||
|
return false; |
||||
|
#endif |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) { |
||||
|
set_radio_at = futureMillis(2000); // give CLI reply some time to be sent back, before applying temp radio params
|
||||
|
pending_freq = freq; |
||||
|
pending_bw = bw; |
||||
|
pending_sf = sf; |
||||
|
pending_cr = cr; |
||||
|
|
||||
|
revert_radio_at = futureMillis(2000 + timeout_mins*60*1000); // schedule when to revert radio params
|
||||
|
} |
||||
|
|
||||
|
void SensorMesh::sendSelfAdvertisement(int delay_millis) { |
||||
|
mesh::Packet* pkt = createSelfAdvert(); |
||||
|
if (pkt) { |
||||
|
sendFlood(pkt, delay_millis); |
||||
|
} else { |
||||
|
MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::updateAdvertTimer() { |
||||
|
if (_prefs.advert_interval > 0) { // schedule local advert timer
|
||||
|
next_local_advert = futureMillis( ((uint32_t)_prefs.advert_interval) * 2 * 60 * 1000); |
||||
|
} else { |
||||
|
next_local_advert = 0; // stop the timer
|
||||
|
} |
||||
|
} |
||||
|
void SensorMesh::updateFloodAdvertTimer() { |
||||
|
if (_prefs.flood_advert_interval > 0) { // schedule flood advert timer
|
||||
|
next_flood_advert = futureMillis( ((uint32_t)_prefs.flood_advert_interval) * 60 * 60 * 1000); |
||||
|
} else { |
||||
|
next_flood_advert = 0; // stop the timer
|
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::setTxPower(uint8_t power_dbm) { |
||||
|
radio_set_tx_power(power_dbm); |
||||
|
} |
||||
|
|
||||
|
float SensorMesh::getTelemValue(uint8_t channel, uint8_t type) { |
||||
|
auto buf = telemetry.getBuffer(); |
||||
|
uint8_t size = telemetry.getSize(); |
||||
|
uint8_t i = 0; |
||||
|
|
||||
|
while (i + 2 < size) { |
||||
|
// Get channel #
|
||||
|
uint8_t ch = buf[i++]; |
||||
|
// Get data type
|
||||
|
uint8_t t = buf[i++]; |
||||
|
uint8_t sz = getDataSize(t); |
||||
|
|
||||
|
if (ch == channel && t == type) { |
||||
|
return getFloat(&buf[i], sz, getMultiplier(t), isSigned(t)); |
||||
|
} |
||||
|
i += sz; // skip
|
||||
|
} |
||||
|
return 0.0f; // not found
|
||||
|
} |
||||
|
|
||||
|
bool SensorMesh::getGPS(uint8_t channel, float& lat, float& lon, float& alt) { |
||||
|
if (channel == TELEM_CHANNEL_SELF) { |
||||
|
lat = sensors.node_lat; |
||||
|
lon = sensors.node_lon; |
||||
|
alt = sensors.node_altitude; |
||||
|
return true; |
||||
|
} |
||||
|
// REVISIT: custom GPS channels??
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
void SensorMesh::loop() { |
||||
|
mesh::Mesh::loop(); |
||||
|
|
||||
|
if (next_flood_advert && millisHasNowPassed(next_flood_advert)) { |
||||
|
mesh::Packet* pkt = createSelfAdvert(); |
||||
|
if (pkt) sendFlood(pkt); |
||||
|
|
||||
|
updateFloodAdvertTimer(); // schedule next flood advert
|
||||
|
updateAdvertTimer(); // also schedule local advert (so they don't overlap)
|
||||
|
} else if (next_local_advert && millisHasNowPassed(next_local_advert)) { |
||||
|
mesh::Packet* pkt = createSelfAdvert(); |
||||
|
if (pkt) sendZeroHop(pkt); |
||||
|
|
||||
|
updateAdvertTimer(); // schedule next local advert
|
||||
|
} |
||||
|
|
||||
|
if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params
|
||||
|
set_radio_at = 0; // clear timer
|
||||
|
radio_set_params(pending_freq, pending_bw, pending_sf, pending_cr); |
||||
|
MESH_DEBUG_PRINTLN("Temp radio params"); |
||||
|
} |
||||
|
|
||||
|
if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
|
||||
|
revert_radio_at = 0; // clear timer
|
||||
|
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr); |
||||
|
MESH_DEBUG_PRINTLN("Radio params restored"); |
||||
|
} |
||||
|
|
||||
|
uint32_t curr = getRTCClock()->getCurrentTime(); |
||||
|
if (curr >= last_read_time + SENSOR_READ_INTERVAL_SECS) { |
||||
|
telemetry.reset(); |
||||
|
telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f); |
||||
|
// query other sensors -- target specific
|
||||
|
sensors.querySensors(0xFF, telemetry); // allow all telemetry permissions
|
||||
|
|
||||
|
onSensorDataRead(); |
||||
|
|
||||
|
last_read_time = curr; |
||||
|
} |
||||
|
|
||||
|
// check the alert send queue
|
||||
|
if (num_alert_tasks > 0) { |
||||
|
auto t = alert_tasks[0]; // process head of queue
|
||||
|
|
||||
|
if (millisHasNowPassed(t->send_expiry)) { // next send needed?
|
||||
|
if (t->attempt >= 4) { // max attempts reached, try next contact
|
||||
|
t->curr_contact_idx++; |
||||
|
if (t->curr_contact_idx >= num_contacts) { // no more contacts to try?
|
||||
|
num_alert_tasks--; // remove t from queue
|
||||
|
for (int i = 0; i < num_alert_tasks; i++) { |
||||
|
alert_tasks[i] = alert_tasks[i + 1]; |
||||
|
} |
||||
|
} else { |
||||
|
auto c = &contacts[t->curr_contact_idx]; |
||||
|
uint16_t pri_mask = (t->pri == HIGH_PRI_ALERT) ? PERM_RECV_ALERTS_HI : PERM_RECV_ALERTS_LO; |
||||
|
|
||||
|
if (c->permissions & pri_mask) { // contact wants alert
|
||||
|
// reset attempts
|
||||
|
t->attempt = (t->pri == LOW_PRI_ALERT) ? 3 : 0; // Low pri alerts, start at attempt #3 (ie. only make ONE attempt)
|
||||
|
t->timestamp = getRTCClock()->getCurrentTimeUnique(); // need unique timestamp per contact
|
||||
|
|
||||
|
sendAlert(c, t); // NOTE: modifies attempt, expected_acks[] and send_expiry
|
||||
|
} else { |
||||
|
// next contact tested in next ::loop()
|
||||
|
} |
||||
|
} |
||||
|
} else if (t->curr_contact_idx < num_contacts) { |
||||
|
auto c = &contacts[t->curr_contact_idx]; // send next attempt
|
||||
|
sendAlert(c, t); // NOTE: modifies attempt, expected_acks[] and send_expiry
|
||||
|
} else { |
||||
|
// contact list has likely been modified while waiting for alert ACK, cancel this task
|
||||
|
t->attempt = 4; // next ::loop() will remove t from queue
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// is there are pending dirty contacts write needed?
|
||||
|
if (dirty_contacts_expiry && millisHasNowPassed(dirty_contacts_expiry)) { |
||||
|
saveContacts(); |
||||
|
dirty_contacts_expiry = 0; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,175 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Arduino.h> // needed for PlatformIO |
||||
|
#include <Mesh.h> |
||||
|
|
||||
|
#include "TimeSeriesData.h" |
||||
|
|
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
#include <InternalFileSystem.h> |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
#include <LittleFS.h> |
||||
|
#elif defined(ESP32) |
||||
|
#include <SPIFFS.h> |
||||
|
#endif |
||||
|
|
||||
|
#include <helpers/ArduinoHelpers.h> |
||||
|
#include <helpers/StaticPoolPacketManager.h> |
||||
|
#include <helpers/SimpleMeshTables.h> |
||||
|
#include <helpers/IdentityStore.h> |
||||
|
#include <helpers/AdvertDataHelpers.h> |
||||
|
#include <helpers/TxtDataHelpers.h> |
||||
|
#include <helpers/CommonCLI.h> |
||||
|
#include <RTClib.h> |
||||
|
#include <target.h> |
||||
|
|
||||
|
#define PERM_ACL_ROLE_MASK 3 // lower 2 bits
|
||||
|
#define PERM_ACL_GUEST 0 |
||||
|
#define PERM_ACL_READ_ONLY 1 |
||||
|
#define PERM_ACL_READ_WRITE 2 |
||||
|
#define PERM_ACL_ADMIN 3 |
||||
|
|
||||
|
#define PERM_RESERVED1 (1 << 2) |
||||
|
#define PERM_RESERVED2 (1 << 3) |
||||
|
#define PERM_RESERVED3 (1 << 4) |
||||
|
#define PERM_RESERVED4 (1 << 5) |
||||
|
#define PERM_RECV_ALERTS_LO (1 << 6) // low priority alerts
|
||||
|
#define PERM_RECV_ALERTS_HI (1 << 7) // high priority alerts
|
||||
|
|
||||
|
struct ContactInfo { |
||||
|
mesh::Identity id; |
||||
|
uint8_t permissions; |
||||
|
int8_t out_path_len; |
||||
|
uint8_t out_path[MAX_PATH_SIZE]; |
||||
|
uint8_t shared_secret[PUB_KEY_SIZE]; |
||||
|
uint32_t last_timestamp; // by THEIR clock (transient)
|
||||
|
uint32_t last_activity; // by OUR clock (transient)
|
||||
|
|
||||
|
bool isAdmin() const { return (permissions & PERM_ACL_ROLE_MASK) == PERM_ACL_ADMIN; } |
||||
|
}; |
||||
|
|
||||
|
#ifndef FIRMWARE_BUILD_DATE |
||||
|
#define FIRMWARE_BUILD_DATE "24 Jul 2025" |
||||
|
#endif |
||||
|
|
||||
|
#ifndef FIRMWARE_VERSION |
||||
|
#define FIRMWARE_VERSION "v1.7.4" |
||||
|
#endif |
||||
|
|
||||
|
#define FIRMWARE_ROLE "sensor" |
||||
|
|
||||
|
#define MAX_CONTACTS 20 |
||||
|
|
||||
|
#define MAX_SEARCH_RESULTS 8 |
||||
|
#define MAX_CONCURRENT_ALERTS 4 |
||||
|
|
||||
|
class SensorMesh : public mesh::Mesh, public CommonCLICallbacks { |
||||
|
public: |
||||
|
SensorMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables); |
||||
|
void begin(FILESYSTEM* fs); |
||||
|
void loop(); |
||||
|
void handleCommand(uint32_t sender_timestamp, char* command, char* reply); |
||||
|
|
||||
|
// CommonCLI callbacks
|
||||
|
const char* getFirmwareVer() override { return FIRMWARE_VERSION; } |
||||
|
const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; } |
||||
|
const char* getRole() override { return FIRMWARE_ROLE; } |
||||
|
const char* getNodeName() { return _prefs.node_name; } |
||||
|
NodePrefs* getNodePrefs() { return &_prefs; } |
||||
|
void savePrefs() override { _cli.savePrefs(_fs); } |
||||
|
bool formatFileSystem() override; |
||||
|
void sendSelfAdvertisement(int delay_millis) override; |
||||
|
void updateAdvertTimer() override; |
||||
|
void updateFloodAdvertTimer() override; |
||||
|
void setLoggingOn(bool enable) override { } |
||||
|
void eraseLogFile() override { } |
||||
|
void dumpLogFile() override { } |
||||
|
void setTxPower(uint8_t power_dbm) override; |
||||
|
void formatNeighborsReply(char *reply) override { |
||||
|
strcpy(reply, "not supported"); |
||||
|
} |
||||
|
const uint8_t* getSelfIdPubKey() override { return self_id.pub_key; } |
||||
|
void clearStats() override { } |
||||
|
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override; |
||||
|
|
||||
|
float getTelemValue(uint8_t channel, uint8_t type); |
||||
|
|
||||
|
protected: |
||||
|
// current telemetry data queries
|
||||
|
float getVoltage(uint8_t channel) { return getTelemValue(channel, LPP_VOLTAGE); } |
||||
|
float getCurrent(uint8_t channel) { return getTelemValue(channel, LPP_CURRENT); } |
||||
|
float getPower(uint8_t channel) { return getTelemValue(channel, LPP_POWER); } |
||||
|
float getTemperature(uint8_t channel) { return getTelemValue(channel, LPP_TEMPERATURE); } |
||||
|
float getRelativeHumidity(uint8_t channel) { return getTelemValue(channel, LPP_RELATIVE_HUMIDITY); } |
||||
|
float getBarometricPressure(uint8_t channel) { return getTelemValue(channel, LPP_BAROMETRIC_PRESSURE); } |
||||
|
float getAltitude(uint8_t channel) { return getTelemValue(channel, LPP_ALTITUDE); } |
||||
|
bool getGPS(uint8_t channel, float& lat, float& lon, float& alt); |
||||
|
|
||||
|
// alerts
|
||||
|
enum AlertPriority { LOW_PRI_ALERT, HIGH_PRI_ALERT }; |
||||
|
|
||||
|
struct Trigger { |
||||
|
uint32_t timestamp; |
||||
|
AlertPriority pri; |
||||
|
uint32_t expected_acks[4]; |
||||
|
int8_t curr_contact_idx; |
||||
|
uint8_t attempt; |
||||
|
unsigned long send_expiry; |
||||
|
char text[MAX_PACKET_PAYLOAD]; |
||||
|
|
||||
|
Trigger() { text[0] = 0; } |
||||
|
bool isTriggered() const { return text[0] != 0; } |
||||
|
}; |
||||
|
void alertIf(bool condition, Trigger& t, AlertPriority pri, const char* text); |
||||
|
|
||||
|
virtual void onSensorDataRead() = 0; // for app to implement
|
||||
|
virtual int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) = 0; // for app to implement
|
||||
|
virtual bool handleCustomCommand(uint32_t sender_timestamp, char* command, char* reply) { return false; } |
||||
|
|
||||
|
// Mesh overrides
|
||||
|
float getAirtimeBudgetFactor() const override; |
||||
|
bool allowPacketForward(const mesh::Packet* packet) override; |
||||
|
int calcRxDelay(float score, uint32_t air_time) const override; |
||||
|
uint32_t getRetransmitDelay(const mesh::Packet* packet) override; |
||||
|
uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override; |
||||
|
int getInterferenceThreshold() const override; |
||||
|
int getAGCResetInterval() const override; |
||||
|
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override; |
||||
|
int searchPeersByHash(const uint8_t* hash) override; |
||||
|
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override; |
||||
|
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override; |
||||
|
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override; |
||||
|
void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override; |
||||
|
|
||||
|
private: |
||||
|
FILESYSTEM* _fs; |
||||
|
unsigned long next_local_advert, next_flood_advert; |
||||
|
NodePrefs _prefs; |
||||
|
CommonCLI _cli; |
||||
|
uint8_t reply_data[MAX_PACKET_PAYLOAD]; |
||||
|
ContactInfo contacts[MAX_CONTACTS]; |
||||
|
int num_contacts; |
||||
|
unsigned long dirty_contacts_expiry; |
||||
|
CayenneLPP telemetry; |
||||
|
uint32_t last_read_time; |
||||
|
int matching_peer_indexes[MAX_SEARCH_RESULTS]; |
||||
|
int num_alert_tasks; |
||||
|
Trigger* alert_tasks[MAX_CONCURRENT_ALERTS]; |
||||
|
unsigned long set_radio_at, revert_radio_at; |
||||
|
float pending_freq; |
||||
|
float pending_bw; |
||||
|
uint8_t pending_sf; |
||||
|
uint8_t pending_cr; |
||||
|
|
||||
|
void loadContacts(); |
||||
|
void saveContacts(); |
||||
|
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data); |
||||
|
uint8_t handleRequest(uint8_t perms, uint32_t sender_timestamp, uint8_t req_type, uint8_t* payload, size_t payload_len); |
||||
|
mesh::Packet* createSelfAdvert(); |
||||
|
ContactInfo* getContact(const uint8_t* pubkey, int key_len); |
||||
|
ContactInfo* putContact(const mesh::Identity& id, uint8_t init_perms); |
||||
|
bool applyContactPermissions(const uint8_t* pubkey, int key_len, uint8_t perms); |
||||
|
|
||||
|
void sendAlert(ContactInfo* c, Trigger* t); |
||||
|
|
||||
|
}; |
||||
@ -0,0 +1,45 @@ |
|||||
|
#include "TimeSeriesData.h" |
||||
|
|
||||
|
void TimeSeriesData::recordData(mesh::RTCClock* clock, float value) { |
||||
|
uint32_t now = clock->getCurrentTime(); |
||||
|
if (now >= last_timestamp + interval_secs) { |
||||
|
last_timestamp = now; |
||||
|
|
||||
|
data[next] = value; // append to cycle table
|
||||
|
next = (next + 1) % num_slots; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void TimeSeriesData::calcMinMaxAvg(mesh::RTCClock* clock, uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg* dest, uint8_t channel, uint8_t lpp_type) const { |
||||
|
int i = next, n = num_slots; |
||||
|
uint32_t ago = clock->getCurrentTime() - last_timestamp; |
||||
|
int num_values = 0; |
||||
|
float total = 0.0f; |
||||
|
|
||||
|
dest->_channel = channel; |
||||
|
dest->_lpp_type = lpp_type; |
||||
|
|
||||
|
// start at most recet recording, back-track through to oldest
|
||||
|
while (n > 0) { |
||||
|
n--; |
||||
|
i = (i + num_slots - 1) % num_slots; // go back by one
|
||||
|
if (ago >= end_secs_ago && ago < start_secs_ago) { // filter by the desired time range
|
||||
|
float v = data[i]; |
||||
|
num_values++; |
||||
|
total += v; |
||||
|
if (num_values == 1) { |
||||
|
dest->_max = dest->_min = v; |
||||
|
} else { |
||||
|
if (v < dest->_min) dest->_min = v; |
||||
|
if (v > dest->_max) dest->_max = v; |
||||
|
} |
||||
|
} |
||||
|
ago += interval_secs; |
||||
|
} |
||||
|
// calc average
|
||||
|
if (num_values > 0) { |
||||
|
dest->_avg = total / num_values; |
||||
|
} else { |
||||
|
dest->_max = dest->_min = dest->_avg = NAN; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <Arduino.h> |
||||
|
#include <Mesh.h> |
||||
|
|
||||
|
struct MinMaxAvg { |
||||
|
float _min, _max, _avg; |
||||
|
uint8_t _lpp_type, _channel; |
||||
|
}; |
||||
|
|
||||
|
class TimeSeriesData { |
||||
|
float* data; |
||||
|
int num_slots, next; |
||||
|
uint32_t last_timestamp; |
||||
|
uint32_t interval_secs; |
||||
|
|
||||
|
public: |
||||
|
TimeSeriesData(float* array, int num, uint32_t secs) : num_slots(num), data(array), last_timestamp(0), next(0), interval_secs(secs) { |
||||
|
memset(data, 0, sizeof(float)*num); |
||||
|
} |
||||
|
TimeSeriesData(int num, uint32_t secs) : num_slots(num), last_timestamp(0), next(0), interval_secs(secs) { |
||||
|
data = new float[num]; |
||||
|
memset(data, 0, sizeof(float)*num); |
||||
|
} |
||||
|
|
||||
|
void recordData(mesh::RTCClock* clock, float value); |
||||
|
void calcMinMaxAvg(mesh::RTCClock* clock, uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg* dest, uint8_t channel, uint8_t lpp_type) const; |
||||
|
}; |
||||
|
|
||||
@ -0,0 +1,114 @@ |
|||||
|
#include "UITask.h" |
||||
|
#include <Arduino.h> |
||||
|
#include <helpers/CommonCLI.h> |
||||
|
|
||||
|
#define AUTO_OFF_MILLIS 20000 // 20 seconds
|
||||
|
#define BOOT_SCREEN_MILLIS 4000 // 4 seconds
|
||||
|
|
||||
|
// 'meshcore', 128x13px
|
||||
|
static const uint8_t meshcore_logo [] PROGMEM = { |
||||
|
0x3c, 0x01, 0xe3, 0xff, 0xc7, 0xff, 0x8f, 0x03, 0x87, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, |
||||
|
0x3c, 0x03, 0xe3, 0xff, 0xc7, 0xff, 0x8e, 0x03, 0x8f, 0xfe, 0x3f, 0xfe, 0x1f, 0xff, 0x1f, 0xfe, |
||||
|
0x3e, 0x03, 0xc3, 0xff, 0x8f, 0xff, 0x0e, 0x07, 0x8f, 0xfe, 0x7f, 0xfe, 0x1f, 0xff, 0x1f, 0xfc, |
||||
|
0x3e, 0x07, 0xc7, 0x80, 0x0e, 0x00, 0x0e, 0x07, 0x9e, 0x00, 0x78, 0x0e, 0x3c, 0x0f, 0x1c, 0x00, |
||||
|
0x3e, 0x0f, 0xc7, 0x80, 0x1e, 0x00, 0x0e, 0x07, 0x1e, 0x00, 0x70, 0x0e, 0x38, 0x0f, 0x3c, 0x00, |
||||
|
0x7f, 0x0f, 0xc7, 0xfe, 0x1f, 0xfc, 0x1f, 0xff, 0x1c, 0x00, 0x70, 0x0e, 0x38, 0x0e, 0x3f, 0xf8, |
||||
|
0x7f, 0x1f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x0e, 0x38, 0x0e, 0x3f, 0xf8, |
||||
|
0x7f, 0x3f, 0xc7, 0xfe, 0x0f, 0xff, 0x1f, 0xff, 0x1c, 0x00, 0xf0, 0x1e, 0x3f, 0xfe, 0x3f, 0xf0, |
||||
|
0x77, 0x3b, 0x87, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xfc, 0x38, 0x00, |
||||
|
0x77, 0xfb, 0x8f, 0x00, 0x00, 0x07, 0x1c, 0x0f, 0x3c, 0x00, 0xe0, 0x1c, 0x7f, 0xf8, 0x38, 0x00, |
||||
|
0x73, 0xf3, 0x8f, 0xff, 0x0f, 0xff, 0x1c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x78, 0x7f, 0xf8, |
||||
|
0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfe, 0x3c, 0x0e, 0x3f, 0xf8, 0xff, 0xfc, 0x70, 0x3c, 0x7f, 0xf8, |
||||
|
0xe3, 0xe3, 0x8f, 0xff, 0x1f, 0xfc, 0x3c, 0x0e, 0x1f, 0xf8, 0xff, 0xf8, 0x70, 0x3c, 0x7f, 0xf8, |
||||
|
}; |
||||
|
|
||||
|
void UITask::begin(NodePrefs* node_prefs, const char* build_date, const char* firmware_version) { |
||||
|
_prevBtnState = HIGH; |
||||
|
_auto_off = millis() + AUTO_OFF_MILLIS; |
||||
|
_node_prefs = node_prefs; |
||||
|
_display->turnOn(); |
||||
|
|
||||
|
// strip off dash and commit hash by changing dash to null terminator
|
||||
|
// e.g: v1.2.3-abcdef -> v1.2.3
|
||||
|
char *version = strdup(firmware_version); |
||||
|
char *dash = strchr(version, '-'); |
||||
|
if(dash){ |
||||
|
*dash = 0; |
||||
|
} |
||||
|
|
||||
|
// v1.2.3 (1 Jan 2025)
|
||||
|
sprintf(_version_info, "%s (%s)", version, build_date); |
||||
|
} |
||||
|
|
||||
|
void UITask::renderCurrScreen() { |
||||
|
char tmp[80]; |
||||
|
if (millis() < BOOT_SCREEN_MILLIS) { // boot screen
|
||||
|
// meshcore logo
|
||||
|
_display->setColor(DisplayDriver::BLUE); |
||||
|
int logoWidth = 128; |
||||
|
_display->drawXbm((_display->width() - logoWidth) / 2, 3, meshcore_logo, logoWidth, 13); |
||||
|
|
||||
|
// version info
|
||||
|
_display->setColor(DisplayDriver::LIGHT); |
||||
|
_display->setTextSize(1); |
||||
|
uint16_t versionWidth = _display->getTextWidth(_version_info); |
||||
|
_display->setCursor((_display->width() - versionWidth) / 2, 22); |
||||
|
_display->print(_version_info); |
||||
|
|
||||
|
// node type
|
||||
|
const char* node_type = "< Sensor >"; |
||||
|
uint16_t typeWidth = _display->getTextWidth(node_type); |
||||
|
_display->setCursor((_display->width() - typeWidth) / 2, 35); |
||||
|
_display->print(node_type); |
||||
|
} else { // home screen
|
||||
|
// node name
|
||||
|
_display->setCursor(0, 0); |
||||
|
_display->setTextSize(1); |
||||
|
_display->setColor(DisplayDriver::GREEN); |
||||
|
_display->print(_node_prefs->node_name); |
||||
|
|
||||
|
// freq / sf
|
||||
|
_display->setCursor(0, 20); |
||||
|
_display->setColor(DisplayDriver::YELLOW); |
||||
|
sprintf(tmp, "FREQ: %06.3f SF%d", _node_prefs->freq, _node_prefs->sf); |
||||
|
_display->print(tmp); |
||||
|
|
||||
|
// bw / cr
|
||||
|
_display->setCursor(0, 30); |
||||
|
sprintf(tmp, "BW: %03.2f CR: %d", _node_prefs->bw, _node_prefs->cr); |
||||
|
_display->print(tmp); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void UITask::loop() { |
||||
|
#ifdef PIN_USER_BTN |
||||
|
if (millis() >= _next_read) { |
||||
|
int btnState = digitalRead(PIN_USER_BTN); |
||||
|
if (btnState != _prevBtnState) { |
||||
|
if (btnState == LOW) { // pressed?
|
||||
|
if (_display->isOn()) { |
||||
|
// TODO: any action ?
|
||||
|
} else { |
||||
|
_display->turnOn(); |
||||
|
} |
||||
|
_auto_off = millis() + AUTO_OFF_MILLIS; // extend auto-off timer
|
||||
|
} |
||||
|
_prevBtnState = btnState; |
||||
|
} |
||||
|
_next_read = millis() + 200; // 5 reads per second
|
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
if (_display->isOn()) { |
||||
|
if (millis() >= _next_refresh) { |
||||
|
_display->startFrame(); |
||||
|
renderCurrScreen(); |
||||
|
_display->endFrame(); |
||||
|
|
||||
|
_next_refresh = millis() + 1000; // refresh every second
|
||||
|
} |
||||
|
if (millis() > _auto_off) { |
||||
|
_display->turnOff(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <helpers/ui/DisplayDriver.h> |
||||
|
#include <helpers/CommonCLI.h> |
||||
|
|
||||
|
class UITask { |
||||
|
DisplayDriver* _display; |
||||
|
unsigned long _next_read, _next_refresh, _auto_off; |
||||
|
int _prevBtnState; |
||||
|
NodePrefs* _node_prefs; |
||||
|
char _version_info[32]; |
||||
|
|
||||
|
void renderCurrScreen(); |
||||
|
public: |
||||
|
UITask(DisplayDriver& display) : _display(&display) { _next_read = _next_refresh = 0; } |
||||
|
void begin(NodePrefs* node_prefs, const char* build_date, const char* firmware_version); |
||||
|
|
||||
|
void loop(); |
||||
|
}; |
||||
@ -0,0 +1,147 @@ |
|||||
|
#include "SensorMesh.h" |
||||
|
|
||||
|
#ifdef DISPLAY_CLASS |
||||
|
#include "UITask.h" |
||||
|
static UITask ui_task(display); |
||||
|
#endif |
||||
|
|
||||
|
class MyMesh : public SensorMesh { |
||||
|
public: |
||||
|
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables) |
||||
|
: SensorMesh(board, radio, ms, rng, rtc, tables), |
||||
|
battery_data(12*24, 5*60) // 24 hours worth of battery data, every 5 minutes
|
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
protected: |
||||
|
/* ========================== custom logic here ========================== */ |
||||
|
Trigger low_batt, critical_batt; |
||||
|
TimeSeriesData battery_data; |
||||
|
|
||||
|
void onSensorDataRead() override { |
||||
|
float batt_voltage = getVoltage(TELEM_CHANNEL_SELF); |
||||
|
|
||||
|
battery_data.recordData(getRTCClock(), batt_voltage); // record battery
|
||||
|
alertIf(batt_voltage < 3.4f, critical_batt, HIGH_PRI_ALERT, "Battery is critical!"); |
||||
|
alertIf(batt_voltage < 3.6f, low_batt, LOW_PRI_ALERT, "Battery is low"); |
||||
|
} |
||||
|
|
||||
|
int querySeriesData(uint32_t start_secs_ago, uint32_t end_secs_ago, MinMaxAvg dest[], int max_num) override { |
||||
|
battery_data.calcMinMaxAvg(getRTCClock(), start_secs_ago, end_secs_ago, &dest[0], TELEM_CHANNEL_SELF, LPP_VOLTAGE); |
||||
|
return 1; |
||||
|
} |
||||
|
|
||||
|
bool handleCustomCommand(uint32_t sender_timestamp, char* command, char* reply) override { |
||||
|
if (strcmp(command, "magic") == 0) { // example 'custom' command handling
|
||||
|
strcpy(reply, "**Magic now done**"); |
||||
|
return true; // handled
|
||||
|
} |
||||
|
return false; // not handled
|
||||
|
} |
||||
|
/* ======================================================================= */ |
||||
|
}; |
||||
|
|
||||
|
StdRNG fast_rng; |
||||
|
SimpleMeshTables tables; |
||||
|
|
||||
|
MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables); |
||||
|
|
||||
|
void halt() { |
||||
|
while (1) ; |
||||
|
} |
||||
|
|
||||
|
static char command[120]; |
||||
|
|
||||
|
void setup() { |
||||
|
Serial.begin(115200); |
||||
|
delay(1000); |
||||
|
|
||||
|
board.begin(); |
||||
|
|
||||
|
#ifdef DISPLAY_CLASS |
||||
|
if (display.begin()) { |
||||
|
display.startFrame(); |
||||
|
display.print("Please wait..."); |
||||
|
display.endFrame(); |
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
if (!radio_init()) { halt(); } |
||||
|
|
||||
|
fast_rng.begin(radio_get_rng_seed()); |
||||
|
|
||||
|
FILESYSTEM* fs; |
||||
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM) |
||||
|
InternalFS.begin(); |
||||
|
fs = &InternalFS; |
||||
|
IdentityStore store(InternalFS, ""); |
||||
|
#elif defined(ESP32) |
||||
|
SPIFFS.begin(true); |
||||
|
fs = &SPIFFS; |
||||
|
IdentityStore store(SPIFFS, "/identity"); |
||||
|
#elif defined(RP2040_PLATFORM) |
||||
|
LittleFS.begin(); |
||||
|
fs = &LittleFS; |
||||
|
IdentityStore store(LittleFS, "/identity"); |
||||
|
store.begin(); |
||||
|
#else |
||||
|
#error "need to define filesystem" |
||||
|
#endif |
||||
|
if (!store.load("_main", the_mesh.self_id)) { |
||||
|
MESH_DEBUG_PRINTLN("Generating new keypair"); |
||||
|
the_mesh.self_id = radio_new_identity(); // create new random identity
|
||||
|
int count = 0; |
||||
|
while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
|
||||
|
the_mesh.self_id = radio_new_identity(); count++; |
||||
|
} |
||||
|
store.save("_main", the_mesh.self_id); |
||||
|
} |
||||
|
|
||||
|
Serial.print("Sensor ID: "); |
||||
|
mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println(); |
||||
|
|
||||
|
command[0] = 0; |
||||
|
|
||||
|
sensors.begin(); |
||||
|
|
||||
|
the_mesh.begin(fs); |
||||
|
|
||||
|
#ifdef DISPLAY_CLASS |
||||
|
ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION); |
||||
|
#endif |
||||
|
|
||||
|
// send out initial Advertisement to the mesh
|
||||
|
the_mesh.sendSelfAdvertisement(16000); |
||||
|
} |
||||
|
|
||||
|
void loop() { |
||||
|
int len = strlen(command); |
||||
|
while (Serial.available() && len < sizeof(command)-1) { |
||||
|
char c = Serial.read(); |
||||
|
if (c != '\n') { |
||||
|
command[len++] = c; |
||||
|
command[len] = 0; |
||||
|
} |
||||
|
Serial.print(c); |
||||
|
} |
||||
|
if (len == sizeof(command)-1) { // command buffer full
|
||||
|
command[sizeof(command)-1] = '\r'; |
||||
|
} |
||||
|
|
||||
|
if (len > 0 && command[len - 1] == '\r') { // received complete line
|
||||
|
command[len - 1] = 0; // replace newline with C string null terminator
|
||||
|
char reply[160]; |
||||
|
the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
|
||||
|
if (reply[0]) { |
||||
|
Serial.print(" -> "); Serial.println(reply); |
||||
|
} |
||||
|
|
||||
|
command[0] = 0; // reset command buffer
|
||||
|
} |
||||
|
|
||||
|
the_mesh.loop(); |
||||
|
sensors.loop(); |
||||
|
#ifdef DISPLAY_CLASS |
||||
|
ui_task.loop(); |
||||
|
#endif |
||||
|
} |
||||
@ -1,46 +0,0 @@ |
|||||
#pragma once |
|
||||
|
|
||||
#include <RadioLib.h> |
|
||||
|
|
||||
#define LR1110_IRQ_HAS_PREAMBLE 0b0000000100 // 4 4 valid LoRa header received
|
|
||||
#define LR1110_IRQ_HEADER_VALID 0b0000010000 // 4 4 valid LoRa header received
|
|
||||
|
|
||||
class CustomLR1110 : public LR1110 { |
|
||||
public: |
|
||||
CustomLR1110(Module *mod) : LR1110(mod) { } |
|
||||
|
|
||||
RadioLibTime_t getTimeOnAir(size_t len) override { |
|
||||
uint32_t symbolLength_us = ((uint32_t)(1000 * 10) << this->spreadingFactor) / (this->bandwidthKhz * 10) ; |
|
||||
uint8_t sfCoeff1_x4 = 17; // (4.25 * 4)
|
|
||||
uint8_t sfCoeff2 = 8; |
|
||||
if(this->spreadingFactor == 5 || this->spreadingFactor == 6) { |
|
||||
sfCoeff1_x4 = 25; // 6.25 * 4
|
|
||||
sfCoeff2 = 0; |
|
||||
} |
|
||||
uint8_t sfDivisor = 4*this->spreadingFactor; |
|
||||
if(symbolLength_us >= 16000) { |
|
||||
sfDivisor = 4*(this->spreadingFactor - 2); |
|
||||
} |
|
||||
const int8_t bitsPerCrc = 16; |
|
||||
const int8_t N_symbol_header = this->headerType == RADIOLIB_SX126X_LORA_HEADER_EXPLICIT ? 20 : 0; |
|
||||
|
|
||||
// numerator of equation in section 6.1.4 of SX1268 datasheet v1.1 (might not actually be bitcount, but it has len * 8)
|
|
||||
int16_t bitCount = (int16_t) 8 * len + this->crcTypeLoRa * bitsPerCrc - 4 * this->spreadingFactor + sfCoeff2 + N_symbol_header; |
|
||||
if(bitCount < 0) { |
|
||||
bitCount = 0; |
|
||||
} |
|
||||
// add (sfDivisor) - 1 to the numerator to give integer CEIL(...)
|
|
||||
uint16_t nPreCodedSymbols = (bitCount + (sfDivisor - 1)) / (sfDivisor); |
|
||||
|
|
||||
// preamble can be 65k, therefore nSymbol_x4 needs to be 32 bit
|
|
||||
uint32_t nSymbol_x4 = (this->preambleLengthLoRa + 8) * 4 + sfCoeff1_x4 + nPreCodedSymbols * (this->codingRate + 4) * 4; |
|
||||
|
|
||||
return((symbolLength_us * nSymbol_x4) / 4); |
|
||||
} |
|
||||
|
|
||||
bool isReceiving() { |
|
||||
uint16_t irq = getIrqStatus(); |
|
||||
bool detected = ((irq & LR1110_IRQ_HEADER_VALID) || (irq & LR1110_IRQ_HAS_PREAMBLE)); |
|
||||
return detected; |
|
||||
} |
|
||||
}; |
|
||||
@ -0,0 +1,70 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <RadioLib.h> |
||||
|
|
||||
|
#define LR1110_IRQ_HAS_PREAMBLE 0b0000000100 // 4 4 valid LoRa header received
|
||||
|
#define LR1110_IRQ_HEADER_VALID 0b0000010000 // 4 4 valid LoRa header received
|
||||
|
|
||||
|
class CustomLR1110 : public LR1110 { |
||||
|
public: |
||||
|
CustomLR1110(Module *mod) : LR1110(mod) { } |
||||
|
|
||||
|
RadioLibTime_t getTimeOnAir(size_t len) override { |
||||
|
// calculate number of symbols
|
||||
|
float N_symbol = 0; |
||||
|
if(this->codingRate <= RADIOLIB_LR11X0_LORA_CR_4_8_SHORT) { |
||||
|
// legacy coding rate - nice and simple
|
||||
|
// get SF coefficients
|
||||
|
float coeff1 = 0; |
||||
|
int16_t coeff2 = 0; |
||||
|
int16_t coeff3 = 0; |
||||
|
if(this->spreadingFactor < 7) { |
||||
|
// SF5, SF6
|
||||
|
coeff1 = 6.25; |
||||
|
coeff2 = 4*this->spreadingFactor; |
||||
|
coeff3 = 4*this->spreadingFactor; |
||||
|
} else if(this->spreadingFactor < 11) { |
||||
|
// SF7. SF8, SF9, SF10
|
||||
|
coeff1 = 4.25; |
||||
|
coeff2 = 4*this->spreadingFactor + 8; |
||||
|
coeff3 = 4*this->spreadingFactor; |
||||
|
} else { |
||||
|
// SF11, SF12
|
||||
|
coeff1 = 4.25; |
||||
|
coeff2 = 4*this->spreadingFactor + 8; |
||||
|
coeff3 = 4*(this->spreadingFactor - 2); |
||||
|
} |
||||
|
|
||||
|
// get CRC length
|
||||
|
int16_t N_bitCRC = 16; |
||||
|
if(this->crcTypeLoRa == RADIOLIB_LR11X0_LORA_CRC_DISABLED) { |
||||
|
N_bitCRC = 0; |
||||
|
} |
||||
|
|
||||
|
// get header length
|
||||
|
int16_t N_symbolHeader = 20; |
||||
|
if(this->headerType == RADIOLIB_LR11X0_LORA_HEADER_IMPLICIT) { |
||||
|
N_symbolHeader = 0; |
||||
|
} |
||||
|
|
||||
|
// calculate number of LoRa preamble symbols - NO! Lora preamble is already in symbols
|
||||
|
// uint32_t N_symbolPreamble = (this->preambleLengthLoRa & 0x0F) * (uint32_t(1) << ((this->preambleLengthLoRa & 0xF0) >> 4));
|
||||
|
|
||||
|
// calculate the number of symbols - nope
|
||||
|
// N_symbol = (float)N_symbolPreamble + coeff1 + 8.0f + ceilf((float)RADIOLIB_MAX((int16_t)(8 * len + N_bitCRC - coeff2 + N_symbolHeader), (int16_t)0) / (float)coeff3) * (float)(this->codingRate + 4);
|
||||
|
// calculate the number of symbols - using only preamblelora because it's already in symbols
|
||||
|
N_symbol = (float)preambleLengthLoRa + coeff1 + 8.0f + ceilf((float)RADIOLIB_MAX((int16_t)(8 * len + N_bitCRC - coeff2 + N_symbolHeader), (int16_t)0) / (float)coeff3) * (float)(this->codingRate + 4); |
||||
|
} else { |
||||
|
// long interleaving - not needed for this modem
|
||||
|
} |
||||
|
|
||||
|
// get time-on-air in us
|
||||
|
return(((uint32_t(1) << this->spreadingFactor) / this->bandwidthKhz) * N_symbol * 1000.0f); |
||||
|
} |
||||
|
|
||||
|
bool isReceiving() { |
||||
|
uint16_t irq = getIrqStatus(); |
||||
|
bool detected = ((irq & LR1110_IRQ_HEADER_VALID) || (irq & LR1110_IRQ_HAS_PREAMBLE)); |
||||
|
return detected; |
||||
|
} |
||||
|
}; |
||||
@ -0,0 +1,91 @@ |
|||||
|
#include <Arduino.h> |
||||
|
#include "MinewsemiME25LS01Board.h" |
||||
|
#include <Wire.h> |
||||
|
|
||||
|
#include <bluefruit.h> |
||||
|
|
||||
|
void MinewsemiME25LS01Board::begin() { |
||||
|
// for future use, sub-classes SHOULD call this from their begin()
|
||||
|
startup_reason = BD_STARTUP_NORMAL; |
||||
|
btn_prev_state = HIGH; |
||||
|
|
||||
|
pinMode(PIN_VBAT_READ, INPUT); |
||||
|
|
||||
|
sd_power_mode_set(NRF_POWER_MODE_LOWPWR); |
||||
|
|
||||
|
#ifdef BUTTON_PIN |
||||
|
pinMode(BUTTON_PIN, INPUT); |
||||
|
pinMode(LED_PIN, OUTPUT); |
||||
|
#endif |
||||
|
|
||||
|
#if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL) |
||||
|
Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL); |
||||
|
#endif |
||||
|
|
||||
|
Wire.begin(); |
||||
|
|
||||
|
#ifdef P_LORA_TX_LED |
||||
|
pinMode(P_LORA_TX_LED, OUTPUT); |
||||
|
digitalWrite(P_LORA_TX_LED, LOW); |
||||
|
#endif |
||||
|
|
||||
|
delay(10); // give sx1262 some time to power up
|
||||
|
} |
||||
|
|
||||
|
static BLEDfu bledfu; |
||||
|
|
||||
|
static void connect_callback(uint16_t conn_handle) { |
||||
|
(void)conn_handle; |
||||
|
MESH_DEBUG_PRINTLN("BLE client connected"); |
||||
|
} |
||||
|
|
||||
|
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) { |
||||
|
(void)conn_handle; |
||||
|
(void)reason; |
||||
|
|
||||
|
MESH_DEBUG_PRINTLN("BLE client disconnected"); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
bool MinewsemiME25LS01Board::startOTAUpdate(const char* id, char reply[]) { |
||||
|
// Config the peripheral connection with maximum bandwidth
|
||||
|
// more SRAM required by SoftDevice
|
||||
|
// Note: All config***() function must be called before begin()
|
||||
|
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
||||
|
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16); |
||||
|
|
||||
|
Bluefruit.begin(1, 0); |
||||
|
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
|
Bluefruit.setTxPower(4); |
||||
|
// Set the BLE device name
|
||||
|
Bluefruit.setName("Minewsemi_OTA"); |
||||
|
|
||||
|
Bluefruit.Periph.setConnectCallback(connect_callback); |
||||
|
Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
||||
|
|
||||
|
// To be consistent OTA DFU should be added first if it exists
|
||||
|
bledfu.begin(); |
||||
|
|
||||
|
// Set up and start advertising
|
||||
|
// Advertising packet
|
||||
|
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
||||
|
Bluefruit.Advertising.addTxPower(); |
||||
|
Bluefruit.Advertising.addName(); |
||||
|
|
||||
|
/* Start Advertising
|
||||
|
- Enable auto advertising if disconnected |
||||
|
- Interval: fast mode = 20 ms, slow mode = 152.5 ms |
||||
|
- Timeout for fast mode is 30 seconds |
||||
|
- Start(timeout) with timeout = 0 will advertise forever (until connected) |
||||
|
|
||||
|
For recommended advertising interval |
||||
|
https://developer.apple.com/library/content/qa/qa1931/_index.html
|
||||
|
*/ |
||||
|
Bluefruit.Advertising.restartOnDisconnect(true); |
||||
|
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
||||
|
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
||||
|
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
|
||||
|
|
||||
|
strcpy(reply, "OK - started"); |
||||
|
return true; |
||||
|
} |
||||
@ -0,0 +1,88 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <MeshCore.h> |
||||
|
#include <Arduino.h> |
||||
|
|
||||
|
// LoRa and SPI pins
|
||||
|
|
||||
|
#define P_LORA_DIO_1 (32 + 12) // P1.12
|
||||
|
#define P_LORA_NSS (32 + 13) // P1.13
|
||||
|
#define P_LORA_RESET (32 + 11) // P1.11
|
||||
|
#define P_LORA_BUSY (32 + 10) // P1.10
|
||||
|
#define P_LORA_SCLK (32 + 15) // P1.15
|
||||
|
#define P_LORA_MISO (0 + 29) // P0.29
|
||||
|
#define P_LORA_MOSI (0 + 2) // P0.2
|
||||
|
|
||||
|
#define LR11X0_DIO_AS_RF_SWITCH true |
||||
|
#define LR11X0_DIO3_TCXO_VOLTAGE 1.6 |
||||
|
|
||||
|
#define PIN_VBAT_READ BATTERY_PIN |
||||
|
#define ADC_MULTIPLIER (1.815f) // dependent on voltage divider resistors. TODO: more accurate battery tracking
|
||||
|
|
||||
|
|
||||
|
class MinewsemiME25LS01Board : public mesh::MainBoard { |
||||
|
protected: |
||||
|
uint8_t startup_reason; |
||||
|
uint8_t btn_prev_state; |
||||
|
|
||||
|
public: |
||||
|
void begin(); |
||||
|
|
||||
|
#define BATTERY_SAMPLES 8 |
||||
|
|
||||
|
uint16_t getBattMilliVolts() override { |
||||
|
analogReadResolution(12); |
||||
|
|
||||
|
uint32_t raw = 0; |
||||
|
for (int i = 0; i < BATTERY_SAMPLES; i++) { |
||||
|
raw += analogRead(PIN_VBAT_READ); |
||||
|
} |
||||
|
raw = raw / BATTERY_SAMPLES; |
||||
|
return (ADC_MULTIPLIER * raw); |
||||
|
} |
||||
|
|
||||
|
uint8_t getStartupReason() const override { return startup_reason; } |
||||
|
|
||||
|
const char* getManufacturerName() const override { |
||||
|
return "Minewsemi"; |
||||
|
} |
||||
|
|
||||
|
void powerOff() override { |
||||
|
#ifdef HAS_GPS |
||||
|
digitalWrite(GPS_VRTC_EN, LOW); |
||||
|
digitalWrite(GPS_RESET, LOW); |
||||
|
digitalWrite(GPS_SLEEP_INT, LOW); |
||||
|
digitalWrite(GPS_RTC_INT, LOW); |
||||
|
pinMode(GPS_RESETB, OUTPUT); |
||||
|
digitalWrite(GPS_RESETB, LOW); |
||||
|
#endif |
||||
|
|
||||
|
#ifdef BUZZER_EN |
||||
|
digitalWrite(BUZZER_EN, LOW); |
||||
|
#endif |
||||
|
|
||||
|
#ifdef LED_PIN |
||||
|
digitalWrite(LED_PIN, LOW); |
||||
|
#endif |
||||
|
#ifdef BUTTON_PIN |
||||
|
nrf_gpio_cfg_sense_input(digitalPinToInterrupt(BUTTON_PIN), NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_HIGH); |
||||
|
#endif |
||||
|
sd_power_system_off(); |
||||
|
} |
||||
|
|
||||
|
#if defined(P_LORA_TX_LED) |
||||
|
void onBeforeTransmit() override { |
||||
|
digitalWrite(P_LORA_TX_LED, HIGH);// turn TX LED on
|
||||
|
} |
||||
|
void onAfterTransmit() override { |
||||
|
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED off
|
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
|
||||
|
void reboot() override { |
||||
|
NVIC_SystemReset(); |
||||
|
} |
||||
|
|
||||
|
bool startOTAUpdate(const char* id, char reply[]) override; |
||||
|
}; |
||||
@ -0,0 +1,24 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <helpers/ui/DisplayDriver.h> |
||||
|
|
||||
|
class NullDisplayDriver : public DisplayDriver { |
||||
|
public: |
||||
|
NullDisplayDriver() : DisplayDriver(128, 64) { } |
||||
|
bool begin() { return false; } // not present
|
||||
|
|
||||
|
bool isOn() override { return false; } |
||||
|
void turnOn() override { } |
||||
|
void turnOff() override { } |
||||
|
void clear() override { } |
||||
|
void startFrame(Color bkg = DARK) override { } |
||||
|
void setTextSize(int sz) override { } |
||||
|
void setColor(Color c) override { } |
||||
|
void setCursor(int x, int y) override { } |
||||
|
void print(const char* str) override { } |
||||
|
void fillRect(int x, int y, int w, int h) override { } |
||||
|
void drawRect(int x, int y, int w, int h) override { } |
||||
|
void drawXbm(int x, int y, const uint8_t* bits, int w, int h) override { } |
||||
|
uint16_t getTextWidth(const char* str) override { return 0; } |
||||
|
void endFrame() { } |
||||
|
}; |
||||
@ -0,0 +1,164 @@ |
|||||
|
; ----------------- NRF52 me25ls01--------------------- |
||||
|
[nrf52840_me25ls01] |
||||
|
extends = nrf52_base |
||||
|
platform_packages = framework-arduinoadafruitnrf52 |
||||
|
build_flags = ${nrf52_base.build_flags} |
||||
|
-I src/helpers/nrf52 |
||||
|
-I lib/nrf52/s140_nrf52_7.3.0_API/include |
||||
|
-I lib/nrf52/s140_nrf52_7.3.0_API/include/nrf52 |
||||
|
lib_ignore = |
||||
|
BluetoothOTA |
||||
|
lib5b4 |
||||
|
lib_deps = |
||||
|
${nrf52_base.lib_deps} |
||||
|
rweather/Crypto @ ^0.4.0 |
||||
|
|
||||
|
[me25ls01] |
||||
|
extends = nrf52840_me25ls01 |
||||
|
board = minewsemi_me25ls01 |
||||
|
board_build.ldscript = boards/nrf52840_s140_v7.ld |
||||
|
build_flags = ${nrf52840_me25ls01.build_flags} |
||||
|
-I variants/minewsemi_me25ls01 |
||||
|
-D me25ls01 |
||||
|
-D PIN_USER_BTN=27 |
||||
|
-D USER_BTN_PRESSED=HIGH |
||||
|
-D PIN_STATUS_LED=39 |
||||
|
-D P_LORA_TX_LED=22 |
||||
|
-D RADIO_CLASS=CustomLR1110 |
||||
|
-D WRAPPER_CLASS=CustomLR1110Wrapper |
||||
|
-D LORA_TX_POWER=22 |
||||
|
-D ENV_INCLUDE_GPS=0 |
||||
|
-D ENV_INCLUDE_AHTX0=1 |
||||
|
-D ENV_INCLUDE_BME280=1 |
||||
|
-D ENV_INCLUDE_INA3221=1 |
||||
|
-D ENV_INCLUDE_INA219=1 |
||||
|
build_src_filter = ${nrf52840_me25ls01.build_src_filter} |
||||
|
+<helpers/*.cpp> |
||||
|
+<../variants/minewsemi_me25ls01> |
||||
|
+<helpers/sensors> |
||||
|
debug_tool = jlink |
||||
|
upload_protocol = nrfutil |
||||
|
lib_deps = ${nrf52840_me25ls01.lib_deps} |
||||
|
densaugeo/base64 @ ~1.4.0 |
||||
|
stevemarple/MicroNMEA @ ^2.0.6 |
||||
|
end2endzone/NonBlockingRTTTL@^1.3.0 |
||||
|
adafruit/Adafruit SSD1306 @ ^2.5.13 |
||||
|
adafruit/Adafruit INA3221 Library @ ^1.0.1 |
||||
|
adafruit/Adafruit INA219 @ ^1.2.3 |
||||
|
adafruit/Adafruit AHTX0 @ ^2.0.5 |
||||
|
adafruit/Adafruit BME280 Library @ ^2.3.0 |
||||
|
|
||||
|
[env:Minewsemi_me25ls01_companion_radio_ble] |
||||
|
extends = me25ls01 |
||||
|
build_flags = ${me25ls01.build_flags} |
||||
|
-D MAX_CONTACTS=100 |
||||
|
-D MAX_GROUP_CHANNELS=8 |
||||
|
-D BLE_PIN_CODE=123456 |
||||
|
; -D BLE_DEBUG_LOGGING=1 |
||||
|
-D MESH_PACKET_LOGGING=1 |
||||
|
-D MESH_DEBUG=1 |
||||
|
-D OFFLINE_QUEUE_SIZE=256 |
||||
|
-D RX_BOOSTED_GAIN=true |
||||
|
-D RF_SWITCH_TABLE |
||||
|
-D DISPLAY_CLASS=NullDisplayDriver |
||||
|
;-D PIN_BUZZER=25 |
||||
|
;-D PIN_BUZZER_EN=37 |
||||
|
build_src_filter = ${me25ls01.build_src_filter} |
||||
|
+<helpers/nrf52/SerialBLEInterface.cpp> |
||||
|
+<../examples/companion_radio/*.cpp> |
||||
|
lib_deps = ${me25ls01.lib_deps} |
||||
|
adafruit/RTClib @ ^2.1.3 |
||||
|
|
||||
|
|
||||
|
[env:Minewsemi_me25ls01_repeater] |
||||
|
extends = me25ls01 |
||||
|
build_flags = ${me25ls01.build_flags} |
||||
|
-D MAX_CONTACTS=100 |
||||
|
-D MAX_GROUP_CHANNELS=8 |
||||
|
-D BLE_PIN_CODE=123456 |
||||
|
; -D BLE_DEBUG_LOGGING=1 |
||||
|
-D MESH_PACKET_LOGGING=1 |
||||
|
-D MESH_DEBUG=1 |
||||
|
-D OFFLINE_QUEUE_SIZE=256 |
||||
|
-D RX_BOOSTED_GAIN=true |
||||
|
-D RF_SWITCH_TABLE |
||||
|
-D ADVERT_NAME='"ME25LS01 Repeater"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
-D MAX_NEIGHBOURS=8 |
||||
|
-D DISPLAY_CLASS=NullDisplayDriver |
||||
|
build_src_filter = ${me25ls01.build_src_filter} |
||||
|
+<../examples/simple_repeater> |
||||
|
lib_deps = ${me25ls01.lib_deps} |
||||
|
adafruit/RTClib @ ^2.1.3 |
||||
|
|
||||
|
|
||||
|
|
||||
|
[env:Minewsemi_me25ls01_room_server] |
||||
|
extends = me25ls01 |
||||
|
build_flags = ${me25ls01.build_flags} |
||||
|
-D MAX_CONTACTS=100 |
||||
|
-D MAX_GROUP_CHANNELS=8 |
||||
|
; -D BLE_PIN_CODE=123456 |
||||
|
; -D BLE_DEBUG_LOGGING=1 |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
; -D MESH_DEBUG=1 |
||||
|
-D OFFLINE_QUEUE_SIZE=256 |
||||
|
-D RX_BOOSTED_GAIN=true |
||||
|
-D RF_SWITCH_TABLE |
||||
|
-D ADVERT_NAME='"ME25LS01 Room"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
-D ROOM_PASSWORD='"hello"' |
||||
|
-D MAX_NEIGHBOURS=8 |
||||
|
-D DISPLAY_CLASS=NullDisplayDriver |
||||
|
build_src_filter = ${me25ls01.build_src_filter} |
||||
|
+<../examples/simple_room_server> |
||||
|
lib_deps = ${me25ls01.lib_deps} |
||||
|
adafruit/RTClib @ ^2.1.3 |
||||
|
|
||||
|
[env:Minewsemi_me25ls01_terminal_chat] |
||||
|
extends = me25ls01 |
||||
|
build_flags = ${me25ls01.build_flags} |
||||
|
-D MAX_CONTACTS=100 |
||||
|
-D MAX_GROUP_CHANNELS=8 |
||||
|
-D BLE_PIN_CODE=123456 |
||||
|
; -D BLE_DEBUG_LOGGING=1 |
||||
|
-D MESH_PACKET_LOGGING=1 |
||||
|
-D MESH_DEBUG=1 |
||||
|
-D OFFLINE_QUEUE_SIZE=256 |
||||
|
-D RX_BOOSTED_GAIN=true |
||||
|
-D RF_SWITCH_TABLE |
||||
|
-D ADVERT_NAME='"ME25LS01 Chat"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
-D ROOM_PASSWORD='"hello"' |
||||
|
-D MAX_NEIGHBOURS=8 |
||||
|
-D DISPLAY_CLASS=NullDisplayDriver |
||||
|
build_src_filter = ${me25ls01.build_src_filter} |
||||
|
+<../examples/simple_secure_chat/main.cpp> |
||||
|
lib_deps = ${me25ls01.lib_deps} |
||||
|
adafruit/RTClib @ ^2.1.3 |
||||
|
|
||||
|
[env:Minewsemi_me25ls01_companion_radio_usb] |
||||
|
extends = me25ls01 |
||||
|
build_flags = ${me25ls01.build_flags} |
||||
|
-D MAX_CONTACTS=100 |
||||
|
-D MAX_GROUP_CHANNELS=8 |
||||
|
;-D BLE_PIN_CODE=123456 |
||||
|
; -D BLE_DEBUG_LOGGING=1 |
||||
|
-D MESH_PACKET_LOGGING=1 |
||||
|
-D MESH_DEBUG=1 |
||||
|
-D OFFLINE_QUEUE_SIZE=256 |
||||
|
-D RX_BOOSTED_GAIN=true |
||||
|
-D RF_SWITCH_TABLE |
||||
|
-D DISPLAY_CLASS=NullDisplayDriver |
||||
|
build_src_filter = ${me25ls01.build_src_filter} |
||||
|
+<helpers/nrf52/*.cpp> |
||||
|
+<../examples/companion_radio> |
||||
|
lib_deps = ${me25ls01.lib_deps} |
||||
|
adafruit/RTClib @ ^2.1.3 |
||||
|
|
||||
@ -0,0 +1,98 @@ |
|||||
|
#include <Arduino.h> |
||||
|
#include "target.h" |
||||
|
|
||||
|
MinewsemiME25LS01Board board; |
||||
|
|
||||
|
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI); |
||||
|
|
||||
|
WRAPPER_CLASS radio_driver(radio, board); |
||||
|
|
||||
|
VolatileRTCClock rtc_clock; |
||||
|
extern EnvironmentSensorManager sensors; |
||||
|
#if ENV_INCLUDE_GPS |
||||
|
#include <helpers/sensors/MicroNMEALocationProvider.h> |
||||
|
MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock); |
||||
|
EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); |
||||
|
#else |
||||
|
EnvironmentSensorManager sensors; |
||||
|
#endif |
||||
|
|
||||
|
#ifdef DISPLAY_CLASS |
||||
|
NullDisplayDriver display; |
||||
|
#endif |
||||
|
|
||||
|
#ifndef LORA_CR |
||||
|
#define LORA_CR 5 |
||||
|
#endif |
||||
|
|
||||
|
#ifdef RF_SWITCH_TABLE |
||||
|
static const uint32_t rfswitch_dios[Module::RFSWITCH_MAX_PINS] = { |
||||
|
RADIOLIB_LR11X0_DIO5, |
||||
|
RADIOLIB_LR11X0_DIO6, |
||||
|
RADIOLIB_LR11X0_DIO7, |
||||
|
RADIOLIB_LR11X0_DIO8, |
||||
|
RADIOLIB_NC |
||||
|
}; |
||||
|
|
||||
|
static const Module::RfSwitchMode_t rfswitch_table[] = { |
||||
|
// mode DIO5 DIO6 DIO7 DIO8
|
||||
|
{ LR11x0::MODE_STBY, {LOW, LOW, LOW, LOW }}, |
||||
|
{ LR11x0::MODE_RX, {HIGH, LOW, LOW, HIGH }}, |
||||
|
{ LR11x0::MODE_TX, {HIGH, HIGH, LOW, HIGH }}, |
||||
|
{ LR11x0::MODE_TX_HP, {LOW, HIGH, LOW, HIGH }}, |
||||
|
{ LR11x0::MODE_TX_HF, {LOW, LOW, LOW, LOW }}, |
||||
|
{ LR11x0::MODE_GNSS, {LOW, LOW, HIGH, LOW }}, |
||||
|
{ LR11x0::MODE_WIFI, {LOW, LOW, LOW, LOW }}, |
||||
|
END_OF_MODE_TABLE, |
||||
|
}; |
||||
|
#endif |
||||
|
|
||||
|
bool radio_init() { |
||||
|
//rtc_clock.begin(Wire);
|
||||
|
|
||||
|
#ifdef LR11X0_DIO3_TCXO_VOLTAGE |
||||
|
float tcxo = LR11X0_DIO3_TCXO_VOLTAGE; |
||||
|
#else |
||||
|
float tcxo = 1.6f; |
||||
|
#endif |
||||
|
|
||||
|
SPI.setPins(P_LORA_MISO, P_LORA_SCLK, P_LORA_MOSI); |
||||
|
SPI.begin(); |
||||
|
int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_LR11X0_LORA_SYNC_WORD_PRIVATE, LORA_TX_POWER, 16, tcxo); |
||||
|
if (status != RADIOLIB_ERR_NONE) { |
||||
|
Serial.print("ERROR: radio init failed: "); |
||||
|
Serial.println(status); |
||||
|
return false; // fail
|
||||
|
} |
||||
|
|
||||
|
radio.setCRC(1); |
||||
|
|
||||
|
#ifdef RF_SWITCH_TABLE |
||||
|
radio.setRfSwitchTable(rfswitch_dios, rfswitch_table); |
||||
|
#endif |
||||
|
#ifdef RX_BOOSTED_GAIN |
||||
|
radio.setRxBoostedGainMode(RX_BOOSTED_GAIN); |
||||
|
#endif |
||||
|
|
||||
|
return true; // success
|
||||
|
} |
||||
|
|
||||
|
uint32_t radio_get_rng_seed() { |
||||
|
return radio.random(0x7FFFFFFF); |
||||
|
} |
||||
|
|
||||
|
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) { |
||||
|
radio.setFrequency(freq); |
||||
|
radio.setSpreadingFactor(sf); |
||||
|
radio.setBandwidth(bw); |
||||
|
radio.setCodingRate(cr); |
||||
|
} |
||||
|
|
||||
|
void radio_set_tx_power(uint8_t dbm) { |
||||
|
radio.setOutputPower(dbm); |
||||
|
} |
||||
|
|
||||
|
mesh::LocalIdentity radio_new_identity() { |
||||
|
RadioNoiseListener rng(radio); |
||||
|
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#define RADIOLIB_STATIC_ONLY 1 |
||||
|
#include <RadioLib.h> |
||||
|
#include <helpers/radiolib/RadioLibWrappers.h> |
||||
|
#include <MinewsemiME25LS01Board.h> |
||||
|
#include <helpers/radiolib/CustomLR1110Wrapper.h> |
||||
|
#include <helpers/ArduinoHelpers.h> |
||||
|
#include <helpers/SensorManager.h> |
||||
|
#include <helpers/sensors/LocationProvider.h> |
||||
|
#include <helpers/sensors/EnvironmentSensorManager.h> |
||||
|
#ifdef DISPLAY_CLASS |
||||
|
#include "NullDisplayDriver.h" |
||||
|
#endif |
||||
|
|
||||
|
#ifdef DISPLAY_CLASS |
||||
|
extern NullDisplayDriver display; |
||||
|
#endif |
||||
|
|
||||
|
extern MinewsemiME25LS01Board board; |
||||
|
extern WRAPPER_CLASS radio_driver; |
||||
|
extern VolatileRTCClock rtc_clock; |
||||
|
extern EnvironmentSensorManager sensors; |
||||
|
|
||||
|
bool radio_init(); |
||||
|
uint32_t radio_get_rng_seed(); |
||||
|
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr); |
||||
|
void radio_set_tx_power(uint8_t dbm); |
||||
|
mesh::LocalIdentity radio_new_identity(); |
||||
@ -0,0 +1,70 @@ |
|||||
|
#include "variant.h" |
||||
|
#include "wiring_constants.h" |
||||
|
#include "wiring_digital.h" |
||||
|
|
||||
|
const uint32_t g_ADigitalPinMap[PINS_COUNT + 1] = |
||||
|
{ |
||||
|
0, // P0.00
|
||||
|
1, // P0.01
|
||||
|
2, // P0.02
|
||||
|
3, // P0.03
|
||||
|
4, // P0.04
|
||||
|
5, // P0.05
|
||||
|
6, // P0.06
|
||||
|
7, // P0.07
|
||||
|
8, // P0.08
|
||||
|
9, // P0.09
|
||||
|
10, // P0.10
|
||||
|
11, // P0.11
|
||||
|
12, // P0.12
|
||||
|
13, // P0.13, PIN_SERIAL1_TX
|
||||
|
14, // P0.14, PIN_SERIAL1_RX
|
||||
|
15, // P0.15, PIN_SERIAL2_RX
|
||||
|
16, // P0.16, PIN_WIRE_SCL
|
||||
|
17, // P0.17, PIN_SERIAL2_TX
|
||||
|
18, // P0.18
|
||||
|
19, // P0.19
|
||||
|
20, // P0.20
|
||||
|
21, // P0.21, PIN_WIRE_SDA
|
||||
|
22, // P0.22
|
||||
|
23, // P0.23
|
||||
|
24, // P0.24,
|
||||
|
25, // P0.25,
|
||||
|
26, // P0.26,
|
||||
|
27, // P0.27,
|
||||
|
28, // P0.28
|
||||
|
29, // P0.29,
|
||||
|
30, // P0.30
|
||||
|
31, // P0.31, BATTERY_PIN
|
||||
|
32, // P1.00
|
||||
|
33, // P1.01, LORA_DIO_1
|
||||
|
34, // P1.02
|
||||
|
35, // P1.03,
|
||||
|
36, // P1.04
|
||||
|
37, // P1.05, LR1110_EN
|
||||
|
38, // P1.06,
|
||||
|
39, // P1.07,
|
||||
|
40, // P1.08, PIN_SPI_MISO
|
||||
|
41, // P1.09, PIN_SPI_MOSI
|
||||
|
42, // P1.10, LORA_RESET
|
||||
|
43, // P1.11, GPS_EN
|
||||
|
44, // P1.12, GPS_SLEEP_INT
|
||||
|
45, // P1.13
|
||||
|
46, // P1.14, GPS_RESETB
|
||||
|
47, // P1.15, PIN_GPS_RESET
|
||||
|
255, // NRFX_SPIM_PIN_NOT_USED
|
||||
|
}; |
||||
|
|
||||
|
void initVariant() |
||||
|
{ |
||||
|
pinMode(BATTERY_PIN, INPUT); |
||||
|
pinMode(PIN_BUTTON1, INPUT); |
||||
|
|
||||
|
// pinMode(PIN_3V3_EN, OUTPUT);
|
||||
|
// pinMode(PIN_3V3_ACC_EN, OUTPUT);
|
||||
|
pinMode(LED_PIN, OUTPUT); |
||||
|
pinMode(P_LORA_TX_LED, OUTPUT); |
||||
|
|
||||
|
digitalWrite(LED_PIN, HIGH); |
||||
|
digitalWrite(P_LORA_TX_LED, LOW); |
||||
|
} |
||||
@ -0,0 +1,94 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include "WVariant.h" |
||||
|
|
||||
|
// Low frequency clock source
|
||||
|
#define USE_LFXO // 32.768 kHz crystal oscillator
|
||||
|
#define VARIANT_MCK (64000000ul) |
||||
|
// #define USE_LFRC // 32.768 kHz RC oscillator
|
||||
|
|
||||
|
// Power
|
||||
|
#define BATTERY_PIN (31) |
||||
|
#define BATTERY_IMMUTABLE |
||||
|
#define ADC_MULTIPLIER (2.0F) |
||||
|
|
||||
|
#define ADC_RESOLUTION (14) |
||||
|
#define BATTERY_SENSE_RES (12) |
||||
|
|
||||
|
// Number of pins
|
||||
|
#define PINS_COUNT (48) |
||||
|
#define NUM_DIGITAL_PINS (48) |
||||
|
#define NUM_ANALOG_INPUTS (6) |
||||
|
#define NUM_ANALOG_OUTPUTS (0) |
||||
|
|
||||
|
// UART pin definition
|
||||
|
#define PIN_SERIAL1_RX (14) // P0.14
|
||||
|
#define PIN_SERIAL1_TX (13) // P0.13
|
||||
|
|
||||
|
#define PIN_SERIAL2_RX (15) // P0.15
|
||||
|
#define PIN_SERIAL2_TX (17) // P0.17
|
||||
|
|
||||
|
// I2C pin definition
|
||||
|
#define HAS_WIRE (1) |
||||
|
#define WIRE_INTERFACES_COUNT (1) |
||||
|
|
||||
|
#define PIN_WIRE_SDA (21) // P0.21
|
||||
|
#define PIN_WIRE_SCL (16) // P0.16
|
||||
|
#define I2C_NO_RESCAN |
||||
|
|
||||
|
// SPI pin definition
|
||||
|
#define SPI_INTERFACES_COUNT (1) |
||||
|
|
||||
|
#define PIN_SPI_MISO (0 + 29) // P0.29
|
||||
|
#define PIN_SPI_MOSI (0 + 2) // P0.2
|
||||
|
#define PIN_SPI_SCK (32 + 15) // P1.15
|
||||
|
#define PIN_SPI_NSS (32 + 13) // P1.13
|
||||
|
|
||||
|
// Builtin LEDs
|
||||
|
#define LED_BUILTIN (-1) |
||||
|
#define LED_RED (32 + 5) // P1.5
|
||||
|
#define LED_BLUE (32 + 7) // P1.7
|
||||
|
#define LED_PIN LED_BLUE |
||||
|
#define P_LORA_TX_LED LED_RED |
||||
|
|
||||
|
#define LED_STATE_ON HIGH |
||||
|
|
||||
|
// Builtin buttons
|
||||
|
|
||||
|
#define PIN_BUTTON1 (0 + 27) // P0.6
|
||||
|
#define BUTTON_PIN PIN_BUTTON1 |
||||
|
|
||||
|
// LR1110
|
||||
|
#define LORA_DIO_1 (32 + 12) // P1.12
|
||||
|
#define LORA_DIO_2 (32 + 10) // P1.10
|
||||
|
#define LORA_NSS (PIN_SPI_NSS) // P1.13
|
||||
|
#define LORA_RESET (32 + 11) // P1.11
|
||||
|
#define LORA_BUSY (32 + 10) // P1.10
|
||||
|
#define LORA_SCLK (PIN_SPI_SCK) // P1.15
|
||||
|
#define LORA_MISO (PIN_SPI_MISO) // P0.29
|
||||
|
#define LORA_MOSI (PIN_SPI_MOSI) // P0.2
|
||||
|
#define LORA_CS PIN_SPI_NSS // P1.13
|
||||
|
|
||||
|
#define LR11X0_DIO_AS_RF_SWITCH true |
||||
|
#define LR11X0_DIO3_TCXO_VOLTAGE 1.6 |
||||
|
|
||||
|
#define LR1110_IRQ_PIN LORA_DIO_1 |
||||
|
#define LR1110_NRESET_PIN LORA_RESET |
||||
|
#define LR1110_BUSY_PIN LORA_DIO_2 |
||||
|
#define LR1110_SPI_NSS_PIN LORA_CS |
||||
|
#define LR1110_SPI_SCK_PIN LORA_SCLK |
||||
|
#define LR1110_SPI_MOSI_PIN LORA_MOSI |
||||
|
#define LR1110_SPI_MISO_PIN LORA_MISO |
||||
|
|
||||
|
// GPS
|
||||
|
#define HAS_GPS 0 |
||||
|
#define GPS_RX_PIN PIN_SERIAL1_RX |
||||
|
#define GPS_TX_PIN PIN_SERIAL1_TX |
||||
|
|
||||
|
#define GPS_EN (-1) // P1.11
|
||||
|
#define GPS_RESET (-1) // P1.15
|
||||
|
|
||||
|
#define GPS_VRTC_EN (-1) // P0.8
|
||||
|
#define GPS_SLEEP_INT (-1) // P1.12
|
||||
|
#define GPS_RTC_INT (-1) // P0.15
|
||||
|
#define GPS_RESETB (-1) // P1.14
|
||||
@ -0,0 +1,81 @@ |
|||||
|
#include <Arduino.h> |
||||
|
#include "SenseCapSolarBoard.h" |
||||
|
|
||||
|
#include <bluefruit.h> |
||||
|
#include <Wire.h> |
||||
|
|
||||
|
static BLEDfu bledfu; |
||||
|
|
||||
|
static void connect_callback(uint16_t conn_handle) { |
||||
|
(void)conn_handle; |
||||
|
MESH_DEBUG_PRINTLN("BLE client connected"); |
||||
|
} |
||||
|
|
||||
|
static void disconnect_callback(uint16_t conn_handle, uint8_t reason) { |
||||
|
(void)conn_handle; |
||||
|
(void)reason; |
||||
|
|
||||
|
MESH_DEBUG_PRINTLN("BLE client disconnected"); |
||||
|
} |
||||
|
|
||||
|
void SenseCapSolarBoard::begin() { |
||||
|
// for future use, sub-classes SHOULD call this from their begin()
|
||||
|
startup_reason = BD_STARTUP_NORMAL; |
||||
|
|
||||
|
#if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL) |
||||
|
Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL); |
||||
|
#endif |
||||
|
|
||||
|
Wire.begin(); |
||||
|
|
||||
|
#ifdef P_LORA_TX_LED |
||||
|
pinMode(P_LORA_TX_LED, OUTPUT); |
||||
|
digitalWrite(P_LORA_TX_LED, LOW); |
||||
|
#endif |
||||
|
|
||||
|
delay(10); // give sx1262 some time to power up
|
||||
|
} |
||||
|
|
||||
|
bool SenseCapSolarBoard::startOTAUpdate(const char* id, char reply[]) { |
||||
|
// Config the peripheral connection with maximum bandwidth
|
||||
|
// more SRAM required by SoftDevice
|
||||
|
// Note: All config***() function must be called before begin()
|
||||
|
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
||||
|
Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16); |
||||
|
|
||||
|
Bluefruit.begin(1, 0); |
||||
|
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
|
||||
|
Bluefruit.setTxPower(4); |
||||
|
// Set the BLE device name
|
||||
|
Bluefruit.setName("SENSECAP_SOLAR_OTA"); |
||||
|
|
||||
|
Bluefruit.Periph.setConnectCallback(connect_callback); |
||||
|
Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
||||
|
|
||||
|
// To be consistent OTA DFU should be added first if it exists
|
||||
|
bledfu.begin(); |
||||
|
|
||||
|
// Set up and start advertising
|
||||
|
// Advertising packet
|
||||
|
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
||||
|
Bluefruit.Advertising.addTxPower(); |
||||
|
Bluefruit.Advertising.addName(); |
||||
|
|
||||
|
/* Start Advertising
|
||||
|
- Enable auto advertising if disconnected |
||||
|
- Interval: fast mode = 20 ms, slow mode = 152.5 ms |
||||
|
- Timeout for fast mode is 30 seconds |
||||
|
- Start(timeout) with timeout = 0 will advertise forever (until connected) |
||||
|
|
||||
|
For recommended advertising interval |
||||
|
https://developer.apple.com/library/content/qa/qa1931/_index.html
|
||||
|
*/ |
||||
|
Bluefruit.Advertising.restartOnDisconnect(true); |
||||
|
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
|
||||
|
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
|
||||
|
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
|
||||
|
|
||||
|
strcpy(reply, "OK - started"); |
||||
|
|
||||
|
return true; |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <MeshCore.h> |
||||
|
#include <Arduino.h> |
||||
|
|
||||
|
class SenseCapSolarBoard : public mesh::MainBoard { |
||||
|
protected: |
||||
|
uint8_t startup_reason; |
||||
|
|
||||
|
public: |
||||
|
void begin(); |
||||
|
uint8_t getStartupReason() const override { return startup_reason; } |
||||
|
|
||||
|
#if defined(P_LORA_TX_LED) |
||||
|
void onBeforeTransmit() override { |
||||
|
digitalWrite(P_LORA_TX_LED, HIGH); // turn TX LED on
|
||||
|
} |
||||
|
void onAfterTransmit() override { |
||||
|
digitalWrite(P_LORA_TX_LED, LOW); // turn TX LED off
|
||||
|
} |
||||
|
#endif |
||||
|
|
||||
|
uint16_t getBattMilliVolts() override { |
||||
|
digitalWrite(VBAT_ENABLE, LOW); |
||||
|
int adcvalue = 0; |
||||
|
analogReadResolution(12); |
||||
|
analogReference(AR_INTERNAL_3_0); |
||||
|
delay(10); |
||||
|
adcvalue = analogRead(BATTERY_PIN); |
||||
|
return (adcvalue * ADC_MULTIPLIER * AREF_VOLTAGE) / 4.096; |
||||
|
} |
||||
|
|
||||
|
const char* getManufacturerName() const override { |
||||
|
return "Seeed SenseCap Solar"; |
||||
|
} |
||||
|
|
||||
|
void reboot() override { |
||||
|
NVIC_SystemReset(); |
||||
|
} |
||||
|
|
||||
|
bool startOTAUpdate(const char* id, char reply[]) override; |
||||
|
}; |
||||
@ -0,0 +1,106 @@ |
|||||
|
[SenseCap_Solar] |
||||
|
extends = nrf52_base |
||||
|
board = seeed_sensecap_solar |
||||
|
board_build.ldscript = boards/nrf52840_s140_v7.ld |
||||
|
build_flags = ${nrf52_base.build_flags} |
||||
|
-I lib/nrf52/s140_nrf52_7.3.0_API/include |
||||
|
-I lib/nrf52/s140_nrf52_7.3.0_API/include/nrf52 |
||||
|
-I variants/sensecap_solar |
||||
|
-I src/helpers/nrf52 |
||||
|
-D NRF52_PLATFORM=1 |
||||
|
-D RADIO_CLASS=CustomSX1262 |
||||
|
-D WRAPPER_CLASS=CustomSX1262Wrapper |
||||
|
-D P_LORA_TX_LED=12 |
||||
|
-D P_LORA_DIO_1=1 |
||||
|
-D P_LORA_RESET=2 |
||||
|
-D P_LORA_BUSY=3 |
||||
|
-D P_LORA_NSS=4 |
||||
|
-D LORA_TX_POWER=22 |
||||
|
-D SX126X_RXEN=5 |
||||
|
-D SX126X_TXEN=RADIOLIB_NC |
||||
|
-D SX126X_DIO2_AS_RF_SWITCH=1 |
||||
|
-D SX126X_DIO3_TCXO_VOLTAGE=1.8 |
||||
|
-D SX126X_CURRENT_LIMIT=140 |
||||
|
-D SX126X_RX_BOOSTED_GAIN=1 |
||||
|
-D ENV_INCLUDE_AHTX0=1 |
||||
|
-D ENV_INCLUDE_BME280=1 |
||||
|
-D ENV_INCLUDE_BMP280=1 |
||||
|
-D ENV_INCLUDE_SHTC3=1 |
||||
|
-D ENV_INCLUDE_LPS22HB=1 |
||||
|
-D ENV_INCLUDE_INA3221=1 |
||||
|
-D ENV_INCLUDE_INA219=1 |
||||
|
build_src_filter = ${nrf52_base.build_src_filter} |
||||
|
+<helpers/*.cpp> |
||||
|
+<helpers/sensors> |
||||
|
+<../variants/sensecap_solar> |
||||
|
debug_tool = jlink |
||||
|
upload_protocol = nrfutil |
||||
|
lib_deps = |
||||
|
${nrf52_base.lib_deps} |
||||
|
adafruit/Adafruit INA3221 Library @ ^1.0.1 |
||||
|
adafruit/Adafruit INA219 @ ^1.2.3 |
||||
|
adafruit/Adafruit AHTX0 @ ^2.0.5 |
||||
|
adafruit/Adafruit BME280 Library @ ^2.3.0 |
||||
|
adafruit/Adafruit BMP280 Library @ ^2.6.8 |
||||
|
adafruit/Adafruit SHTC3 Library @ ^1.0.1 |
||||
|
arduino-libraries/Arduino_LPS22HB @ ^1.0.2 |
||||
|
|
||||
|
[env:SenseCap_Solar_repeater] |
||||
|
extends = SenseCap_Solar |
||||
|
build_flags = |
||||
|
${SenseCap_Solar.build_flags} |
||||
|
-D ADVERT_NAME='"SenseCap_Solar Repeater"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
-D MAX_NEIGHBOURS=8 |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
; -D MESH_DEBUG=1 |
||||
|
build_src_filter = ${SenseCap_Solar.build_src_filter} |
||||
|
+<../examples/simple_repeater/main.cpp> |
||||
|
|
||||
|
[env:SenseCap_Solar_room_server] |
||||
|
extends = SenseCap_Solar |
||||
|
build_flags = |
||||
|
${SenseCap_Solar.build_flags} |
||||
|
-D ADVERT_NAME='"SenseCap_Solar Room"' |
||||
|
-D ADVERT_LAT=0.0 |
||||
|
-D ADVERT_LON=0.0 |
||||
|
-D ADMIN_PASSWORD='"password"' |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
; -D MESH_DEBUG=1 |
||||
|
build_src_filter = ${SenseCap_Solar.build_src_filter} |
||||
|
+<../examples/simple_room_server/main.cpp> |
||||
|
|
||||
|
[env:SenseCap_Solar_companion_radio_ble] |
||||
|
extends = SenseCap_Solar |
||||
|
build_flags = |
||||
|
${SenseCap_Solar.build_flags} |
||||
|
-D MAX_CONTACTS=100 |
||||
|
-D MAX_GROUP_CHANNELS=8 |
||||
|
-D BLE_PIN_CODE=123456 |
||||
|
-D OFFLINE_QUEUE_SIZE=256 |
||||
|
; -D BLE_DEBUG_LOGGING=1 |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
; -D MESH_DEBUG=1 |
||||
|
build_src_filter = ${SenseCap_Solar.build_src_filter} |
||||
|
+<helpers/nrf52/SerialBLEInterface.cpp> |
||||
|
+<../examples/companion_radio> |
||||
|
lib_deps = |
||||
|
${SenseCap_Solar.lib_deps} |
||||
|
densaugeo/base64 @ ~1.4.0 |
||||
|
|
||||
|
[env:SenseCap_Solar_companion_radio_usb] |
||||
|
extends = SenseCap_Solar |
||||
|
build_flags = |
||||
|
${SenseCap_Solar.build_flags} |
||||
|
-D MAX_CONTACTS=100 |
||||
|
-D MAX_GROUP_CHANNELS=8 |
||||
|
; -D MESH_PACKET_LOGGING=1 |
||||
|
; -D MESH_DEBUG=1 |
||||
|
build_src_filter = ${SenseCap_Solar.build_src_filter} |
||||
|
+<helpers/nrf52/SerialBLEInterface.cpp> |
||||
|
+<../examples/companion_radio> |
||||
|
lib_deps = |
||||
|
${SenseCap_Solar.lib_deps} |
||||
|
densaugeo/base64 @ ~1.4.0 |
||||
@ -0,0 +1,39 @@ |
|||||
|
#include <Arduino.h> |
||||
|
#include "target.h" |
||||
|
#include <helpers/ArduinoHelpers.h> |
||||
|
|
||||
|
SenseCapSolarBoard board; |
||||
|
|
||||
|
RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI); |
||||
|
|
||||
|
WRAPPER_CLASS radio_driver(radio, board); |
||||
|
|
||||
|
VolatileRTCClock fallback_clock; |
||||
|
AutoDiscoverRTCClock rtc_clock(fallback_clock); |
||||
|
EnvironmentSensorManager sensors; |
||||
|
|
||||
|
bool radio_init() { |
||||
|
rtc_clock.begin(Wire); |
||||
|
|
||||
|
return radio.std_init(&SPI); |
||||
|
} |
||||
|
|
||||
|
uint32_t radio_get_rng_seed() { |
||||
|
return radio.random(0x7FFFFFFF); |
||||
|
} |
||||
|
|
||||
|
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) { |
||||
|
radio.setFrequency(freq); |
||||
|
radio.setSpreadingFactor(sf); |
||||
|
radio.setBandwidth(bw); |
||||
|
radio.setCodingRate(cr); |
||||
|
} |
||||
|
|
||||
|
void radio_set_tx_power(uint8_t dbm) { |
||||
|
radio.setOutputPower(dbm); |
||||
|
} |
||||
|
|
||||
|
mesh::LocalIdentity radio_new_identity() { |
||||
|
RadioNoiseListener rng(radio); |
||||
|
return mesh::LocalIdentity(&rng); // create new random identity
|
||||
|
} |
||||
@ -0,0 +1,21 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#define RADIOLIB_STATIC_ONLY 1 |
||||
|
#include <RadioLib.h> |
||||
|
#include <helpers/radiolib/RadioLibWrappers.h> |
||||
|
#include <SenseCapSolarBoard.h> |
||||
|
#include <helpers/radiolib/CustomSX1262Wrapper.h> |
||||
|
#include <helpers/AutoDiscoverRTCClock.h> |
||||
|
#include <helpers/ArduinoHelpers.h> |
||||
|
#include <helpers/sensors/EnvironmentSensorManager.h> |
||||
|
|
||||
|
extern SenseCapSolarBoard board; |
||||
|
extern WRAPPER_CLASS radio_driver; |
||||
|
extern AutoDiscoverRTCClock rtc_clock; |
||||
|
extern EnvironmentSensorManager sensors; |
||||
|
|
||||
|
bool radio_init(); |
||||
|
uint32_t radio_get_rng_seed(); |
||||
|
void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr); |
||||
|
void radio_set_tx_power(uint8_t dbm); |
||||
|
mesh::LocalIdentity radio_new_identity(); |
||||
@ -0,0 +1,71 @@ |
|||||
|
#include "variant.h" |
||||
|
#include "wiring_constants.h" |
||||
|
#include "wiring_digital.h" |
||||
|
#include "nrf.h" |
||||
|
|
||||
|
const uint32_t g_ADigitalPinMap[] = { |
||||
|
// D0 .. D10 - Peripheral control pins
|
||||
|
2, // D0 P0.02 (A0) GNSS_WAKEUP
|
||||
|
3, // D1 P0.03 (A1) LORA_DIO1
|
||||
|
28, // D2 P0.28 (A2) LORA_RESET
|
||||
|
29, // D3 P0.29 (A3) LORA_BUSY
|
||||
|
4, // D4 P0.04 (A4/SDA) LORA_CS
|
||||
|
5, // D5 P0.05 (A5/SCL) LORA_SW
|
||||
|
43, // D6 P1.11 (UART_TX) GNSS_TX
|
||||
|
44, // D7 P1.12 (UART_RX) GNSS_RX
|
||||
|
45, // D8 P1.13 (SPI_SCK) LORA_SCK
|
||||
|
46, // D9 P1.14 (SPI_MISO) LORA_MISO
|
||||
|
47, // D10 P1.15 (SPI_MOSI) LORA_MOSI
|
||||
|
|
||||
|
// D11-D12 - LED outputs
|
||||
|
15, // D11 P0.15 User LED
|
||||
|
19, // D12 P0.19 Breathing LED
|
||||
|
|
||||
|
// D13 - User input
|
||||
|
33, // D13 P1.01 User Button
|
||||
|
|
||||
|
// D14-D15 - Grove/NFC interface
|
||||
|
9, // D14 P0.09 NFC1/GROVE_D1
|
||||
|
10, // D15 P0.10 NFC2/GROVE_D0
|
||||
|
|
||||
|
// D16 - Power management
|
||||
|
// 31, // D16 P0.31 VBAT_ADC (Battery voltage)
|
||||
|
31, // D16 P0.31 VBAT_ADC (Battery voltage)
|
||||
|
// D17 - GNSS control
|
||||
|
35, // D17 P1.03 GNSS_RESET
|
||||
|
|
||||
|
37, // D18 P1.05 GNSS_ENABLE
|
||||
|
14, // D19 P0.14 BAT_READ
|
||||
|
39, // D20 P1.07 USER_BUTTON
|
||||
|
|
||||
|
//
|
||||
|
21, // D21 P0.21 (QSPI_SCK)
|
||||
|
25, // D22 P0.25 (QSPI_CSN)
|
||||
|
20, // D23 P0.20 (QSPI_SIO_0 DI)
|
||||
|
24, // D24 P0.24 (QSPI_SIO_1 DO)
|
||||
|
22, // D25 P0.22 (QSPI_SIO_2 WP)
|
||||
|
23, // D26 P0.23 (QSPI_SIO_3 HOLD)
|
||||
|
}; |
||||
|
|
||||
|
void initVariant() { |
||||
|
pinMode(GPS_EN, OUTPUT); |
||||
|
digitalWrite(GPS_EN, LOW); |
||||
|
|
||||
|
pinMode(BATTERY_PIN, INPUT); |
||||
|
pinMode(VBAT_ENABLE, OUTPUT); |
||||
|
digitalWrite(VBAT_ENABLE, LOW); |
||||
|
|
||||
|
pinMode(PIN_QSPI_CS, OUTPUT); |
||||
|
digitalWrite(PIN_QSPI_CS, HIGH); |
||||
|
|
||||
|
pinMode(LED_GREEN, OUTPUT); |
||||
|
digitalWrite(LED_GREEN, LOW); |
||||
|
|
||||
|
pinMode(LED_BLUE, OUTPUT); |
||||
|
digitalWrite(LED_BLUE, LOW); |
||||
|
|
||||
|
/* disable gps until we actually support it.
|
||||
|
pinMode(GPS_EN, OUTPUT); |
||||
|
digitalWrite(GPS_EN, HIGH); |
||||
|
*/ |
||||
|
} |
||||
@ -0,0 +1,85 @@ |
|||||
|
#ifndef _SEEED_SENSECAP_SOLAR_H_ |
||||
|
#define _SEEED_SENSECAP_SOLAR_H_ |
||||
|
|
||||
|
/** Master clock frequency */ |
||||
|
#define VARIANT_MCK (64000000ul) |
||||
|
|
||||
|
#define USE_LFXO // Board uses 32khz crystal for LF
|
||||
|
|
||||
|
/*----------------------------------------------------------------------------
|
||||
|
* Headers |
||||
|
*----------------------------------------------------------------------------*/ |
||||
|
|
||||
|
#include "WVariant.h" |
||||
|
|
||||
|
#define PINS_COUNT (33) |
||||
|
#define NUM_DIGITAL_PINS (33) |
||||
|
#define NUM_ANALOG_INPUTS (8) |
||||
|
#define NUM_ANALOG_OUTPUTS (0) |
||||
|
|
||||
|
// LEDs
|
||||
|
#define PIN_LED (12) |
||||
|
#define LED_PWR (PINS_COUNT) |
||||
|
|
||||
|
#define LED_BUILTIN (PIN_LED) |
||||
|
|
||||
|
#define LED_RED (PINS_COUNT) |
||||
|
#define LED_GREEN (12) |
||||
|
#define LED_BLUE (11) |
||||
|
|
||||
|
#define LED_STATE_ON (1) // State when LED is litted
|
||||
|
|
||||
|
// Buttons
|
||||
|
#define PIN_BUTTON1 (13) |
||||
|
#define PIN_BUTTON2 (20) |
||||
|
|
||||
|
#define VBAT_ENABLE (19) // Output LOW to enable reading of the BAT voltage.
|
||||
|
|
||||
|
// Analog pins
|
||||
|
#define BATTERY_PIN (16) // Read the BAT voltage.
|
||||
|
#define AREF_VOLTAGE (3.0F) |
||||
|
#define ADC_MULTIPLIER (3.0F) // 1M, 512k divider bridge
|
||||
|
#define ADC_RESOLUTION (12) |
||||
|
|
||||
|
// Serial interfaces
|
||||
|
#define PIN_SERIAL1_RX (7) |
||||
|
#define PIN_SERIAL1_TX (6) |
||||
|
|
||||
|
// SPI Interfaces
|
||||
|
#define SPI_INTERFACES_COUNT (1) |
||||
|
|
||||
|
#define PIN_SPI_MISO (9) |
||||
|
#define PIN_SPI_MOSI (10) |
||||
|
#define PIN_SPI_SCK (8) |
||||
|
|
||||
|
// Lora SPI is on SPI0
|
||||
|
#define P_LORA_SCLK PIN_SPI_SCK |
||||
|
#define P_LORA_MISO PIN_SPI_MISO |
||||
|
#define P_LORA_MOSI PIN_SPI_MOSI |
||||
|
|
||||
|
// Wire Interfaces
|
||||
|
#define WIRE_INTERFACES_COUNT (1) |
||||
|
|
||||
|
#define PIN_WIRE_SDA (14) |
||||
|
#define PIN_WIRE_SCL (15) |
||||
|
|
||||
|
// GPS L76KB
|
||||
|
#define GPS_BAUDRATE 9600 |
||||
|
#define GPS_THREAD_INTERVAL 50 |
||||
|
#define PIN_GPS_TX PIN_SERIAL1_RX |
||||
|
#define PIN_GPS_RX PIN_SERIAL1_TX |
||||
|
#define PIN_GPS_STANDBY (0) |
||||
|
#define GPS_EN (18) |
||||
|
|
||||
|
// QSPI Pins
|
||||
|
#define PIN_QSPI_SCK (21) |
||||
|
#define PIN_QSPI_CS (22) |
||||
|
#define PIN_QSPI_IO0 (23) |
||||
|
#define PIN_QSPI_IO1 (24) |
||||
|
#define PIN_QSPI_IO2 (25) |
||||
|
#define PIN_QSPI_IO3 (26) |
||||
|
|
||||
|
#define EXTERNAL_FLASH_DEVICES P25Q16H |
||||
|
#define EXTERNAL_FLASH_USE_QSPI |
||||
|
|
||||
|
#endif |
||||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue