touchscreen.ino 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10

/*
Dara Klinkner, George Fox University
ENGR 152 Semester Project, P.D.A
Touchscreen Code
*/

// ************************
//  Include all libraries
// ************************
thallia's avatar
thallia committed
11 12 13 14

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
thallia's avatar
thallia committed
15 16
#include <ArduinoJson.h>
#include <SD.h>
thallia's avatar
thallia committed
17

18 19 20 21 22

// *****************************************
//  Define RA8875 Pins and Program States
// *****************************************

thallia's avatar
thallia committed
23 24 25 26 27 28 29
// Library only supports hardware SPI at this time
// Connect SCLK to UNO Digital #13 (Hardware SPI clock)
// Connect MISO to UNO Digital #12 (Hardware SPI MISO)
// Connect MOSI to UNO Digital #11 (Hardware SPI MOSI)
#define RA8875_INT 3 // defines the interrupt pin
#define RA8875_CS 10 // touchscreen chip select
#define RA8875_RESET 9 // reset pin, 10k resistor
thallia's avatar
thallia committed
30
#define SD_CS 7
thallia's avatar
thallia committed
31

32 33 34 35
// define all the states!
#define HOMESCREEN 1 // first state, display the homescreen
#define CHORES 2 // display when they ask to display the chore list
#define NEWCHORE 3 // display when they ask to input a new chore for the kid
36
#define DAY 4 // when the kid picks a day, display the chores they're supposed to do on that day
37 38 39 40 41 42 43 44 45

// ************************
//  Variables
// ************************

// the starting state of the program
int currentState = HOMESCREEN;
boolean hasDisplayed = false; // so we don't blow up the serial monitor

thallia's avatar
thallia committed
46 47 48
uint16_t lastX = 0; // these are for touch debouncing
uint16_t lastY = 0;

49 50 51 52
// for displaying days of the week
char daysOfTheWeek[7][10] = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};

thallia's avatar
thallia committed
53 54
//char choresToDo[9][15] = {"Wash dishes", "Walk dog", "Fold laundry", "Do homework",
//"Make bed", "Clean room", "Shower"};
55

56 57 58 59 60 61 62 63 64 65 66
// ----------------IMPORTANT READ--------------------//
/*
  When we get to the beginning of the program, we need to pull
  basic child data here. This includes first name, last name, level,
  experience, and potentially mon-sun chores. We can also have the
  chores pulled when the touch functions are activated.
*/

// for now, all the strings are hardcoded for testing purposes
// prep all the data to be printed
// first name needs to be 9 char max
thallia's avatar
thallia committed
67
//char firstName[] = "Bobby";
68
// last name needs to be
thallia's avatar
thallia committed
69
//char lastName[] = "Bargfield";
70 71 72
// Level
char level[] = "Level: ";
// level Level
thallia's avatar
thallia committed
73
//char actualLevel[] = "3";
74
// experience number, that hopefully changes ;; 1800 max
thallia's avatar
thallia committed
75
//char xP[] = "1000";
76 77 78 79
// XP prefix
char eXPerience[] = "XP:  ";
// day to display question
char dayDisplay[] = "What day would you like to view?";
80 81
// back button
char backButton[] = "Back";
82

thallia's avatar
thallia committed
83 84 85 86 87
char *firstName;
char *lastName;
int lvl;
int xp;

88
// int variables we need to pull from data
thallia's avatar
thallia committed
89
//int xpValue = 1000; // initial XP value we should get from data
90
// map(value, fromLow, fromHigh, toLow, toHigh)
thallia's avatar
thallia committed
91
//int xpDisplay = map(xp, 0, 1800, 20, 140); // mapped to pixels so we don't overwhelm the screen
92 93

// was the day button triggered?
94
boolean dayButtonWork = true;
thallia's avatar
thallia committed
95
boolean touched = false;
96 97
boolean trial;
boolean keepGoing = true;
98

thallia's avatar
thallia committed
99
int dayChosen; // so we know what day we gets
100
boolean breakDisplayed = false;
101 102 103 104

// **********************************
//   Init RA8875 Class& Touch Inputs
// **********************************
thallia's avatar
thallia committed
105
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET); // assigns the ra8875 call to tft so we don't have to type it all out every time
106 107
uint16_t tx, ty; // touch inputs

