#include <SD.h>
#include <adafruit_1947_Obj.h>
#include <idlers.h>
#include <screen.h>
#include <colorRect.h>
#include <label.h>
#include <mechButton.h>
#include <iconButton.h>

#define BTN_PATH "system/icons/standard/check32.bmp"
#define MASK_PATH "system/icons/standard/mask32.bmp"
/*
some other icon choices..
trashC32.bmp
check32.bmp
choice32.bmp
cross32.bmp
sort32.bmp
sTrek32.bmp

These icons can use this mask to blend better to the background.
mask32.bmp
*/

mechButton  purpleButton(28); // The mechanical button object. Debounce & callback.
label*      ourLabel;         // A global text label pointer. (Created later)

void setup() {
   
   Serial.begin(115200);                                          // Fire up serial for debugging.
   while (!Serial) {                                              // We need to wait for connection..
      delay(10);                                                  // Smoke a cigarette.
   }

   if (!initScreen(ADAFRUIT_1947,ADA_1947_SHIELD_CS,PORTRAIT)) {  // Init screen.
      Serial.println("NO SCREEN!");                               // Screen init failed. Tell user.
      Serial.flush();                                             // Make sure the message gets out.
      while(true);                                                // Lock the process here.
   }

   if (!SD.begin(ADA_1947_SHIELD_SDCS)) {                         // With icons, we now MUST have an SD card.
      Serial.println("NO SD CARD!");                              // Tell user we have no SD card.
      Serial.flush();                                             // Make sure the message gets out.
      while(true);                                                // Lock the process here.
   }

   screen->fillScreen(&blue);                                     // Lets set the screen to.. Blue?
  
   colorRect* aRect = new colorRect(40,40,100,100,&green);        // Make an ugly green rectangle.
   viewList.addObj(aRect);                                        // And add that rectangle to the screen.
   
   ourLabel = new label(40,180,150,18,"Test");                    // Create our text label. Save it's pointer.
   ourLabel->setTextSize(2);                                      // Set the text size.
   ourLabel->setColors(&white,&blue);                             // Set it's colors.
   viewList.addObj(ourLabel);                                     // Add this label to the screen.
   
   iconButton* anIcon = new iconButton(40,220,BTN_PATH);          // Create an iconn button.
   if (anIcon) {                                                  // If we got one..
      anIcon->setCallback(iconClick);                             // Setup a callback function.
      
      bmpMask* aMask = new bmpMask;                               // We can set up a mask to blen in the drawing better.
      aMask->readFromBMP(MASK_PATH);                              // Create a mask object.
      anIcon->setMask(aMask);                                     // Hand it off to the iconButton.

      viewList.addObj(anIcon);                                    // Add it to the list.
   } else {                                                       // Else it was NULL..
      Serial.println("NO ICON");                                  // Tell the world we didn't get one.
   }

   ourEventMgr.begin();                                           // Fire up the event manager..
   purpleButton.setCallback(purpleClick);                         // Hook the button to a callback.
}


void iconClick(void) {

  ourLabel->setValue("Icon click!!");
}


// When the mechanical button gets clicked, or un-clicked, this function is called.
void purpleClick(void) {

   if (!purpleButton.getState()) {            // If clicked..
      ourLabel->setValue("Button click!!");   // Say it's been clicked.
   } else {                                   // Else lifted..
      ourLabel->setValue("test");             // Revert to "test"
   }                                          //
}


// During loop..
void loop() {     
   
  idle();    // Idlers get their time. (Buttons, screen things, etc.)
  delay(1);   // Needs this to function for some Wokwi reason.
  
  // You can do stuff here.
  // Want delay()? use sleep().
  // Keeps screen active.
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT