firmware/src/graphics/Screen.h

255 lines
7.9 KiB
C
Raw Normal View History

2020-02-07 21:51:17 +00:00
#pragma once
#include <cstring>
#include <OLEDDisplayUi.h>
#ifdef USE_SH1106
#include <SH1106Wire.h>
#else
#include <SSD1306Wire.h>
#endif
2020-09-26 16:40:48 +00:00
#include "EInkDisplay.h"
#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>
2020-02-21 12:57:08 +00:00
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;
/// Sets the name of the channel.
void setChannelNameStatus(const char *name)
{
2020-07-05 22:54:30 +00:00
concurrency::LockGuard guard(&lock);
channelName = name;
}
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);
std::string channelName;
/// 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);
public:
2020-04-23 23:20:07 +00:00
Screen(uint8_t address, int sda = -1, int scl = -1);
Screen(const Screen &) = delete;
Screen &operator=(const Screen &) = delete;
/// 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});
}
/// 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();
uint8_t brightness = 150;
/// 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
/// 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);
protected:
/// Updates the UI.
//
// Called periodically from the main loop.
uint32_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);
setInterval(0); // handle ASAP
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);
/// Rebuilds our list of frames (screens) to default ones.
void setFrames();
/// 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-08-28 22:06:52 +00:00
#elif defined(USE_SH1106)
SH1106Wire dispdev;
#else
SSD1306Wire dispdev;
#endif
/// UI helper for rendering to frames and switching between them
OLEDDisplayUi ui;
};
2020-07-07 08:46:49 +00:00
} // namespace graphics