108 109
char levelArray[3];
char xpArray[5];
thallia's avatar
thallia committed
110
JsonArray desChores;
111 112

int xpDisplay;
thallia's avatar
thallia committed
113 114 115
int cursor_x = 185;
int cursor_y = 120;

thallia's avatar
thallia committed
116 117 118 119 120 121 122
boolean completedChore_1 = false;
boolean completedChore_2 = false;
boolean completedChore_3 = false;
boolean completedChore_4 = false;
boolean completedChore_5 = false;
boolean completedChore_6 = false;

123 124 125
// ************************
//  Setup Code
// ************************
thallia's avatar
thallia committed
126 127 128

void setup()
{
129
  Serial.begin(9600); // Begin the serial monitor!
130

thallia's avatar
thallia committed
131 132 133
  if(!SD.begin(SD_CS)) {
    while(1);
  }
thallia's avatar
thallia committed
134 135
  Serial.println(F("\n SD Card successfully Initialized."));
  Serial.println(availableMemory());
thallia's avatar
thallia committed
136

thallia's avatar
thallia committed
137 138 139
  if(SD.exists("/json/")){
    Serial.print(F("this worked\n"));
    //Serial.print(availableMemory());
thallia's avatar
thallia committed
140 141 142
  }

  File current_child;
143
  StaticJsonDocument<138> json_obj;
thallia's avatar
thallia committed
144

thallia's avatar
thallia committed
145
  Serial.print(F("------reading for files...-----\n"));
146
  current_child = SD.open("/json/child1.txt", FILE_READ);
thallia's avatar
thallia committed
147 148 149 150
  if(!current_child){
    Serial.println(F("ERRFILE"));
    while(1);
  }
151
  Serial.print(availableMemory());
thallia's avatar
thallia committed
152

thallia's avatar
thallia committed
153 154 155 156
// open selected child
  //current_child = SD.open("/json/child-1.json");

  // display the file data
thallia's avatar
thallia committed
157
   /*
thallia's avatar
thallia committed
158 159 160
  while(current_child.available()){
    Serial.write(current_child.read());
  }
thallia's avatar
thallia committed
161

thallia's avatar
thallia committed
162
// Open into json doc
thallia's avatar
thallia committed
163 164
  current_child.seek(0); */

thallia's avatar
thallia committed
165
  DeserializationError error = deserializeJson(json_obj, current_child); // check error below please
thallia's avatar
thallia committed
166

167 168 169 170
//  if(error){
  //  Serial.println(error.c_str());
    //while(1);
  //}
thallia's avatar
thallia committed
171 172 173 174
  // (You can use the StaticJsonDocument to pull data from 'file' (memory) now)
  // How to access data now
  desChores = json_obj["chores"].as<JsonArray>(); // access an array

thallia's avatar
thallia committed
175 176 177 178 179
  // get all the data
  firstName = json_obj["name"]; // access string value
  lastName = json_obj["lastname"];
  lvl = json_obj["level"]; // access integer value
  xp = json_obj["xp"];
180 181 182 183 184 185

  Serial.println("\nLevel: " + String(lvl));
  Serial.println("xp: " + String(xp));

  String(lvl).toCharArray(levelArray, 3);
  String(xp).toCharArray(xpArray, 5);
thallia's avatar
thallia committed
186 187 188
// int variables we need to pull from data
//int xpValue = 1000; // initial XP value we should get from data
// map(value, fromLow, fromHigh, toLow, toHigh)
189
xpDisplay = map(xp, 0, 1800, 20, 140); // mapped to pixels so we don't overwhelm the screen
190

thallia's avatar
thallia committed
191
  Serial.println(F("RA8875 start")); // display that the RA8875 is ON
thallia's avatar
thallia committed
192 193 194

  /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
  if (!tft.begin(RA8875_800x480)) { // make it 800x480 because that's the size of our touchscreen
thallia's avatar
thallia committed
195
    Serial.println(F("RA8875 Not Found!")); // if the initialization fails -- typically incorrect wiring
thallia's avatar
thallia committed
196 197 198
    while (1); // won't print until it initializes correct
  }

thallia's avatar
thallia committed
199
  Serial.println(F("Found RA8875")); // initialization success
thallia's avatar
thallia committed
200 201 202 203 204 205 206 207

  tft.displayOn(true); // turn display on
  tft.GPIOX(true);      //
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  tft.PWM1out(255);

  // this will be the start of the homeScreen function
  tft.fillScreen(RA8875_BLACK); // fills screen with blackness of death
thallia's avatar
thallia committed
208
  current_child.close();
209 210
}

