firmware/src/graphics/Screen.h

315 lines
9.6 KiB
C
Raw Normal View History

2020-02-07 21:51:17 +00:00
#pragma once
2021-03-15 02:00:20 +00:00
#ifdef NO_SCREEN
namespace graphics
{
// Noop class for boards without screen.
class Screen
{
public:
Screen(char){}
void onPress() {}
void setup() {}
void setOn(bool) {}
void print(const char*){}
void adjustBrightness(){}
void doDeepSleep() {}
void forceDisplay() {}
2021-03-15 02:00:20 +00:00
};
}
#else
#include <cstring>
#include <OLEDDisplayUi.h>
#include "../configuration.h"
#ifdef USE_ST7567
2020-10-23 10:00:43 +00:00
#include <ST7567Wire.h>
#else
// the SH1106/SSD1306 variant is auto-detected
#include <AutoOLEDWire.h>
#endif
#include "EInkDisplay2.h"
2020-09-26 16:40:48 +00:00
#include "TFTDisplay.h"
#include "TypedQueue.h"
2020-08-28 22:06:52 +00:00
#include "commands.h"
2020-07-06 08:45:55 +00:00
#include "concurrency/LockGuard.h"
#include "concurrency/OSThread.h"
#include "power.h"
#include <string>
#include "mesh/MeshModule.h"
2020-02-21 12:57:08 +00:00
2020-10-24 00:16:15 +00:00
// 0 to 255, though particular variants might define different defaults
#ifndef BRIGHTNESS_DEFAULT
#define BRIGHTNESS_DEFAULT 150
#endif
2020-07-07 08:46:49 +00:00
namespace graphics
{
// Forward declarations
class Screen;
/// Handles gathering and displaying debug information.
class DebugInfo
{
public:
DebugInfo(const DebugInfo &) = delete;
DebugInfo &operator=(const DebugInfo &) = delete;
private:
friend Screen;
DebugInfo() {}
/// Renders the debug screen.
void drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
void drawFrameSettings(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
void drawFrameWiFi(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
/// Protects all of internal state.
2020-07-05 22:54:30 +00:00
concurrency::Lock lock;
};
2020-07-07 08:46:49 +00:00
/**
* @brief This class deals with showing things on the screen of the device.
2020-08-28 22:06:52 +00:00
*
* @details Other than setup(), this class is thread-safe as long as drawFrame is not called
* multiple times simultaneously. All state-changing calls are queued and executed
2020-07-07 08:46:49 +00:00
* when the main loop calls us.
*/
class Screen : public concurrency::OSThread
{
2020-08-28 22:06:52 +00:00
CallbackObserver<Screen, const meshtastic::Status *> powerStatusObserver =
CallbackObserver<Screen, const meshtastic::Status *>(this, &Screen::handleStatusUpdate);
CallbackObserver<Screen, const meshtastic::Status *> gpsStatusObserver =
CallbackObserver<Screen, const meshtastic::Status *>(this, &Screen::handleStatusUpdate);
CallbackObserver<Screen, const meshtastic::Status *> nodeStatusObserver =
CallbackObserver<Screen, const meshtastic::Status *>(this, &Screen::handleStatusUpdate);
CallbackObserver<Screen, const MeshPacket *> textMessageObserver =
CallbackObserver<Screen, const MeshPacket *>(this, &Screen::handleTextMessage);
2022-01-13 08:19:36 +00:00
CallbackObserver<Screen, const UIFrameEvent *> uiFrameEventObserver =
CallbackObserver<Screen, const UIFrameEvent *>(this, &Screen::handleUIFrameEvent);
public:
2022-01-24 17:24:40 +00:00
explicit Screen(uint8_t address, int sda = -1, int scl = -1);
Screen(const Screen &) = delete;
Screen &operator=(const Screen &) = delete;
2021-12-18 16:02:54 +00:00
uint8_t address_found;
/// Initializes the UI, turns on the display, starts showing boot screen.
//
// Not thread safe - must be called before any other methods are called.
void setup();
/// Turns the screen on/off.
void setOn(bool on)
{
if (!on)
handleSetOn(
false); // We handle off commands immediately, because they might be called because the CPU is shutting down
else
2020-07-07 08:46:49 +00:00
enqueueCmd(ScreenCmd{.cmd = on ? Cmd::SET_ON : Cmd::SET_OFF});
}
/**
* Prepare the display for the unit going to the lowest power mode possible. Most screens will just
* poweroff, but eink screens will show a "I'm sleeping" graphic, possibly with a QR code
*/
void doDeepSleep();
2020-12-20 22:45:45 +00:00
void blink();
/// Handles a button press.
2020-07-07 08:46:49 +00:00
void onPress() { enqueueCmd(ScreenCmd{.cmd = Cmd::ON_PRESS}); }
2020-02-22 20:01:59 +00:00
// Implementation to Adjust Brightness
void adjustBrightness();
2020-10-24 00:16:15 +00:00
uint8_t brightness = BRIGHTNESS_DEFAULT;
/// Starts showing the Bluetooth PIN screen.
//
// Switches over to a static frame showing the Bluetooth pairing screen
// with the PIN.
void startBluetoothPinScreen(uint32_t pin)
{
2020-07-07 08:46:49 +00:00
ScreenCmd cmd;
cmd.cmd = Cmd::START_BLUETOOTH_PIN_SCREEN;
cmd.bluetooth_pin = pin;
enqueueCmd(cmd);
}
2020-02-22 20:01:59 +00:00
void startFirmwareUpdateScreen()
{
ScreenCmd cmd;
cmd.cmd = Cmd::START_FIRMWARE_UPDATE_SCREEN;
enqueueCmd(cmd);
}
void startShutdownScreen()
{
ScreenCmd cmd;
cmd.cmd = Cmd::START_SHUTDOWN_SCREEN;
enqueueCmd(cmd);
}
/// Stops showing the bluetooth PIN screen.
2020-07-07 08:46:49 +00:00
void stopBluetoothPinScreen() { enqueueCmd(ScreenCmd{.cmd = Cmd::STOP_BLUETOOTH_PIN_SCREEN}); }
2020-02-22 20:01:59 +00:00
/// Stops showing the boot screen.
2020-07-07 08:46:49 +00:00
void stopBootScreen() { enqueueCmd(ScreenCmd{.cmd = Cmd::STOP_BOOT_SCREEN}); }
/// Writes a string to the screen.
void print(const char *text)
{
2020-07-07 08:46:49 +00:00
ScreenCmd cmd;
cmd.cmd = Cmd::PRINT;
// TODO(girts): strdup() here is scary, but we can't use std::string as
// FreeRTOS queue is just dumbly copying memory contents. It would be
// nice if we had a queue that could copy objects by value.
cmd.print_text = strdup(text);
if (!enqueueCmd(cmd)) {
free(cmd.print_text);
}
}
/// Overrides the default utf8 character conversion, to replace empty space with question marks
2020-08-28 22:06:52 +00:00
static char customFontTableLookup(const uint8_t ch)
{
// UTF-8 to font table index converter
// Code form http://playground.arduino.cc/Main/Utf8ascii
static uint8_t LASTCHAR;
2020-08-28 22:06:52 +00:00
static bool SKIPREST; // Only display a single unconvertable-character symbol per sequence of unconvertable characters
if (ch < 128) { // Standard ASCII-set 0..0x7F handling
LASTCHAR = 0;
SKIPREST = false;
return ch;
}
2020-08-28 22:06:52 +00:00
uint8_t last = LASTCHAR; // get last char
LASTCHAR = ch;
2020-08-28 22:06:52 +00:00
switch (last) { // conversion depnding on first UTF8-character
case 0xC2: {
SKIPREST = false;
return (uint8_t)ch;
}
case 0xC3: {
SKIPREST = false;
return (uint8_t)(ch | 0xC0);
}
}
// We want to strip out prefix chars for two-byte char formats
2020-08-28 22:06:52 +00:00
if (ch == 0xC2 || ch == 0xC3 || ch == 0x82)
return (uint8_t)0;
2020-08-28 22:06:52 +00:00
// If we already returned an unconvertable-character symbol for this unconvertable-character sequence, return NULs for the
// rest of it
if (SKIPREST)
return (uint8_t)0;
SKIPREST = true;
2020-08-28 22:06:52 +00:00
return (uint8_t)191; // otherwise: return ¿ if character can't be converted (note that the font map we're using doesn't
// stick to standard EASCII codes)
}
/// Returns a handle to the DebugInfo screen.
//
// Use this handle to set things like battery status, user count, GPS status, etc.
2020-08-28 22:06:52 +00:00
DebugInfo *debug_info() { return &debugInfo; }
2020-06-29 01:17:52 +00:00
int handleStatusUpdate(const meshtastic::Status *arg);
int handleTextMessage(const MeshPacket *arg);
2022-01-13 08:19:36 +00:00
int handleUIFrameEvent(const UIFrameEvent *arg);
2020-10-15 07:56:38 +00:00
/// Used to force (super slow) eink displays to draw critical frames
void forceDisplay();
2021-11-26 20:09:16 +00:00
/// Draws our SSL cert screen during boot (called from WebServer)
void setSSLFrames();
protected:
/// Updates the UI.
//
// Called periodically from the main loop.
2020-10-10 01:57:57 +00:00
int32_t runOnce() final;
private:
2020-07-07 08:46:49 +00:00
struct ScreenCmd {
Cmd cmd;
union {
uint32_t bluetooth_pin;
char *print_text;
};
};
/// Enques given command item to be processed by main loop().
2020-07-07 08:46:49 +00:00
bool enqueueCmd(const ScreenCmd &cmd)
{
if (!useDisplay)
return true; // claim success if our display is not in use
else {
bool success = cmdQueue.enqueue(cmd, 0);
2020-10-12 00:13:32 +00:00
enabled = true; // handle ASAP (we are the registered reader for cmdQueue, but might have been disabled)
return success;
}
}
// Implementations of various commands, called from doTask().
void handleSetOn(bool on);
void handleOnPress();
void handleStartBluetoothPinScreen(uint32_t pin);
void handlePrint(const char *text);
void handleStartFirmwareUpdateScreen();
void handleShutdownScreen();
/// Rebuilds our list of frames (screens) to default ones.
void setFrames();
2020-10-16 02:53:55 +00:00
/// Try to start drawing ASAP
void setFastFramerate();
/// Called when debug screen is to be drawn, calls through to debugInfo.drawFrame.
static void drawDebugInfoTrampoline(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
static void drawDebugInfoSettingsTrampoline(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
static void drawDebugInfoWiFiTrampoline(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y);
/// Queue of commands to execute in doTask.
2020-07-07 08:46:49 +00:00
TypedQueue<ScreenCmd> cmdQueue;
/// Whether we are using a display
bool useDisplay = false;
/// Whether the display is currently powered
bool screenOn = false;
// Whether we are showing the regular screen (as opposed to booth screen or
// Bluetooth PIN screen)
bool showingNormalScreen = false;
/// Holds state for debug information
DebugInfo debugInfo;
2020-07-07 08:46:49 +00:00
/// Display device
2020-08-28 22:06:52 +00:00
/** FIXME cleanup display abstraction */
#ifdef ST7735_CS
TFTDisplay dispdev;
2020-09-26 16:40:48 +00:00
#elif defined(HAS_EINK)
EInkDisplay dispdev;
2020-10-23 10:00:43 +00:00
#elif defined(USE_ST7567)
ST7567Wire dispdev;
#else
AutoOLEDWire dispdev;
#endif
/// UI helper for rendering to frames and switching between them
OLEDDisplayUi ui;
};
2020-07-07 08:46:49 +00:00
} // namespace graphics
2022-01-24 17:24:40 +00:00
#endif