Gavin
Published

Todolist Sync Monitor

Sync with [Todoist] website, Get the To do list and display on the Screen(ILI9488), You can update the task list when finish this task.

AdvancedFull instructions provided1,237
Todolist Sync Monitor

Things used in this project

Hardware components

RP2040
Raspberry Pi RP2040
×1
WizFi360-EVB-Pico
WIZnet WizFi360-EVB-Pico
×1
ILI9488
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Todolist Sync Monitor_Schmatic

Code

Todolist Sync Monitor_Arduino_Code

Arduino
Todolist Sync Monitor_Arduino_Code
#include "WizFi360.h"
#include "icon.h"
#include <ArduinoJson.h>

#include <Arduino_GFX_Library.h>
Arduino_GFX *tft = create_default_Arduino_GFX();

#include <PNGdec.h>
PNG png;
uint8_t image_x;
uint8_t image_y;

//#define debug_msg
#define buttonPin 14
#define ledPin    12
#define backLightPin 22

typedef enum 
{
  send_project_request = 0,
  get_project_list,
  send_sections_request,
  get_sections_list,
  send_task_request, 
  get_task_list,
  display_todolist,
  todolist_close_task, 
  show_close_result, 
}STATE_;
STATE_ currentState;

/* Wi-Fi info */
char ssid[] = "wiznet";       // your network SSID (name)
char pass[] = "******";   // your network password
IPAddress ip;

char todoist_server[] = "api.todoist.com"; 
char todoist_token[] = "837a9777cbe10ad8f1255fd7c907e2*******";

#define todoist_project_max   16
#define todoist_sections_max  32
#define todoist_task_max      128

struct _todoist_project{
  boolean Exist = false;
  String ID;
  String Name;
  String Color_Str;
  String Url; 
  uint8_t Start;
  uint8_t End;
} ;
_todoist_project todoist_project[todoist_project_max];
uint8_t todoist_project_num = 0;
uint8_t todoist_project_cnt = 0;

struct _todoist_sections{
  boolean Exist = false;
  String ID;
  String Name;
  String Project;
  uint8_t Start;
  uint8_t End;
};
_todoist_sections todoist_sections[todoist_sections_max];
uint16_t todoist_sections_num;
uint16_t todoist_sections_cnt = 0;

struct _todoist_task{
  boolean Exist = false;
  boolean Finished;
  String ID;
  String Section;
  String Parent;
  String Name;
};
_todoist_task todoist_task[todoist_task_max];

struct _todoist_task_select{
  uint8_t x;
  uint16_t y;
  String ID;
  String Name;
};
_todoist_task_select todoist_task_select[16];

struct _todoist_task_close{
  String ID;
  String Name;
};
_todoist_task_close todoist_task_close;

uint16_t todoist_task_num;
uint16_t todoist_task_cnt = 0;

uint16_t todoist_task_len = 0;
uint8_t display_task_num = 0;

uint8_t data_now = 0; 
String json_String; 
uint16_t dataStart = 0;
uint16_t dataEnd = 0;
String dataStr;
uint8_t jump_out_flag = 0; 
uint8_t display_time_cnt = 0;
boolean yes_or_no = false;
boolean display_close_int = false;
// variables will change:
bool buttonState = false;     // variable for reading the pushbutton status

int status = WL_IDLE_STATUS;  // the Wifi radio's status

// Initialize the Ethernet client object
WiFiClient client;
    
void setup() {
  // initialize serial for debugging  
  Serial.begin(115200);

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT); 
  digitalWrite(ledPin, LOW); 
  
  tft->begin();
  tft->fillScreen(WHITE);
  pinMode(backLightPin, OUTPUT); 
  digitalWrite(backLightPin, HIGH); 
  
  display_logo(240,120,BLACK);
  image_x = 140;
  image_y = 165;
  display_todoist_icon(1);
  display_wifi_status(240,250);

  // initialize serial for WizFi360 module
  Serial2.setFIFOSize(4096);
  Serial2.begin(2000000);
  WiFi.init(&Serial2);
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    // don't continue
    while (true);
  }
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }
  display_wifi_status(240,250);
