Skip to content

Commit

Permalink
Merge pull request #45 from girtsf/screen-cpp-refactor
Browse files Browse the repository at this point in the history
Screen cleanups and refactoring
  • Loading branch information
geeksville authored Mar 19, 2020
2 parents c9e2e6c + daf8594 commit 6bc8e1b
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 346 deletions.
20 changes: 14 additions & 6 deletions lib/BluetoothOTA/src/BluetoothUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <Arduino.h>
#include <Update.h>
#include "configuration.h"
#include "screen.h"

SimpleAllocator btPool;

Expand Down Expand Up @@ -173,7 +172,7 @@ uint32_t getValue32(BLECharacteristic *c, uint32_t defaultValue)

class MySecurity : public BLESecurityCallbacks
{

protected:
bool onConfirmPIN(uint32_t pin)
{
Serial.printf("onConfirmPIN %u\n", pin);
Expand All @@ -189,7 +188,7 @@ class MySecurity : public BLESecurityCallbacks
void onPassKeyNotify(uint32_t pass_key)
{
Serial.printf("onPassKeyNotify %u\n", pass_key);
screen_start_bluetooth(pass_key);
startCb(pass_key);
}

bool onSecurityRequest()
Expand All @@ -211,9 +210,13 @@ class MySecurity : public BLESecurityCallbacks
Serial.printf("onAuthenticationComplete -> fail %d\n", cmpl.fail_reason);
}

// Remove our custom screen
screen.setFrames();
// Remove our custom PIN request screen.
stopCb();
}

public:
StartBluetoothPinScreenCallback startCb;
StopBluetoothPinScreenCallback stopCb;
};

BLEServer *pServer;
Expand Down Expand Up @@ -255,7 +258,10 @@ void deinitBLE()
btPool.reset();
}

BLEServer *initBLE(std::string deviceName, std::string hwVendor, std::string swVersion, std::string hwVersion)
BLEServer *initBLE(
StartBluetoothPinScreenCallback startBtPinScreen,
StopBluetoothPinScreenCallback stopBtPinScreen,
std::string deviceName, std::string hwVendor, std::string swVersion, std::string hwVersion)
{
BLEDevice::init(deviceName);
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
Expand All @@ -264,6 +270,8 @@ BLEServer *initBLE(std::string deviceName, std::string hwVendor, std::string swV
* Required in authentication process to provide displaying and/or input passkey or yes/no butttons confirmation
*/
static MySecurity mySecurity;
mySecurity.startCb = startBtPinScreen;
mySecurity.stopCb = stopBtPinScreen;
BLEDevice::setSecurityCallbacks(&mySecurity);

// Create the BLE Server
Expand Down
10 changes: 9 additions & 1 deletion lib/BluetoothOTA/src/BluetoothUtil.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <functional>

#include <Arduino.h>
#include <BLEDevice.h>
#include <BLEServer.h>
Expand All @@ -17,8 +19,14 @@ void dumpCharacteristic(BLECharacteristic *c);
/** converting endianness pull out a 32 bit value */
uint32_t getValue32(BLECharacteristic *c, uint32_t defaultValue);

// TODO(girts): create a class for the bluetooth utils helpers?
using StartBluetoothPinScreenCallback = std::function<void(uint32_t pass_key)>;
using StopBluetoothPinScreenCallback = std::function<void(void)>;

void loopBLE();
BLEServer *initBLE(std::string devName, std::string hwVendor, std::string swVersion, std::string hwVersion = "");
BLEServer *initBLE(
StartBluetoothPinScreenCallback startBtPinScreen, StopBluetoothPinScreenCallback stopBtPinScreen,
std::string devName, std::string hwVendor, std::string swVersion, std::string hwVersion = "");
void deinitBLE();

/// Add a characteristic that we will delete when we restart
Expand Down
6 changes: 3 additions & 3 deletions src/MeshService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include <Arduino.h>
#include <assert.h>

#include "main.h"
#include "mesh-pb-constants.h"
#include "MeshService.h"
#include "MeshBluetoothService.h"
#include "NodeDB.h"
#include "GPS.h"
#include "screen.h"
#include "Periodic.h"
#include "PowerFSM.h"

Expand Down Expand Up @@ -118,7 +118,7 @@ MeshPacket *MeshService::handleFromRadioUser(MeshPacket *mp)
sendOurOwner(mp->from);

String lcd = String("Joined: ") + mp->payload.variant.user.long_name + "\n";
screen_print(lcd.c_str());
screen.print(lcd.c_str());
}

return mp;
Expand Down Expand Up @@ -360,4 +360,4 @@ void MeshService::onNotify(Observable *o)
{
DEBUG_MSG("got gps notify\n");
onGPSChanged();
}
}
2 changes: 1 addition & 1 deletion src/PowerFSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ void PowerFSM_setup()
// timeout");

