Skip to content

Commit

Permalink
Add screen manager and virtual sensors to app (#1453)
Browse files Browse the repository at this point in the history
- add simple UI framework for tiny screens and few buttons
- add README documenting simple UI framework
- add screens for list of items, QR code
- add demo devices/endpoints/clusters/attributes
- add VLEDs (virtual LEDs)
- improve display wake behaviour
  • Loading branch information
mlepage-google authored Jul 7, 2020
1 parent 92fda7c commit 6350c46
Show file tree
Hide file tree
Showing 15 changed files with 1,339 additions and 265 deletions.
91 changes: 91 additions & 0 deletions examples/wifi-echo/server/esp32/README.screen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Simple Screen UI Framework

## Overview

This is a framework for creating simple user interfaces for devices with tiny
screens and just a few buttons.

For example, the [M5Stack](http://m5stack.com/) ESP32 Basic Core IoT device has
a 320x240 TFT display and three push buttons.

This framework enables a UI such as the following, where focus is indicated by
color, the left and middle buttons function as a single axis navpad, the right
button function as an action button, and the back button is provided by the
framework onscreen:

+--------------------------------------+
| < SCREEN TITLE |
| ------------ |
| |
| ITEM 1 |
| ITEM 2 |
| ITEM 3 |
| |
| |
| |
| |
| PREVIOUS NEXT ACTION |
+--------------------------------------+

## Features

- stack of screens with system back button
- labeled buttons for navigation and action
- focus handling
- custom screen content
- virtual LEDs (VLEDs)

## Screen Manager

The screen manager maintains a stack of screens that are pushed and popped for
interaction. Each is drawn with a title, system back button, three button
labels, and custom screen content. The screen manager dispatches events to the
topmost screen, such as:

- enter/exit events (including whether the screen was pushed or popped)
- focus events (for navigation)
- action events
- display events (lazily)

If configured, virtual LEDs (VLEDs) are omnipresent and can be toggled at will.

## Screen

Screens provide a title and three button labels to the screen manager. They
handle events from the screen manager, and cooperate to ensure focus behaves
nicely.

## Focus Handling

Screens are created without focus, and indicate to the screen manager whether
they can receive focus. If so, the screen manager focuses them when they are
pushed, and subsequently dispatches focus next/previous events which the screen
can use for navigation before any action is performed.

The screen can request the screen manager to focus the system back button, which
is always available except when there are no covered screens on the stack. In
this way, a list screen can wrap its focus of list items through the system back
button if it is available, while skipping it otherwise.

Blur and unblur focus events allow a screen's focus state to be preserved when
it is covered by a pushed screen and restored when it is subsequently uncovered.

In summary, screens collaborate with the screen manager to handle focus via the
following focus event types:

- NONE: remove focus from screen completely
- BLUR: unfocus screen (but retain focus state)
- UNBLUR: restore focus state (that was retained when blurred)
- NEXT: navigate focus forward in screen (possibly requesting focus back
button)
- PREVIOUS: navigate focus forward (possibly requesting back button focus)

## Typical Usage

A list screen can provide multiple options to the user. Each list item can push
another screen on the stack with more items. In this way a hierarchy of screens
can be formed.

The back button dismisses a screen much as "escape" or "cancel" would. It's
possible to push an informational screen that is not focusable and has no
interaction, such that the only action available is back.
12 changes: 9 additions & 3 deletions examples/wifi-echo/server/esp32/main/Display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#if CONFIG_HAVE_DISPLAY

// Brightness picked such that it's easy for cameras to focus on
#define DEFFAULT_BRIGHTNESS_PERCENT 10
#define DEFAULT_BRIGHTNESS_PERCENT 10

// 8MHz is the recommended SPI speed to init the driver with
// It later gets set to the preconfigured defaults within the driver
Expand All @@ -59,6 +59,8 @@ extern const char * TAG;
uint16_t DisplayHeight = 0;
uint16_t DisplayWidth = 0;

bool awake = false;

#if CONFIG_DISPLAY_AUTO_OFF
// FreeRTOS timer used to turn the display off after a short while
TimerHandle_t displayTimer = NULL;
Expand Down Expand Up @@ -154,13 +156,16 @@ void SetBrightness(uint16_t brightness_percent)
}
}

void WakeDisplay()
bool WakeDisplay()
{
SetBrightness(DEFFAULT_BRIGHTNESS_PERCENT);
bool woken = !awake;
awake = true;
SetBrightness(DEFAULT_BRIGHTNESS_PERCENT);
#if CONFIG_DISPLAY_AUTO_OFF
xTimerStart(displayTimer, 0);
ESP_LOGI(TAG, "Display awake but will switch off automatically in %d seconds", DISPLAY_TIMEOUT_MS / 1000);
#endif
return woken;
}

void ClearDisplay()
Expand Down Expand Up @@ -197,6 +202,7 @@ void TimerCallback(TimerHandle_t xTimer)
{
ESP_LOGI(TAG, "Display going to sleep...");
SetBrightness(0);
awake = false;
}

void SetupBrightnessControl()
Expand Down
64 changes: 23 additions & 41 deletions examples/wifi-echo/server/esp32/main/LEDWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*/

#include "LEDWidget.h"
#include "Display.h"

#include "ScreenManager.h"

