見出し画像

M5Stack Core2 シンプルな時計

パソコンからシリアル通信で、時刻を送ってもらって、内臓RTCに時間をセットします。

main.c

#include <M5Core2.h>
#include "EEPROM.h"
#include "mySerialRXTask.hpp"
//Buttonクラスの練習帳
//Page展開

#define LCDWIDE 320
#define LCDHEIGHT 240

TFT_eSprite canvas = TFT_eSprite(&M5.Lcd);

RTC_DateTypeDef RTC_Date; // Data
RTC_DateTypeDef StartDate;
RTC_DateTypeDef EndDate;
RTC_TimeTypeDef RTC_Time; // Time
RTC_TimeTypeDef StartTime;
RTC_TimeTypeDef EndTime;
RTC_DateTypeDef ElapseDate;
RTC_TimeTypeDef ElapseTime;

void Page0Paint(void);

void setup()
{
   M5.begin();
   canvas.createSprite(320, 240);  //Create a 320x240 canvas.
   canvas.setColorDepth(8);        // Set color depth.     
   //Task for Serial receiving
   rxTask.start();
   Page0Paint();
}


void loop()
{
   
   M5.update();
   
   if(rxTask.Completed)
   {
      Serial.printf("%d echo->%s",rxTask.length,rxTask.Buffer);
      if(rxTask.Buffer[0]=='t' && rxTask.length==15)
      {
         RTC_Date.Year = String(&rxTask.Buffer[1],(unsigned int)2).toInt()+2000;
         RTC_Date.Month = String(&rxTask.Buffer[3],(unsigned int)2).toInt();
         RTC_Date.Date = String(&rxTask.Buffer[5],(unsigned int)2).toInt();
         RTC_Date.WeekDay = 1;//Sun:0
         RTC_Time.Hours = String(&rxTask.Buffer[7],(unsigned int)2).toInt();
         RTC_Time.Minutes = String(&rxTask.Buffer[9],(unsigned int)2).toInt();
         RTC_Time.Seconds = String(&rxTask.Buffer[11],(unsigned int)2).toInt();
         if(!M5.Rtc.SetDate(&RTC_Date)) Serial.println("Wrong Date set");
         if(!M5.Rtc.SetTime(&RTC_Time)) Serial.println("Wrong time set");
      }

      rxTask.length=0;
      rxTask.Completed=false;
   }
   Page0Paint();
   delay(100);
   
}

void Page0Paint(void)
{
   const char *wd[7] = {"Sun","Mon","Tue","Wed","Thr","Fri","Sat"};
   const char position=10;
   M5.Rtc.GetDate(&RTC_Date);
   M5.Rtc.GetTime(&RTC_Time);

   canvas.fillScreen(WHITE);
   //display Current Date
   canvas.fillRect(0,50,320,50,BLUE);
   //canvas.fillRect(1,51,318,48,BLUE);
   canvas.setTextColor(WHITE);
   canvas.setCursor(5,90);
   canvas.setFreeFont(&FreeSans18pt7b);
   canvas.setTextSize(1);
   canvas.printf("%04d/%02d/%02d<%s>", RTC_Date.Year, RTC_Date.Month, RTC_Date.Date,wd[RTC_Date.WeekDay]);
   //display current time
   canvas.fillRect(0,110,320,50,BLUE);
   //canvas.fillRect(1,111,318,48,BLUE);
   canvas.setCursor(5,125); 
   canvas.setTextColor(WHITE);
   canvas.setFreeFont(&FreeSans9pt7b);
   canvas.printf("Current time");
   canvas.setCursor(120,150);
   canvas.setFreeFont(&FreeSans24pt7b);
   canvas.printf("%02d:%02d:%02d", RTC_Time.Hours, RTC_Time.Minutes, RTC_Time.Seconds);
   
   canvas.pushSprite(0,0);
}

mySerialRXTask.h

#ifndef __MYSERIALTASK_HPP_
#define __MYSERIALTASK_HPP_

#include <M5Core2.h>


extern TaskHandle_t taskHandle[2];
extern void mTask1(void *pvParameters);

class mySerialRXTask
{
    public:
        uint8_t  Buffer[100];
        uint8_t  length;
        bool     Completed;
    public:
        void start();
};

extern mySerialRXTask rxTask;

#endif

mySerialRXTask.c


#include <M5Core2.h>
#include "mySerialRXTask.hpp"

TaskHandle_t taskHandle[2];
mySerialRXTask rxTask;

void mySerialRXTask::start()
{
  BaseType_t ret =xTaskCreateUniversal(mTask1, "mTask1", 2000, NULL, 7, &taskHandle[0], APP_CPU_NUM);
  Serial.printf("task start Core1:%d\r",ret);
}

//mTask1()----------------------------------------------------------------------
void mTask1(void *pvParameters){
  uint8_t ch;
  while(1)
  {
    if(Serial.available()>0){
      ch=Serial.read();
      Serial.printf("%c",ch);
      rxTask.Buffer[rxTask.length]=ch;
      rxTask.length++;
      if(rxTask.length>100){
        rxTask.length=0;
      }
      //Serial.printf("%s ",strBuf);
      if(ch == '\n'){
        //Serial.printf("\n");
        ch=' ';
        rxTask.Buffer[rxTask.length]='\0';
        rxTask.Completed=true;
      }
    }  
    vTaskDelay(5);
  } 
}

いいなと思ったら応援しよう!