//  while (!Serial) {
//    ; // wait for serial port to connect. Needed for native USB port only
//  }  
  // print your WiFi shield's IP address
  ip = WiFi.localIP();
  currentState = send_project_request;
  //currentState = todolist_close_task;
  attachInterrupt(digitalPinToInterrupt(buttonPin), onChange, CHANGE); 
}

void loop(){
  switch(currentState){
    case send_project_request:
       {
        // if you get a connection, report back via serial
        if (client.connectSSL(todoist_server,443)) {
          delay(3000);
          // Make a HTTP request          
          client.println(String("GET /rest/v2/projects HTTP/1.1"));
          client.println(String("Host:") + String(todoist_server));
          client.println(String("Authorization: Bearer ") + String(todoist_token));          
          client.println("Connection: close");
          client.println();
          json_String= "";
          currentState = get_project_list;
        }
        else
        {
          client.stop();
          delay(1000);
        }
       }
      break;
      
    case get_project_list:
       {          
          while (client.available()) {
            json_String += (char)client.read();
            data_now =1; 
          }          
          if(data_now)
          {
#ifdef debug_msg
            Serial.println(json_String); 
#endif  
            for(uint8_t i=0; i<todoist_project_max; i++)
            {
              todoist_project[i].Exist = true;
              dataStart = json_String.indexOf("\"id\": \"",dataEnd) + strlen("\"id\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_project[i].ID = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_project.ID:");
              Serial.println(todoist_project[i].ID);
#endif  
              dataStart = json_String.indexOf("\"color\": \"", dataEnd) + strlen("\"color\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_project[i].Color_Str = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_project.Color_Str:");
              Serial.println(todoist_project[i].Color_Str);
#endif  
              dataStart = json_String.indexOf("\"name\": \"", dataEnd) + strlen("\"name\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_project[i].Name = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_project.Name:");
              Serial.println(todoist_project[i].Name);
#endif  
              dataStart = json_String.indexOf("\"url\": \"", dataEnd) + strlen("\"url\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_project[i].Url = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_project.Url:");
              Serial.println(todoist_project[i].Url);
#endif      
              if((dataEnd+40)> json_String.length())
              {
                todoist_project_num = i+1;
                data_now =0; 
                client.stop();
                delay(3000);
                currentState = send_sections_request;
                return;
              }
            } 
          }
       }
      break;
      
    case send_sections_request:
       {
          if (client.connectSSL(todoist_server,443)) {
          delay(3000);
          // Make a HTTP request          
          client.println(String("GET /rest/v2/sections HTTP/1.1"));
          client.println(String("Host: ") + todoist_server);
          client.println(String("Authorization: Bearer ") + String(todoist_token));          
          client.println("Connection: close");
          client.println();
          json_String= "";
          currentState = get_sections_list;       
          }
          else
          {
            client.stop();
            delay(1000);
          }
       }
      break;

    case get_sections_list:
       {
          while (client.available()) {
            json_String += (char)client.read();
            data_now = 1; 
            dataEnd = 0;
          }          
          if(data_now)
          {
#ifdef debug_msg
            Serial.println(json_String); 
#endif
            for(uint8_t m=0; m<todoist_sections_max; m++)
            {
              todoist_sections[m].Exist = true;
              dataStart = json_String.indexOf("\"id\": \"",dataEnd) + strlen("\"id\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_sections[m].ID = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_sections.ID:");
              Serial.println(todoist_sections[m].ID);
#endif
              dataStart = json_String.indexOf("\"project_id\": \"",dataEnd) + strlen("\"project_id\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_sections[m].Project = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_sections.Project:");
              Serial.println(todoist_sections[m].Project);
#endif
              dataStart = json_String.indexOf("\"name\": \"",dataEnd) + strlen("\"name\": \"");
              dataEnd = json_String.indexOf("\"", dataStart); 
              todoist_sections[m].Name = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_sections.Name:");
              Serial.println(todoist_sections[m].Name);
#endif   
              if((dataEnd+40)> json_String.length())
              {
                todoist_sections_num =m+1;
                data_now =0; 
                client.stop();
                delay(3000);
                currentState = send_task_request;
                return;
              }
            }
         }
       }
      break;
      
    case send_task_request:
      {
        if (client.connectSSL(todoist_server,443)) {
        delay(3000);
        // Make a HTTP request          
        client.println(String("GET /rest/v2/tasks HTTP/1.1"));
        client.println(String("Host: ") + todoist_server);
        client.println(String("Authorization: Bearer ") + String(todoist_token));          
        client.println("Connection: close");
        client.println();
        json_String= "";
        currentState = get_task_list;       
        }
        else
        {
          client.stop();
          delay(1000);
        }
      }
      break;
      
    case get_task_list:
      {
         while (client.available()) {
            json_String += (char)client.read();
            data_now = 1; 
            dataEnd = 0;
          }          
          if(data_now)
          {
            dataStart = json_String.indexOf("Content-Length: ") + strlen("Content-Length: ");
            dataEnd = json_String.indexOf("\r\n", dataStart); 
            dataStr = json_String.substring(dataStart, dataEnd);
            dataStart = json_String.indexOf("\r\n\r\n", dataEnd); 
            todoist_task_len = dataStr.toInt()-(json_String.length() - dataStart - 4);//HexStr2Int(dataStr)-(json_String.length() - dataEnd - 4);
#ifdef debug_msg
            Serial.print("todoist_task_len:");
            Serial.println(todoist_task_len);
#endif  
            while(todoist_task_len)
            {
               while (client.available()) {
                json_String += (char)client.read();
                todoist_task_len --;
              }
            }
#ifdef debug_msg
            Serial.println(json_String); 
#endif
            for(uint8_t n=0; n<todoist_task_max; n++)
            {
              todoist_task[n].Exist = true;
              dataStart = json_String.indexOf("\"id\": \"",dataEnd) + strlen("\"id\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_task[n].ID = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_task.ID:");
              Serial.println(todoist_task[n].ID);
#endif  
              dataStart = json_String.indexOf("\"section_id\": ",dataEnd) + strlen("\"section_id\": ")+1;
              dataEnd = json_String.indexOf(",", dataStart)-1; 
              todoist_task[n].Section = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_task.section_id:");
              Serial.println(todoist_task[n].Section);
#endif  
              dataStart = json_String.indexOf("\"parent_id\": ",dataEnd) + strlen("\"parent_id\": ")+1;
              dataEnd = json_String.indexOf(",", dataStart)-1; 
              todoist_task[n].Parent = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_task.Parent_id:");
              Serial.println(todoist_task[n].Parent);
#endif  
              dataStart = json_String.indexOf("\"content\": \"",dataEnd) + strlen("\"content\": \"");
              dataEnd = json_String.indexOf("\",", dataStart); 
              todoist_task[n].Name = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg 
              Serial.print("todoist_task.Name:");
              Serial.println(todoist_task[n].Name);
#endif  
              dataStart = json_String.indexOf("\"is_completed\": ",dataEnd) + strlen("\"is_completed\": ");
              dataEnd = json_String.indexOf(",", dataStart); 
              dataStr = json_String.substring(dataStart, dataEnd);
#ifdef debug_msg
              Serial.print("todoist_task.Finished:");
              Serial.println(dataStr);
#endif  
              if(dataStr == "true"){todoist_task[n].Finished = true;}
              dataEnd = json_String.indexOf("\"url\": ",dataEnd) + strlen("\"url\": ");
              
              if((dataEnd+60)> json_String.length())
              {
                todoist_task_num =n+1;
                data_now =0; 
                client.stop();
                currentState = display_todolist;
                return;
              }
            }
         }
      }
      break;
      
    case display_todolist:
     {  
        uint8_t display_row_num = 0;
        uint8_t display_page_num = 0;
        uint8_t display_string_len = 0;
        String display_string;
        display_task_num = 0;
        display_dashboard(); 
        for(uint8_t i=0; i<todoist_project_num; i++)
        {
           if(jump_out_flag)
           {
            jump_out_flag =0;
            return;
           }
#ifdef debug_msg
          Serial.print("todoist_project_num:");
          Serial.println(i);
#endif  
          if(todoist_project[i].Exist)
          {
            tft->fillCircle(12,26+(20*display_row_num),5,Str2RGB565(todoist_project[i].Color_Str));
            tft->setTextColor(Str2RGB565(todoist_project[i].Color_Str));
            tft->setTextSize(2);
            
            display_string_len = todoist_project[i].Name.length();
            if(display_string_len > 38)
            {
              while(display_string_len > 38)
              {
                display_string = todoist_project[i].Name.substring(todoist_project[i].Name.length()- display_string_len, todoist_project[i].Name.length()-display_string_len+38);
                display_string_len -= 38;
                tft->setCursor(24, 20+(20*display_row_num));
                tft->print(String("[") + display_string + String("]"));
                display_row_num++;
              }
              display_string = todoist_project[i].Name.substring(todoist_project[i].Name.length()-display_string_len, todoist_project[i].Name.length());
              tft->setCursor(24, 20+(20*display_row_num));
              tft->print(String("[") + display_string + String("]"));
              display_row_num++;
            }
            else
            {
              tft->setCursor(24, 20+(20*display_row_num));
              tft->print(String("[") + todoist_project[i].Name + String("]"));
              display_row_num++;
            }            
            for(uint8_t m=0; m<todoist_sections_num; m++)
            {
              if(jump_out_flag)
              {
                jump_out_flag =0;
                return;
              }
              if(todoist_sections[m].Exist)
              {
                if(todoist_sections[m].Project == todoist_project[i].ID)
                {
#ifdef debug_msg
                  Serial.print("todoist_sections_num:");
                  Serial.println(m);
#endif  
                  display_string_len = todoist_sections[m].Name.length();
                  if(display_string_len > 38)
                  {
                    while(display_string_len > 38)
                    {
                      display_string = todoist_sections[m].Name.substring(todoist_sections[m].Name.length()- display_string_len, todoist_sections[m].Name.length()-display_string_len+38);
                      display_string_len -= 38;
                      tft->setCursor(24, 20+(20*display_row_num));  
                      tft->print(display_string);
                      display_row_num++;
                      if(display_row_num == 15)
                      {
                        display_row_num =0; 
                        wait_task_finish_click();display_dashboard();
                      }
                    }
                    display_string = todoist_sections[m].Name.substring(todoist_sections[m].Name.length()- display_string_len, todoist_sections[m].Name.length());
                    tft->setCursor(24, 20+(20*display_row_num));  
                    tft->print(display_string);
                    display_row_num++;
                    if(display_row_num == 15)
                    {
                      display_row_num =0; 
                      wait_task_finish_click();display_dashboard();
                    }
                  }
                  else
                  {
                    tft->setCursor(24, 20+(20*display_row_num));  
                    tft->print(todoist_sections[m].Name); 
                    display_row_num++;    
                    if(display_row_num == 15)
                    {
                      display_row_num =0; 
                      wait_task_finish_click();display_dashboard();
                    }
                  }
                  tft->setTextColor(DARKGREY);
                  for(uint8_t n=0; n<todoist_task_num; n++)
                  {
                     if(jump_out_flag)
                     {
                      jump_out_flag =0;
                      return;
                     }
                    if(todoist_task[n].Exist)
                    {
                      if((todoist_task[n].Section == todoist_sections[m].ID)&& (todoist_task[n].Parent == "ul"))
                      {
#ifdef debug_msg
//                        Serial.print("todoist_task_num:");
//                        Serial.println(n);
//                        Serial.print("todoist_task_ID:");
//                        Serial.println(todoist_task[n].ID);
//                        Serial.print("todoist_task_Name:");
//                        Serial.println(todoist_task[n].Name);
#endif  
                        tft->drawCircle(36,26+(20*display_row_num),5,Str2RGB565(todoist_project[i].Color_Str));
                        todoist_task_select[display_task_num].x = 36;
                        todoist_task_select[display_task_num].y = 26+(20*display_row_num);
                        todoist_task_select[display_task_num].ID = todoist_task[n].ID;
                        todoist_task_select[display_task_num].Name = todoist_task[n].Name;
                        display_task_num++;
                        if(todoist_task[n].Finished)
                        {
                          tft->fillCircle(36,26+(20*display_row_num),3,Str2RGB565(todoist_project[i].Color_Str));  
                          tft->drawLine(48,26+(20*display_row_num),48+todoist_task[n].Name.length()*12,26+(20*display_row_num),Str2RGB565(todoist_project[i].Color_Str));
                        }
                        display_string_len = todoist_task[n].Name.length();
                        if(display_string_len > 36)
                        {
                          while(display_string_len > 36)
                          {
                            display_string = todoist_task[n].Name.substring(todoist_task[n].Name.length()- display_string_len, todoist_task[n].Name.length()-display_string_len+36);
                            display_string_len -= 36;
                            tft->setCursor(48, 20+(20*display_row_num));
                            tft->print(display_string);
                            display_row_num++;
                            if(display_row_num == 15)
                            {
                              display_row_num =0; 
                              wait_task_finish_click();display_dashboard();
                            }
                          }
                          display_string = todoist_task[n].Name.substring(todoist_task[n].Name.length()-display_string_len, todoist_task[n].Name.length());
                          tft->setCursor(48, 20+(20*display_row_num));  
                          tft->print(display_string);
                          display_row_num++;
                          if(display_row_num == 15)
                          {
                            display_row_num =0; 
                            wait_task_finish_click();display_dashboard();
                          }
                        }
                        else
                        {
                           tft->setCursor(48, 20+(20*display_row_num));
                           tft->print(todoist_task[n].Name);
                           display_row_num++;
                           if(display_row_num == 15)
                           {
                            display_row_num =0; 
                            wait_task_finish_click();display_dashboard();
                           }
                        }

                      for(uint8_t p=n; p<todoist_task_num; p++)
                      {
                         if(jump_out_flag)
                         {
                          jump_out_flag =0;
                          return;
                         }
                        if((todoist_task[p].Parent == todoist_task[n].ID))
                        {
#ifdef debug_msg
//                          Serial.print("todoist_task_num:");
//                          Serial.println(p);
//                          Serial.print("todoist_task_ID:");
//                          Serial.println(todoist_task[p].ID);
//                          Serial.print("todoist_task_Parent:");
//                          Serial.println(todoist_task[p].Parent);
//                          Serial.print("todoist_task.ID");
//                          Serial.print(n);
//                          Serial.print(":");
//                          Serial.println(todoist_task[n].ID);
#endif  
                          tft->drawCircle(60,26+(20*display_row_num),5,Str2RGB565(todoist_project[i].Color_Str));
                          todoist_task_select[display_task_num].x = 60;
                          todoist_task_select[display_task_num].y = 26+(20*display_row_num);
                          todoist_task_select[display_task_num].ID = todoist_task[p].ID;
                          todoist_task_select[display_task_num].Name = todoist_task[p].Name; 
                          display_task_num++;                      
                          if(todoist_task[p].Finished)
                          {
                            tft->fillCircle(60,26+(20*display_row_num),3,Str2RGB565(todoist_project[i].Color_Str)); 
                            tft->drawLine(72,26+(20*display_row_num),72+todoist_task[p].Name.length()*12,26+(20*display_row_num),Str2RGB565(todoist_project[i].Color_Str));                         
                          }
                          display_string_len = todoist_task[p].Name.length();
                          if(display_string_len > 34)
                          {
                            while(display_string_len > 34)
                            {
                              display_string = todoist_task[p].Name.substring(todoist_task[p].Name.length()- display_string_len, todoist_task[p].Name.length()-display_string_len+34);
                              display_string_len -= 34;
                              tft->setCursor(72, 20+(20*display_row_num));
                              tft->print(display_string);
                              display_row_num++;
                              if(display_row_num == 15)
                              {
                                display_row_num =0;   
                                wait_task_finish_click();display_dashboard();
                              }
                            }
                            display_string = todoist_task[p].Name.substring(todoist_task[p].Name.length()-display_string_len, todoist_task[p].Name.length());
                            tft->setCursor(72, 20+(20*display_row_num));  
                            tft->print(display_string);
                            display_row_num++;
                          }
                          else
                          {
                             tft->setCursor(72, 20+(20*display_row_num));
                             tft->print(todoist_task[p].Name);
                             display_row_num++;
                          }
                          if(display_row_num == 15)
                          {
                            display_row_num =0;        
                            wait_task_finish_click();display_dashboard();
                          }
                          for(uint8_t q=0; q<todoist_task_num; q++)
                          {
                             if(jump_out_flag)
                             {
                              jump_out_flag =0;
                              return;
                             }
                            if((todoist_task[q].Parent == todoist_task[p].ID))
                            {
#ifdef debug_msg
//                          Serial.print("todoist_task_num2:");
//                          Serial.println(q);
//                          Serial.print("todoist_task_ID2:");
//                          Serial.println(todoist_task[q].ID);
//                          Serial.print("todoist_task_Parent2:");
//                          Serial.println(todoist_task[q].Parent);
#endif  
                              tft->drawCircle(84,26+(20*display_row_num),5,Str2RGB565(todoist_project[i].Color_Str));
                              todoist_task_select[display_task_num].x = 84;
                              todoist_task_select[display_task_num].y = 26+(20*display_row_num);
                              todoist_task_select[display_task_num].ID = todoist_task[q].ID;
                              todoist_task_select[display_task_num].Name = todoist_task[q].Name;  
                              display_task_num++;                      
                              if(todoist_task[q].Finished)
                              {
                                tft->fillCircle(84,26+(20*display_row_num),3,Str2RGB565(todoist_project[i].Color_Str)); 
                                tft->drawLine(96,26+(20*display_row_num),96+todoist_task[q].Name.length()*12,26+(20*display_row_num),Str2RGB565(todoist_project[i].Color_Str));
                         
                              }
                              display_string_len = todoist_task[q].Name.length();
                              if(display_string_len > 32)
                              {
                                while(display_string_len > 32)
                                {
                                  display_string = todoist_task[q].Name.substring(todoist_task[q].Name.length()- display_string_len, todoist_task[q].Name.length()-display_string_len+32);
                                  display_string_len -= 32;
                                  tft->setCursor(96, 20+(20*display_row_num));
                                  tft->print(display_string);
                                  display_row_num++;
                                  if(display_row_num == 15)
                                  {
                                    display_row_num =0;        
                                    wait_task_finish_click();display_dashboard(); 
                                  }
                                }
                                display_string = todoist_task[q].Name.substring(todoist_task[q].Name.length()-display_string_len, todoist_task[q].Name.length());
                                tft->setCursor(96, 20+(20*display_row_num));  
                                tft->print(display_string);
                                display_row_num++;
                              }
                              else
                              {
                                 tft->setCursor(96, 20+(20*display_row_num));
                                 tft->print(todoist_task[q].Name);
                                 display_row_num++;
                              }
                              if(display_row_num == 15)
                              {
                                display_row_num =0;
                                wait_task_finish_click();display_dashboard();
                              }
                            }
                          }
                        }
                      }                      
                     }
                    }
                  }
                }
              }
            }
            tft->setTextColor(DARKGREY);
            for(uint8_t n=0; n<todoist_task_num;n++)
            {
               if(jump_out_flag)
               {
                jump_out_flag =0;
                return;
               }
              if(todoist_task[n].Parent == todoist_project[i].ID)    
              {
#ifdef debug_msg
                 Serial.print("todoist_task_num:");
                 Serial.println(n);
#endif
                tft->drawCircle(48,26+(20*display_row_num),5,Str2RGB565(todoist_project[i].Color_Str));
                todoist_task_select[display_task_num].x = 48;
                todoist_task_select[display_task_num].y = 26+(20*display_row_num);
                todoist_task_select[display_task_num].ID = todoist_task[n].ID;
                todoist_task_select[display_task_num].Name = todoist_task[n].Name;
                display_task_num++;
                display_string_len = todoist_task[n].Name.length();
                if(display_string_len > 34)
                {
                  while(display_string_len > 34)
                  {
                    display_string = todoist_task[n].Name.substring(todoist_task[n].Name.length()- display_string_len, todoist_task[n].Name.length()-display_string_len+34);
                    display_string_len -= 34;
                    tft->setCursor(24, 20+(20*display_row_num));
                    tft->print(display_string);
                    display_row_num++;
                    if(display_row_num == 15)
                    {
                      display_row_num =0;        
                      wait_task_finish_click();display_dashboard();
                    }
                  }
                  display_string = todoist_task[n].Name.substring(todoist_task[n].Name.length()-display_string_len, todoist_task[n].Name.length());
                  tft->setCursor(24, 20+(20*display_row_num));
                  tft->print(display_string);
                  display_row_num++;
                }
                else
                {
                  tft->setCursor(48, 20+(20*display_row_num));
                  tft->print(todoist_task[n].Name); 
                  display_row_num++;
                }
                if(display_row_num == 15)
                {
                   display_row_num =0;
                   wait_task_finish_click();
                   display_dashboard();          
                }
              }
            }
          }
          else
          {
            return;
          }        
        }
        wait_task_finish_click();
        if(display_time_cnt == 6)
        {
          display_time_cnt = 0;
          display_dashboard();
          tft->setTextColor(DARKGREEN);
          tft->setTextSize(2);
          tft->setCursor(132, 120);
          tft->print("Get Todolist Again");
          currentState = send_project_request;
        }
        else
        {
          display_time_cnt++;
        }
        //currentState = todolist_close_task;
     }
    break;
      
    case todolist_close_task:
     {
      if(!display_close_int)
      {
        //currentState = send_project_request;
        display_dashboard();
        tft->drawRect(50,70,385,150,BLACK);
        tft->setTextColor(BLACK);
        tft->setTextSize(2);
        tft->setCursor(60, 80);
        tft->print(String("Do you want to close this task:"));
        tft->setTextColor(DARKGREY);
        tft->setTextSize(2);
        tft->setCursor((480-(12*todoist_task_close.Name.length()))/2, 120);
        tft->print(todoist_task_close.Name);
        
        tft->fillRect(160,175,46,25,WHITE);
        tft->drawRect(160,175,46,25,RED);
        tft->setTextColor(RED);
        tft->setTextSize(2);
        tft->setCursor(165, 180);
        tft->print("YES");

        tft->fillRect(280,175,40,25,WHITE);
        tft->drawRect(280,175,40,25,GREEN);
        tft->setTextColor(GREEN);
        tft->setTextSize(2);
        tft->setCursor(288, 180);
        tft->print("NO");
        
        display_close_int = true;    
      }
      else
      {
        if(buttonState)
        {
          delay(150);
          if(buttonState)
          {
            yes_or_no =!yes_or_no;
            for(uint16_t time_cnt = 0; time_cnt <1000; time_cnt++)
            {
              if(!buttonState)
              {
                if(yes_or_no)
                {
                  tft->fillRect(160,175,46,25,WHITE);
                  tft->drawRect(160,175,46,25,RED);
                  tft->setTextColor(RED);
                  tft->setTextSize(2);
                  tft->setCursor(165, 180);
                  tft->print("YES");
        
                  tft->fillRect(280,175,40,25,GREEN);
                  tft->setTextColor(WHITE);
                  tft->setTextSize(2);
                  tft->setCursor(288, 180);
                  tft->print("NO");
                }
                else
                {
                  tft->fillRect(160,175,46,25,RED);
                  tft->setTextColor(WHITE);
                  tft->setTextSize(2);
                  tft->setCursor(165, 180);
                  tft->print("YES");
                  
                  tft->fillRect(280,175,40,25,WHITE);
                  tft->drawRect(280,175,40,25,GREEN);
                  tft->setTextColor(GREEN);
                  tft->setTextSize(2);
                  tft->setCursor(288, 180);
                  tft->print("NO");
                }                
                time_cnt = 1000;        
              }
              else
              {
                delay(1);                
              }
              if(time_cnt == 999)
              {
                display_dashboard();                
                if(yes_or_no)
                {
                  tft->setTextColor(RED);
                  tft->setTextSize(2);
                  tft->setCursor(180, 120);
                  tft->print("Task Closing");                  
                  yes_or_no = false;
                  if (client.connectSSL(todoist_server,443)) {
                  delay(3000);
                  // Make a HTTP request          
                  client.println(String("POST /rest/v2/tasks/")+todoist_task_close.ID+ String("/close HTTP/1.1"));
                  client.println(String("Host: ") + todoist_server);
                  client.println(String("Authorization: Bearer ") + String(todoist_token));          
                  client.println("Connection: close");
                  client.println();
                  json_String= "";
               
                  currentState = show_close_result;
                  }
                }
                else
                {                  
                  currentState = display_todolist;
                }
                display_close_int = false;
              }
            }
          }
        }        
      }
     }
    break;
    
    case show_close_result:
    {
       while (client.available()) {
        json_String += (char)client.read();
        data_now = 1; 
        dataEnd = 0;
      }          
      if(data_now)
      {
#ifdef debug_msg
         Serial.println(json_String);    
#endif
          client.stop();
          delay(2000);
         currentState = send_project_request;
      }
    }
    break;
  }
}