powerFSM.run_machine(); // run one interation of the state machine, so we run our on enter tasks for the initial DARK state
}
}
32 changes: 28 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ AXP20X_Class axp;
bool pmu_irq = false;
#endif

// Global Screen singleton
#ifdef I2C_SDA
meshtastic::Screen screen(SSD1306_ADDRESS, I2C_SDA, I2C_SCL);
#else
// Fake values for pins to keep build happy, we won't ever initialize it.
meshtastic::Screen screen(SSD1306_ADDRESS, 0, 0);
#endif

// these flags are all in bss so they default false
bool isCharging;
bool isUSBPowered;
Expand Down Expand Up @@ -221,8 +229,6 @@ void setup()
scanI2Cdevice();
#endif

axp192Init();

// Buttons & LED
#ifdef BUTTON_PIN
pinMode(BUTTON_PIN, INPUT_PULLUP);
Expand All @@ -240,13 +246,16 @@ void setup()
if (wakeCause == ESP_SLEEP_WAKEUP_TIMER)
ssd1306_found = false; // forget we even have the hardware

// Initialize the screen first so we can show the logo while we start up everything else.
if (ssd1306_found)
screen.setup();

axp192Init();

// Init GPS
gps.setup();

screen_print("Started...\n");
screen.print("Started...\n");

service.init();

Expand All @@ -264,7 +273,14 @@ void initBluetooth()
// FIXME - we are leaking like crazy
// AllocatorScope scope(btPool);

BLEServer *serve = initBLE(getDeviceName(), HW_VENDOR, xstr(APP_VERSION), xstr(HW_VERSION)); // FIXME, use a real name based on the macaddr
// Note: these callbacks might be coming in from a different thread.
BLEServer *serve = initBLE(
[](uint8_t pin) {
powerFSM.trigger(EVENT_BLUETOOTH_PAIR);
screen.startBluetoothPinScreen(pin);
},
[]() { screen.stopBluetoothPinScreen(); },
getDeviceName(), HW_VENDOR, xstr(APP_VERSION), xstr(HW_VERSION)); // FIXME, use a real name based on the macaddr
createMeshBluetoothService(serve);

// Start advertising - this must be done _after_ creating all services
Expand Down Expand Up @@ -392,6 +408,14 @@ void loop()
}
#endif

// Show boot screen for first 3 seconds, then switch to normal operation.
static bool showingBootScreen = true;
if (showingBootScreen && (millis() > 3000))
{
screen.stopBootScreen();
showingBootScreen = false;
}

// No GPS lock yet, let the OS put the main CPU in low power mode for 100ms (or until another interrupt comes in)
// i.e. don't just keep spinning in loop as fast as we can.
//DEBUG_MSG("msecs %d\n", msecstosleep);
Expand Down
7 changes: 6 additions & 1 deletion src/main.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#pragma once

#include "screen.h"

extern bool axp192_found;
extern bool ssd1306_found;
extern bool isCharging;
extern bool isUSBPowered;
extern bool isUSBPowered;

// Global Screen singleton.
extern meshtastic::Screen screen;
Loading

0 comments on commit 6bc8e1b

Please sign in to comment.