211 212 213 214
// ************************
//  Main Loop
// ************************

215 216
void loop () {

217 218 219 220
  switch (currentState) { // currentState is going to be displaying the homescreen

    case HOMESCREEN:
      // display the homescreen
221 222 223 224 225
      if(!breakDisplayed){
        homeScreen();
      } else {
        break;
      }
226 227

    case CHORES: // next state will be if they press the chore display button
228
      // display the child data and ask what day of chores they want to see
229 230 231 232 233 234

    if(!breakDisplayed){
        choreList();
      } else {
        break;
      }
235

236 237
    case DAY:
      // this is going to, based on the day picked, display the chores due that day
238 239 240 241 242
      if(!breakDisplayed){
        displayDayChores();
      } else {
        break;
      }
243

244 245
    // if i actually had time to complete this and space wasn't an issue, this is where the NEWCHORE
    // function would go.
246 247 248
    //case NEWCHORE:

  }
249 250
}

251 252 253 254 255 256
// ************************
//  User Defined Functions
// ************************

// CHECK TOUCH FUNCTION
void checkTouch() { // see if the screen has been touched, and if so, where?
257 258
  while ((tx == 0) && (ty == 0)){
    if (tft.touched() == true) {
thallia's avatar
thallia committed
259

260 261 262
        tft.touchRead(&tx, &ty); // gets x and y coordinates of where it was touched
        Serial.print("X: " + String(tx)); // prints coordinates for debugging
        Serial.println(", Y: " + String(ty));
thallia's avatar
thallia committed
263

264
    }
265
  }
266 267
}

268 269 270 271 272 273 274
void writeText(int pixDistance, const char* labelName, const char* numBer){
  tft.textSetCursor(20, pixDistance); // provide the distance needed
  tft.textWrite(labelName); // the thing you're gonna write that prefixes a number
  tft.textWrite(numBer); // the number that comes after
  // you'll get something like "Level: 30"
}

thallia's avatar
thallia committed
275
void writeDay(int day, int textCursorX, int textCursorY, int xCoord, int yCoord){
276
    // we're gonna draw the rectangles
thallia's avatar
thallia committed
277
    tft.fillRect(xCoord, yCoord, 175, 75, RA8875_BLUE);
278 279 280 281 282 283 284 285 286

    tft.textTransparent(RA8875_WHITE);
    tft.textSetCursor(textCursorX, textCursorY);
    tft.textWrite(daysOfTheWeek[day]);
    //tft.textSetCursor(215, 150);
    //tft.textWrite(daysOfTheWeek[0]);

}