void onChange()
{
  if(digitalRead(buttonPin)== LOW)
  {
    buttonState = true;
    digitalWrite(ledPin, HIGH); 
  }
  else
  {
    buttonState = false;
    digitalWrite(ledPin, LOW); 
  }
}

uint16_t HexStr2Int(String HexStr)
{
  uint16_t ASCIIValue;
  for (int i = 0; i < HexStr.length(); i++) {
    int val = HexStr[i] > 0x39 ? (HexStr[i] - 'a' + 10) : (HexStr[i] - '0');
    ASCIIValue = ASCIIValue << 4;
    ASCIIValue += val; 
  }
  return ASCIIValue;
}

void wait_task_finish_click()
{
   uint8_t task_select = 0;
   
   for(uint16_t time_cnt; time_cnt <10000; time_cnt++)
   {
      if(buttonState)
      {
        time_cnt = 0;
        delay(150);
        if(buttonState)
        {        
           for(uint16_t time_cnt2 = 0; time_cnt2 <1000; time_cnt2++)
           {
              if(!buttonState)
              {
                 if(task_select > 0 && task_select < display_task_num)
                 {
                    tft->fillCircle(todoist_task_select[task_select-1].x,todoist_task_select[task_select-1].y,3,WHITE);
                    tft->fillCircle(todoist_task_select[task_select].x,todoist_task_select[task_select].y,3,BLACK);
                 }
                 else if(task_select < display_task_num)
                 {
                    tft->fillCircle(todoist_task_select[task_select].x,todoist_task_select[task_select].y,3,BLACK);
                 }
                 else if(task_select == display_task_num)
                 {
                    task_select = 0;
                    display_task_num=0;
                    return;
                 }
                task_select++;
                time_cnt2 = 1000;
              }
              else
              {
                delay(1);
              }
           }
           if(buttonState)
           {
...

This file has been truncated, please download it to see its full contents.

Credits

Gavin

Gavin

24 projects • 23 followers
Make it Funny.

Comments