Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions boards/arduino-nesso-n1/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,34 @@ void InputHandler(void) {
static uint32_t btnBFirstReleaseMs = 0;
static bool btnBWaitingSecondClick = false;
static bool btnBLongPressFired = false;
static bool touchWasPressed = false;
if (millis() - tm < 200 && !LongPress) return;
if (!trylockSysI2CBus()) return; // RFID driver mid-transaction - retry next tick
M5.update();
unlockSysI2CBus();

checkPowerSaveTime();

bool emitNext = false;
bool emitPrev = false;
bool emitEsc = false;
uint32_t now = millis();

auto t = M5.Touch.getDetail();
if (t.isPressed() || t.isHolding()) {
bool touchPressed = t.isPressed() || t.isHolding();
if (touchPressed && !touchWasPressed) {
tm = millis();
touchWasPressed = true;
if (wakeUpScreen()) return;

touchPoint.x = t.x;
touchPoint.y = t.y;
touchPoint.pressed = true;
touchHeatMap(touchPoint);
} else touchPoint.pressed = false;
AnyKeyPress = true;
return;
}
touchWasPressed = touchPressed;

bool btnAActive = M5.BtnA.isPressed() || M5.BtnA.isHolding();
bool btnBActive = M5.BtnB.isPressed() || M5.BtnB.isHolding();
Expand Down Expand Up @@ -99,13 +107,14 @@ void InputHandler(void) {
emitNext = true;
}

AnyKeyPress = btnAActive || btnBActive || btnBWaitingSecondClick || M5.BtnA.wasClicked() || emitNext ||
emitPrev || emitEsc;
bool btnAClicked = M5.BtnA.wasClicked();
AnyKeyPress =
btnAActive || btnBActive || btnBWaitingSecondClick || btnAClicked || emitNext || emitPrev || emitEsc;
if (!AnyKeyPress) return;

if ((btnAActive || btnBActive) && wakeUpScreen()) return;

if (M5.BtnA.wasClicked()) SelPress = true;
if (btnAClicked) SelPress = true;
if (emitNext) NextPress = true;
if (emitPrev) PrevPress = true;
if (emitEsc) EscPress = true;
Expand Down
73 changes: 73 additions & 0 deletions boards/arduino-nesso-n1/nesso_lora.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "core/bus_HAL.h"
#include <Arduino.h>
#include <Wire.h>

namespace {
constexpr uint8_t kExpanderAddress = 0x43;
constexpr uint8_t kExpanderGlobalControl = 0x01;
constexpr uint8_t kExpanderOutputEnable = 0x03;
constexpr uint8_t kExpanderOutputState = 0x05;
constexpr uint8_t kExpanderHighImpedance = 0x07;
constexpr uint8_t kExpanderDefaultOutput = 0x09;
constexpr uint8_t kExpanderInterruptMask = 0x11;
constexpr uint8_t kExpanderGlobalControlEnable = 0x01;
constexpr uint8_t kLoraLnaEnableBit = 5;
constexpr uint8_t kLoraAntennaSwitchBit = 6;
constexpr uint8_t kLoraEnableBit = 7;

bool readExpanderRegister(TwoWire *wire, uint8_t reg, uint8_t &value) {
if (wire == nullptr) return false;
wire->beginTransmission(kExpanderAddress);
wire->write(reg);
if (wire->endTransmission(false) != 0) return false;
if (wire->requestFrom(kExpanderAddress, (uint8_t)1) != 1) return false;
value = wire->read();
return true;
}

bool writeExpanderRegister(TwoWire *wire, uint8_t reg, uint8_t value) {
if (wire == nullptr) return false;
wire->beginTransmission(kExpanderAddress);
wire->write(reg);
wire->write(value);
return wire->endTransmission() == 0;
}

bool writeExpanderBit(TwoWire *wire, uint8_t reg, uint8_t bit, bool enabled) {
uint8_t value = 0;
if (!readExpanderRegister(wire, reg, value)) return false;
uint8_t mask = 1u << bit;
value = enabled ? (value | mask) : (value & ~mask);
return writeExpanderRegister(wire, reg, value);
}

bool configureExpanderOutput(TwoWire *wire, uint8_t bit) {
return writeExpanderBit(wire, kExpanderOutputEnable, bit, true) &&
writeExpanderBit(wire, kExpanderHighImpedance, bit, false);
}

bool prepareNessoLoRaFrontend() {
TwoWire *wire = getSysI2CBus();
if (wire == nullptr) return false;

uint8_t discarded = 0;
if (!readExpanderRegister(wire, kExpanderGlobalControl, discarded)) return false;
if (!writeExpanderRegister(wire, kExpanderGlobalControl, kExpanderGlobalControlEnable)) return false;
if (!writeExpanderRegister(wire, kExpanderDefaultOutput, 0xFF)) return false;
if (!writeExpanderRegister(wire, kExpanderInterruptMask, 0xFF)) return false;
if (!configureExpanderOutput(wire, kLoraEnableBit)) return false;
if (!configureExpanderOutput(wire, kLoraAntennaSwitchBit)) return false;
if (!configureExpanderOutput(wire, kLoraLnaEnableBit)) return false;

if (!writeExpanderBit(wire, kExpanderOutputState, kLoraEnableBit, false)) return false;
delay(10);
if (!writeExpanderBit(wire, kExpanderOutputState, kLoraEnableBit, true)) return false;
delay(10);
if (!writeExpanderBit(wire, kExpanderOutputState, kLoraAntennaSwitchBit, true)) return false;
if (!writeExpanderBit(wire, kExpanderOutputState, kLoraLnaEnableBit, true)) return false;
delay(10);
return true;
}
} // namespace

bool prepareBoardLoRaRadio() { return prepareNessoLoRaFrontend(); }
4 changes: 4 additions & 0 deletions boards/arduino-nesso-n1/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ build_flags =
-DLORA_CS=23
-DLORA_RST=-1
-DLORA_DIO0=15
-DLORA_IRQ=15
-DLORA_BUSY=19
-DDEFAULT_LORA_RADIO='"SX1262"'
-DDEFAULT_LORA_FREQUENCY='"915000000.00"'

;SD Card Setup pins
-D SDCARD_CS=SPI_SS_PIN
Expand Down
27 changes: 22 additions & 5 deletions src/modules/lora/LoRaRF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ volatile bool loraInterruptEnabled = true;
enum class LoRaRadioVariant { SX1276, SX1262 };
LoRaRadioVariant loraRadioVariant = LoRaRadioVariant::SX1276;

#ifndef DEFAULT_LORA_RADIO
#define DEFAULT_LORA_RADIO "SX1276"
#endif

#ifndef DEFAULT_LORA_FREQUENCY
#define DEFAULT_LORA_FREQUENCY "434500000.00"
#endif

bool __attribute__((weak)) prepareBoardLoRaRadio() { return true; }

int getLoraIrqPin() {
#ifdef LORA_IRQ
return LORA_IRQ;
Expand Down Expand Up @@ -85,8 +95,9 @@ void onLoraPacket() {
}

SPIClass *selectLoraSPIBus() {
SPIClass *bus =
acquireSPIBus(bruceConfigPins.LoRa_bus.sck, bruceConfigPins.LoRa_bus.miso, bruceConfigPins.LoRa_bus.mosi);
SPIClass *bus = acquireSPIBus(
bruceConfigPins.LoRa_bus.sck, bruceConfigPins.LoRa_bus.miso, bruceConfigPins.LoRa_bus.mosi
);
if (!bus) {
Serial.println("No hardware SPI bus available for LoRa, falling back to default SPI");
return &SPI;
Expand All @@ -111,6 +122,12 @@ bool startLoraRadio(float bandMHz) {
return false;
}

if (!prepareBoardLoRaRadio()) {
Serial.println("Preparing LoRa frontend failed!");
displayError("LoRa Init Failed", true);
return false;
}

loraSpi = selectLoraSPIBus();
clearLoraRadio();
const int busyPin = (loraRadioVariant == LoRaRadioVariant::SX1262) ? getLoraBusyPin() : GPIO_NUM_NC;
Expand Down Expand Up @@ -293,7 +310,7 @@ void downpress() {
}

void selectRadioVariant(JsonDocument &doc) {
String stored = doc["LoRa_Radio"] | "SX1276";
String stored = doc["LoRa_Radio"] | DEFAULT_LORA_RADIO;
if (stored.equalsIgnoreCase("SX1262")) { loraRadioVariant = LoRaRadioVariant::SX1262; }
std::vector<Option> radioOptions = {
{"SX1276", []() {}},
Expand Down Expand Up @@ -387,9 +404,9 @@ void lorachat() {
Serial.println("creating lora settings .json file");
JsonDocument doc;
File file = LittleFS.open("/lora_settings.json", "w");
doc["LoRa_Frequency"] = "434500000.00";
doc["LoRa_Frequency"] = DEFAULT_LORA_FREQUENCY;
doc["LoRa_Name"] = "BruceTest";
doc["LoRa_Radio"] = "SX1276";
doc["LoRa_Radio"] = DEFAULT_LORA_RADIO;
serializeJson(doc, file);
file.close();
}
Expand Down
Loading