287
void writeDayChosen(){
thallia's avatar
thallia committed
288

thallia's avatar
thallia committed
289 290
  hasDisplayed = true;
  while(hasDisplayed){
thallia's avatar
thallia committed
291
      Serial.print(F("day button worked\n")); // prints if a button was pressed for debugging
thallia's avatar
thallia committed
292
      hasDisplayed = false; // so we don't PWM the actual display text
293 294
      Serial.print("Day: " + String(dayChosen) + "\n"); // debugging statement
      dayButtonWork = false; // did the button print? if so, break out of the while loop
295
      breakDisplayed = false;
thallia's avatar
thallia committed
296
      currentState = DAY; // go to the next state
297 298
      trial = false;

299 300 301
  }
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
void outlineAndProfile(){
  // outline
  tft.drawRect(10, 10, 780, 460, RA8875_BLUE);
  tft.drawRect(10, 10, 165, 460, RA8875_BLUE); // draw the rectangle for the kid's profile
  tft.drawRect(10, 10, 165, 80, RA8875_BLUE);
  tft.fillRect(10, 90, 780, 25, RA8875_BLUE); // break bar
  tft.drawRect(20, 190, 140, 15, RA8875_GREEN); // xp bar outline
  tft.drawRect(20, 850, 145, 60, RA8875_WHITE); // back button

  // this was cool but i don't need it but i'm so impressed i figured it out i don't wanna delete it
  //int currentWidth = 165; // the current place there's a rectangle
  //for(int x = 5; x > 1; x--){
    //  currentWidth = currentWidth + 125; // add 125 pixels to each rectangle
      //tft.drawRect(10, 10, currentWidth, 460, RA8875_BLUE); // draw the rectangle for each day
  //}

  // display all the text!
  tft.textMode();
  tft.textTransparent(RA8875_WHITE); // make the text the color that we want it to be
  tft.textEnlarge(1); // for some reason this is needed so we don't get weird symbols

  // where we will display the first name
  tft.textSetCursor(20, 20); // set where the text will start
  tft.textWrite(firstName); // write the text

  // last lastName
  tft.textSetCursor(20, 50); // where the text will start
  tft.textWrite(lastName); // write the text

  // back button
  tft.textSetCursor(60, 860);
  tft.textWrite(backButton);

  // Level prefix + number
336
  writeText(120, level, levelArray); // this function prevents code bloat, it does what's commented out below
337 338
  //tft.textSetCursor(20, 120);
  //tft.textWrite(level);
thallia's avatar
thallia committed
339
  //tft.textWrite(lvl);
340 341

  // experience
thallia's avatar
thallia committed
342
  String(xp).toCharArray(xpArray, 5);
343
  writeText(150, eXPerience, xpArray);
344 345 346 347 348 349
  //tft.textSetCursor(20, 150);
  //tft.textWrite(eXPerience);
  //tft.textWrite(xP);

  // map function for xp bar
  // map(value, fromLow, fromHigh, toLow, toHigh)
350
  tft.fillRect(20, 190, xpDisplay, 15, RA8875_GREEN); // draw the XP bar
351 352 353

}

354
void choreRectDisplay(){
thallia's avatar
thallia committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  int dist = 85;

  int sq_y = 130;

  tft.drawRect(175, 90, 310, 380, RA8875_BLUE);
  for (int w = 5; w > 0; w--){
    tft.drawRect(175, 90, 310, dist, RA8875_BLUE);
    dist = dist + 60;
  }

  for (int w = 5; w >= 0; w--){
    tft.drawRect(620, sq_y, 40, 40, RA8875_GREEN);
    sq_y = sq_y + 58;
  }

  /*int dist = 205;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

  for (int w = 2; w > 0; w--){

    tft.drawRect(175, 90, dist, 380, RA8875_BLUE);
    dist = dist + 205;
    //tft.drawRect(175, 90, 410, 380, RA8875_BLUE);
    //tft.drawRect(175, 90, 615, 380, RA8875_BLUE);
  }
  int yDown = 115;
  for (int y = 2; y >= 0; y--){
    tft.drawRect(175, yDown, 205, 118, RA8875_BLUE);
    yDown = yDown + 118;

  }
  yDown = 115;
  for (int y = 2; y >= 0; y--){
    tft.drawRect(380, yDown, 205, 118, RA8875_BLUE);
    yDown = yDown + 118;

  }

  yDown = 115;
  for (int y = 2; y >= 0; y--){
    tft.drawRect(585, yDown, 205, 118, RA8875_BLUE);
    yDown = yDown + 118;

thallia's avatar
thallia committed
397
  }*/
398 399 400 401 402 403 404 405



}

// *************************************
//    HOME SCREEN DISPLAY
// *************************************
406 407
void homeScreen()
{
408 409

  tft.fillScreen(RA8875_BLACK);
thallia's avatar
thallia committed
410
  //           x   y   width height  color
411
  tft.drawRect(10, 10, 780, 460, RA8875_BLUE);
thallia's avatar
thallia committed
412 413
  tft.fillRect(50, 150, 300, 200, RA8875_BLUE); // left button
  tft.fillRect(450, 150, 300, 200, RA8875_MAGENTA); // right button
414 415 416
  tft.drawRect(10, 10, 165, 80, RA8875_BLUE);
  tft.drawRect(10, 10, 780, 80, RA8875_BLUE);

thallia's avatar
thallia committed
417 418

  // first button text
419 420 421 422 423 424
  tft.textMode(); // make sure we're in text mode or it won't display
  char firstButton[] = "Chores "; // initialize what we want to display in an array
  tft.textSetCursor(125, 215); // x & y where the text will start
  tft.textTransparent(RA8875_WHITE); // choose the text color
  tft.textEnlarge(2); // make sure this is the right size or it won't print correctly
  tft.textWrite(firstButton); // write what we stored in the array
thallia's avatar
thallia committed
425 426

  // second button text
427 428 429 430 431 432
  char secondButton[] = "New Chore "; // store what we want to display in the second array
  tft.textSetCursor(495, 215); // set the x and y coordinates for the second secondButton
  tft.textTransparent(RA8875_WHITE); // choose the text color
  tft.textEnlarge(2); // size the text
  tft.textWrite(secondButton); // write the text

433 434 435 436 437 438 439 440 441 442 443 444 445
  // Kid's profile
  tft.textEnlarge(1); // for some reason this is needed so we don't get weird symbols
  // where we will display the first name
  tft.textSetCursor(20, 20); // set where the text will start
  tft.textWrite(firstName); // write the text

  // last lastName
  tft.textSetCursor(20, 50); // where the text will start
  tft.textWrite(lastName); // write the text

  // Level prefix + number
  tft.textSetCursor(190, 35);
  tft.textWrite(level);
446
  tft.textWrite(levelArray);
447 448 449 450

  // experience
  tft.textSetCursor(500, 20);
  tft.textWrite(eXPerience);
451
  tft.textWrite(xpArray);
452 453 454

  // xp
  tft.drawRect(500, 60, 140, 15, RA8875_GREEN); // xp bar outline
455
  tft.fillRect(500, 60, xpDisplay, 15, RA8875_GREEN); // draw the XP bar
456

457
  hasDisplayed = true; // make this true so we don't print a million things to the serial monitor
458
  breakDisplayed = true;
thallia's avatar
thallia committed
459 460 461

  tft.touchEnable(true); // enables touchscreen

462
  while(hasDisplayed == true){ // while we have the homescreen displayed, do this stuff
463 464 465 466 467 468
        tx = 0;
        ty = 0;
        checkTouch(); // reads previous touch thingy
        tx = 0;
        ty = 0;
        checkTouch();
thallia's avatar
thallia committed
469
    if (((tx >= 50) && (tx <= 300)) && (ty >= 215)) { // if touched within the first button bounds...
470

thallia's avatar
thallia committed
471
        Serial.print(F("this worked\n")); // debugging statement so we know it worked
472
        hasDisplayed = false; // make this false for the next state
473
        breakDisplayed = false;
474
        currentState = CHORES; // go to display the chores so we can obey the button press
thallia's avatar
thallia committed
475

476 477 478
      }
    }
}
479
// ********************************
480
// CHORE LIST FUNCTION
481
// ********************************
482 483 484
void choreList() {

    Serial.println("Current State: " + String(currentState));
thallia's avatar
thallia committed
485 486 487
    delay(500);
    //tft.touchEnable(false);
    //tft.touchEnable(true);
488
    tft.fillScreen(RA8875_BLACK); // clear the screen
489
    outlineAndProfile();
490 491 492 493 494

    // day to display
    tft.textSetCursor(240, 50);
    tft.textTransparent(RA8875_WHITE);
    tft.textWrite(dayDisplay);
495

496 497
    // make text the size we need it to be for the buttons
    tft.textEnlarge(1);
498

499
    // monday
thallia's avatar
thallia committed
500
    writeDay(0, 215, 160, 200, 140); // args: day, text X, text Y, rectangle x & y
501 502 503 504 505 506 507 508 509 510 511 512 513 514
    // Tuesday
    writeDay(1, 215, 250, 200, 230);
    // Wednesday
    writeDay(2, 215, 340, 200, 320);
    // Thursday
    writeDay(3, 415, 160, 400, 140);
    // Friday
    writeDay(4, 415, 250, 400, 230);
    // Saturday
    writeDay(5, 415, 340, 400, 320);
    // Sunday
    writeDay(6, 615, 250, 600, 230);

    // check for which day button has been pressed!
515
    hasDisplayed = true;
516 517
    breakDisplayed = true;
    dayButtonWork = true;
thallia's avatar
thallia committed
518

519
      // these will test if any of the buttons have been triggered
520
      while(dayButtonWork) {
thallia's avatar
thallia committed
521
        //Serial.print("are we getting here");
522 523 524 525 526 527 528 529 530 531 532 533 534 535
        tx = 0;
        ty = 0;
        checkTouch(); // reads previous touch thingy
        tx = 0;
        ty = 0;
        checkTouch();
        Serial.print("new tx: " + String(tx)); // prints them for debugging purposes
        Serial.print(", new ty: " + String(ty) + "\n");
        if (tx != 0 && ty !=0){ // if the new touches aren't at 0 coordinates, move on!
          trial = true;
        }

        // this was once useful code but it sucked so i made something better
        /*while(hasDisplayed == true){
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
          if (tft.touched()){
            tft.touchRead(&tx, &ty);
          }


          while(!tft.touched() == keepGoing){

            for (int x = 0; x <= 150; x++){
              Serial.print("tx: " + String(tx));
              Serial.print(", ty: " + String(ty) + "\n");
              if (((oldTx <= tx) || (oldTx >= tx)) || ((oldTy <= ty) || (oldTy >= ty))) {
                tft.touchRead(&tx, &ty);
                trial = true;
              }
            }
            keepGoing = false;
552
          }*/
553 554

          while(trial){
555 556 557 558
            checkTouch();
            tx = 0;
            ty = 0;
            checkTouch();
559

560 561 562 563 564 565 566 567 568 569 570 571 572
            if ((tx >= 20) && (tx <= 165) && (ty >= 700)) {
              // we chose the back button here!
              currentState = HOMESCREEN;
              Serial.print("chose to go back to homescreen");
              Serial.print("3rd tx: " + String(tx));
              Serial.println(", 3rd ty: " + String (ty));
              trial = false;
              dayButtonWork = false;
              tx = 0;
              ty = 0;
              breakDisplayed = false;

            } else if ((tx >= 250) && (tx <= 500) && (ty >= 360) && (ty <= 515)){
thallia's avatar
thallia committed
573

574 575
              dayChosen = 0;
              writeDayChosen();
thallia's avatar
thallia committed
576

577
            } else if ((tx >= 250) && (tx <= 500) && (ty >= 515) && (ty <= 675)) {
thallia's avatar
thallia committed
578

579 580
              dayChosen = 1;
              writeDayChosen();
thallia's avatar
thallia committed
581

582
            } else if ((tx >= 250) && (tx <= 500) && (ty >= 675)) {
thallia's avatar
thallia committed
583

584 585
              dayChosen = 2;
              writeDayChosen();
thallia's avatar
thallia committed
586

587
            } else if ((tx >= 500) && (tx <= 730) && (ty >= 360) && (ty <= 515)) {
thallia's avatar
thallia committed
588

589 590
              dayChosen = 3;
              writeDayChosen();
thallia's avatar
thallia committed
591

592
            } else if ((tx >= 500) && (tx <= 730) && (ty >= 515) && (ty <= 675)) {
thallia's avatar
thallia committed
593

594 595
              dayChosen = 4;
              writeDayChosen();
thallia's avatar
thallia committed
596

597
            } else if ((tx >= 500) && (tx <= 730) && (ty >= 675)) {
thallia's avatar
thallia committed
598

599 600
              dayChosen = 5;
              writeDayChosen();
thallia's avatar
thallia committed
601

602
            } else if ((tx >= 730) && (ty >= 515)) {
thallia's avatar
thallia committed
603

604 605 606
              dayChosen = 6;
              writeDayChosen();
            }
607

608
          }
609 610

      }
611

612
    //}
613

614
  }
thallia's avatar
thallia committed
615
void displayTextChores(const char* choreForThatDay, int x, int y){
616 617

  tft.textMode();
thallia's avatar
thallia committed
618
  tft.textSetCursor(cursor_x, cursor_y);
619 620
  tft.textEnlarge(1);
  tft.textTransparent(RA8875_WHITE);
thallia's avatar
thallia committed
621 622
  tft.textWrite(choreForThatDay);
}
623

624 625 626
// ****************************
//    DISPLAY CHORES
// ****************************
627 628 629 630 631 632 633 634
void displayDayChores(){
  /*
     this is where we'll have it determine whether or not the kind
     chose Monday/Tuesday/Wednesday/Thurs/Fri/Sat/Sun to display the
     chores, and then it'll get the JSON data and display the chores
     for that day in particular so we don't have a bunch of repeating
     code.
  */
635

thallia's avatar
thallia committed
636
  Serial.println(F("\nWe're into display chores!"));
637 638 639 640

  tft.fillScreen(RA8875_BLACK); // clears the screen

  outlineAndProfile(); // repetitive drawing code
thallia's avatar
thallia committed
641

642
  // displays day chosen
643
  tft.textMode();
644
  tft.textSetCursor(400, 25);
645 646 647
  tft.textEnlarge(2);
  tft.textTransparent(RA8875_WHITE);
  tft.textWrite(daysOfTheWeek[dayChosen]);
648

649
  choreRectDisplay();
thallia's avatar
thallia committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
  cursor_y = 130;
  for(JsonVariant v : desChores) {

    displayTextChores(v.as<const char*>(), cursor_x, cursor_y);
    Serial.println(v.as<const char*>());

    cursor_y = cursor_y + 60;

  }

  //for (int i = 6; i >= 0; i--){
    //Serial.print("Day chosen: " + String(dayChosen));
    //if (dayChosen == i){
      //displayTextChores(i, 205, 90);
    //}
  //}
thallia's avatar
thallia committed
666
  if (completedChore_1){
thallia's avatar
thallia committed
667

thallia's avatar
thallia committed
668 669 670 671 672 673 674
    tft.fillRect(620, 130, 40, 40, RA8875_GREEN);

  }
 if (completedChore_2){

      tft.fillRect(620, 188, 40, 40, RA8875_GREEN);
    }
675

676 677

  hasDisplayed = true;
thallia's avatar
thallia committed
678
  while (hasDisplayed){
679 680 681 682 683 684
    tx = 0;
    ty = 0;
    checkTouch(); // reads previous touch thingy
    tx = 0;
    ty = 0;
    checkTouch();
thallia's avatar
thallia committed
685 686 687 688 689 690 691

    if(((tx <= 790) && (tx >= 775)) && ((ty <= 370) && (ty >= 350))){

        tft.fillRect(620, 130, 40, 40, RA8875_GREEN);
        xp = xp + 20;
        completedChore_1 = true;

thallia's avatar
thallia committed
692
    } else if ((tx <= 790) && (tx >= 775) && (ty <= 470) && (ty >= 450)){
thallia's avatar
thallia committed
693 694 695 696 697

        tft.fillRect(620, 188, 40, 40, RA8875_GREEN);
        xp = xp + 20;
        completedChore_2 = true;

thallia's avatar
thallia committed
698
    }
thallia's avatar
thallia committed
699 700


701 702 703
    if ((tx >= 20) && (tx <= 165) && (ty >= 700)) {
    // we chose the back button here!
      currentState = CHORES;
thallia's avatar
thallia committed
704
      Serial.print(F("chose to go back to chores"));
705 706 707 708
      Serial.print("3rd tx: " + String(tx));
      Serial.println(", 3rd ty: " + String (ty));
      trial = false;
      dayButtonWork = false;
709 710
      hasDisplayed = false;
      breakDisplayed = false;
711 712
      tx = 0;
      ty = 0;
thallia's avatar
thallia committed
713
      //completedChore = false;
714

715 716
    }
 }
thallia's avatar
thallia committed
717 718


719

thallia's avatar
thallia committed
720
}
thallia's avatar
thallia committed
721 722 723 724 725 726 727 728

int availableMemory() {
  int size = 2048; // Use 2048 with ATmega328
  byte *buf;
  while ((buf = (byte *) malloc(--size)) == NULL);
  free(buf);
  return size;
}