#include "driver/gpio.h"
#include "esp_log.h"
Expand Down Expand Up @@ -54,6 +55,8 @@ void LEDWidget::Init(gpio_num_t gpioNum)
mBlinkOnTimeMS = 0;
mBlinkOffTimeMS = 0;
mGPIONum = gpioNum;
mVLED1 = -1;
mVLED2 = -1;
mState = false;
mError = false;
errorTimer = NULL;
Expand Down Expand Up @@ -87,9 +90,10 @@ void ClearErrorState(TimerHandle_t handle)
#if CONFIG_HAVE_DISPLAY
LEDWidget * pWidget = (LEDWidget *) pvTimerGetTimerID(handle);
pWidget->mError = false;
pWidget->Display();
// If a status change occured, wake the display
WakeDisplay();
if (pWidget->mVLED2 != -1)
{
ScreenManager::SetVLED(pWidget->mVLED2, false);
}
#endif
}

Expand All @@ -103,9 +107,10 @@ void LEDWidget::BlinkOnError()
}
errorTimer = xTimerCreate("ErrorTimer", pdMS_TO_TICKS(2000), false, this, ClearErrorState);
xTimerStart(errorTimer, 0);
Display();
// If a status change occured, wake the display
WakeDisplay();
if (mVLED2 != -1)
{
ScreenManager::SetVLED(mVLED2, true);
}
#endif
}

Expand Down Expand Up @@ -136,49 +141,26 @@ void LEDWidget::DoSet(bool state)
if (stateChange)
{
#if CONFIG_HAVE_DISPLAY

Display();
// If a status change occured, wake the display
WakeDisplay();
if (mVLED1 != -1)
{
ScreenManager::SetVLED(mVLED1, mState);
}
#endif
}
}

#if CONFIG_HAVE_DISPLAY
void LEDWidget::Display()
void LEDWidget::SetVLED(int id1, int id2)
{
uint16_t msgX = 0;
uint16_t msgY = (DisplayHeight * LED_STATUS_POSITION) / 100;
uint16_t circleX = (LED_INDICATOR_X * DisplayWidth) / 100;
uint16_t circleY = (LED_INDICATOR_Y * DisplayHeight) / 100;

// Wipe the Light Status Area
ClearRect(0, LED_STATUS_POSITION);
// Wipe the status circle
TFT_fillCircle(circleX, circleY, LED_INDICATOR_R_PX, TFT_BLACK);
// Display the Light Status on screen
TFT_setFont(DEJAVU24_FONT, NULL);
// Draw the default "Off" indicator
TFT_drawCircle(circleX, circleY, LED_INDICATOR_R_PX, TFT_DARKGREY);

if (mError)
mVLED1 = id1;
if (mVLED1 != -1)
{
TFT_print((char *) "Recv Error", msgX, msgY);
// Draw the "Error" indicator
TFT_fillCircle(circleX, circleY, LED_INDICATOR_R_PX, TFT_RED);
ScreenManager::SetVLED(mVLED1, mState);
}
else
mVLED2 = id2;
if (mVLED2 != -1)
{
if (mState)
{
TFT_print((char *) onMsg, msgX, msgY);
// Draw the "ON" indicator
TFT_fillCircle(circleX, circleY, LED_INDICATOR_R_PX, TFT_GREEN);
}
else
{
TFT_print((char *) offMsg, msgX, msgY);
}
ScreenManager::SetVLED(mVLED2, mError);
}
}
#endif
104 changes: 104 additions & 0 deletions examples/wifi-echo/server/esp32/main/ListScreen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
*
* Copyright (c) 2020 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @file ListScreen.cpp
*
* Simple list screen.
*
*/

#include "ListScreen.h"

#if CONFIG_HAVE_DISPLAY

#include <algorithm>

namespace {

const char * buttonText[] = { "Up", "Down", "Action" };

};

std::string ListScreen::GetButtonText(int id)
{
return buttonText[id - 1];
}

void ListScreen::Display()
{
int i = 0;
int items = (DisplayHeight - ScreenTitleSafeTop - ScreenTitleSafeBottom) / ScreenFontHeight;
if (items < model->GetItemCount())
{
i = std::max(0, focusIndex - items + (focusIndex == model->GetItemCount() - 1 ? 1 : 2));
}

for (int count = 0, y = ScreenTitleSafeTop; i < model->GetItemCount() && count < items; ++i, ++count, y += ScreenFontHeight)
{
tft_fg = focusIndex == i ? ScreenFocusColor : ScreenNormalColor;
TFT_print((char *) model->GetItemText(i).c_str(), ScreenTitleSafeTop, y);
}
}

void ListScreen::Focus(FocusType focus)
{
switch (focus)
{
case FocusType::NONE:
hasFocus = false;
focusIndex = -1;
break;
case FocusType::BLUR:
hasFocus = false;
// leave focus index alone
break;
case FocusType::UNBLUR:
hasFocus = true;
// leave focus index alone
break;
case FocusType::NEXT:
hasFocus = true;
if (focusIndex == -1)
{
focusIndex = 0;
break;
}
focusIndex = (focusIndex + 1) % model->GetItemCount(); // wraparound
if (focusIndex == 0)
{
ScreenManager::FocusBack(); // try focus back if it did wrap
}
break;
case FocusType::PREVIOUS:
hasFocus = true;
if (focusIndex == -1)
{
focusIndex = model->GetItemCount() - 1;
break;
}
focusIndex = (focusIndex + model->GetItemCount() - 1) % model->GetItemCount(); // wraparound
if (focusIndex == model->GetItemCount() - 1)
{
ScreenManager::FocusBack(); // try focus back if it did wrap
}
break;
}
}

#endif // CONFIG_HAVE_DISPLAY
Loading

0 comments on commit 6350c46

Please sign in to comment.