// ************************************************************************************************
//
// Documentation for this, and the rest of the LC libraies that make this up.
// Here's the book : https://github.com/leftCoast/LC_libraryDocs/blob/main/LC_libraries.pdf
// Search for : timeObj, label,
//
// ************************************************************************************************
#include <adafruit_1947.h>
#include <liveText.h>
#include <bargraph.h>
#include <neoPixel.h>
#define DSP_CS 10 // Display chip select.
#define MAX_TIME 15000 // Count down time.
colorObj backColor; // Global color for display background.
colorBargraph* ourBargraph; // Global bargraph object.
timeObj ourTimer(MAX_TIME); // Global cound down timer.
timeObj drawtimer(25); // Global draw timer.
label* valueRemain; // Gloabl label to show remainging time vlaue.
neoPixel theGaugeLight(1,12); // A newoPixel to echo the color.
// The way setup() works here is mostly for hardware setup. If that all works
// then we go on to setup the srceen object and controls.
void setup() {
Serial.begin(57600); // Fire up serial for debugging.
theGaugeLight.begin();
screen = (displayObj*) new adafruit_1947(DSP_CS,-1); // Create our screen object.
if (screen) { // We got one? Cool!
if (screen->begin()) { // Can we successfully call begin on the screen?
screen->setRotation(PORTRAIT); // Ok, set portrait. Good for handhelds.
setupScreen(); // Populate the screen.
return; // Everything fired up successfuly. Lets go!
} //
} //
Serial.println("NO SCREEN!"); // Send an error out the serial port.
while(true)delay(10); // Lock processor here forever.
}
// Extension to setup() that puts all the scren bits and things together.
void setupScreen(void) {
label* barLabel; // Local, we don't need to change it.
rect aRect; // A rect to use to pass info with.
backColor.setColor(&blue); // Set this color object to blue.
backColor.blend(&black,70); // Lets darken it, mixin some black.
screen->fillScreen(&backColor); // A command to "screen" happens (NOW). Objects update later.
barLabel = new label(20,20,200,16); // location, size hand back a label.
if (barLabel) { // If we got one..
barLabel->setValue("Time remaining"); // Set our message.
barLabel->setColors(&yellow,&backColor); // Text color, also sets background color.
barLabel->setJustify(TEXT_CENTER); // Center it.
viewList.addObj(barLabel); // Add it to the view list to manage.
} //
aRect.setRect(30,40,180,55); // Set up rectangle for bargraph.
ourBargraph = new colorBargraph(&aRect,&backColor,rightLeft); // Create it as a global, need it later.
if (ourBargraph) { // If we got it..
ourBargraph->addColor(MAX_TIME,&green); // Add the colors for certan points.
ourBargraph->addColor(MAX_TIME/2.0,&green); // Adding the colors also sets the min & max.
ourBargraph->addColor(MAX_TIME/3.0,&yellow); // So that later on we won't need to.
ourBargraph->addColor(MAX_TIME/7.0,&yellow); //
ourBargraph->addColor(MAX_TIME/9.5,&red); //
ourBargraph->addColor(0,&red); //
ourBargraph->setValue(0); //
viewList.addObj(ourBargraph); // Add it to the view list to manage.
} //
valueRemain = new label(20,125,100,16); // A label to update, so it's global.
if (valueRemain) { // Got it?
valueRemain->setColors(&yellow,&backColor); // Forground and background colors.
valueRemain->setJustify(TEXT_RIGHT); // It's a number, this looks better.
viewList.addObj(valueRemain); // Set it.
} //
screen->drawRoundRect(20,30,200,75,15,&green); // Only background so just draw it now.
}
// Make sure to call idle() in loop.
void loop() {
float currentVal;
colorObj currentColor;
idle(); // Run the magic.
if (drawtimer.ding()){ // If it's time to draw..
currentVal = ourTimer.getFraction()*MAX_TIME; // Caclulate current time left.
ourBargraph->setValue(currentVal); // Send this to the bargraph.
valueRemain->setValue(currentVal); // Send it to the text below.
currentColor = ourBargraph->map(currentVal); // Grab the color from the bargraph.
theGaugeLight.setPixelColor(0,¤tColor); // Send it to the pixel.
theGaugeLight.show(); // All the way down the wire to the pixel.
drawtimer.start(); // Restart the timer.
} //
if (ourTimer.ding()) ourTimer.start(); // If we run out of time, restart the timer.
}