#include <adafruit_1947.h>
#include <label.h>
#include <colorRect.h>
#include <autoPOT.h>
#include <mapper.h>
#include <serialStr.h>
#define DSP_CS 10 // Display chip select
label* HelloText; // Pointer to a label object for the screen.
serialStr serialMgr; // A manaer for the serial port. Brings in complete messages.
colorRect* ourRect; // A rectangle object that is a solid color.
colorObj backColor; // We setup a colorObj for the background color of the display.
colorObj rectColor; // We setup a colorObj for holding the starting color of the rectangle.
autoPOT rectControl(A0); // Auto POT watches an analog pin and when a value changes it reports it.
mapper percentMaper(0,1023,0,100); // A mapper to map raw POT values to a percent.
colorMapper rectColorMap; // A color map to map colors from start color to end color as a percent.
// 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.
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.
}
// When the Serial port sees a message come in. It arrives here.
void newMsg(char* inStr) { HelloText->setValue(inStr); }
// When the POT sees a change in value. The new value arrives here.
void rectColorChg(int newValue) {
float percent;
colorObj newColor;
percent = percentMaper.map(newValue); // Map the raw value and map it to a percent.
newColor = rectColorMap.map(percent); // Map the percent to the desired color.
ourRect->setColor(&newColor); // Tell the rect to become that color.
}
// Extension to setup() that puts all the scren bits and things together.
void setupScreen(void) {
int height;
// Setup background display color.
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.
// Setup a label we can interact with.
HelloText = new label("Hello world!"); // Create a new label object.
if (HelloText) { // If we got one..
HelloText->setColors(&yellow,&backColor); // Set the text color to yellow.
HelloText->setLocation(10,10); // Set the top left corner location to 10,10 x,y.
height = HelloText->height; // Save off the text object's height.
HelloText->setSize(300,height*2); // Reset it's size to be 300 pix long.
HelloText->setTextSize(2); // Make it bigger so we cn read it.
viewList.addObj(HelloText); // Add the object (It's a pointer) to viewList to be managed.
} else { // Didn't get a label?
Serial.println("No label!"); // Tell Miss user.
while(true)delay(10); // Lock processor here forever.
} //
serialMgr.setCallback(newMsg); // Set the callback for when a message comes in from serial.
Serial.println("Type a message for the label"); // Give Miss user a hint.
// Setup a rectangle we can chage the color of.
rectColor.setColor(&red); // Set the starting color for the rect.
ourRect = new colorRect(20,40,50,50,&rectColor); // Create our colorRect object.
if (ourRect) { // If we got one..
viewList.addObj(ourRect); // Add the object (It's a pointer) to view list to be managed.
} else { // Didn't get a rect?
Serial.println("No color rect!"); // Tell Miss user.
while(true)delay(10); // Lock processor here forever.
}
rectColorMap.setColors(&rectColor,&backColor); // Setup the color map for the rectangle.
rectControl.setCallback(rectColorChg); // Setup the callback for when the pot sees a change in value.
}
// In loop, to run all of this, all you need is a call to idle.
void loop() { idle(); }