传统时钟

在官方例程中看到了这个绘制时钟的例程,实在是巧妙,在这篇随笔中总结一下:

1.初始时间从编译时间__TIME__获得,详细资料参考 C语言获取编译时间

2.通过millis()函数计时,参考 millis()函数作用;大概思路就是初始有一个targetTime比millis()返回值大1000(即大1秒),在loop中如果targetTime<millis()返回值,说明过去了一秒,做相应的操作(其中就包含将targetTime加1000),这样有个好处是不用占用CPU跑空循环,可以做别的事情,但是millis()的返回值在一段时间后会溢出;更精确的计时可以使用RTClib

3.绘制指针时充分使用正余弦,秒针的移动通过不断地用背景色覆盖旧指针,绘制新指针实现,分针和时针类似,不过是在秒数等于0时做相应操作

#include <M5Stack.h>

#define TFT_GREY 0x5AEB
//uint16_t 就是无符号的2字节单元
float sx = 0, sy = 1, mx = 1, my = 0, hx = -1, hy = 0;    // Saved H, M, S x & y multipliers
float sdeg=0, mdeg=0, hdeg=0;
uint16_t osx=120, osy=120, omx=120, omy=120, ohx=120, ohy=120;  // Saved H, M, S x & y coords
uint16_t x0=0, x1=0, yy0=0, yy1=0;
uint32_t targetTime = 0;                    // for next 1 second timeout

static uint8_t conv2d(const char* p); // Forward declaration needed for IDE 1.6.x
uint8_t hh=conv2d(__TIME__), mm=conv2d(__TIME__+3), ss=conv2d(__TIME__+6);  //通过编译结束获得时间Get H, M, S from compile time

boolean initial = 1;

void setup(void) {
  M5.begin();
  M5.Power.begin();
  // M5.Lcd.setRotation(0);
  
  //M5.Lcd.fillScreen(TFT_BLACK);
  //M5.Lcd.fillScreen(TFT_RED);
  //M5.Lcd.fillScreen(TFT_GREEN);
  //M5.Lcd.fillScreen(TFT_BLUE);
  //M5.Lcd.fillScreen(TFT_BLACK);
  M5.Lcd.fillScreen(TFT_GREY);
  
  M5.Lcd.setTextColor(TFT_WHITE, TFT_GREY);  // Adding a background colour erases previous text automatically
  
  // 表盘
  M5.Lcd.fillCircle(160, 120, 118, TFT_GREEN);
  M5.Lcd.fillCircle(160, 120, 110, TFT_BLACK);

  // Draw 12 lines
  for(int i = 0; i<360; i+= 30) {
    sx = cos((i-90)*0.0174532925);
    sy = sin((i-90)*0.0174532925);
    x0 = sx*114+160;
    yy0 = sy*114+120;
    x1 = sx*100+160;
    yy1 = sy*100+120;

    M5.Lcd.drawLine(x0, yy0, x1, yy1, TFT_GREEN);
  }

  // 画60个点
  for(int i = 0; i<360; i+= 6) {
    sx = cos((i-90)*0.0174532925);
    sy = sin((i-90)*0.0174532925);
    x0 = sx*102+160;
    yy0 = sy*102+120;
    // Draw minute markers
    M5.Lcd.drawPixel(x0, yy0, TFT_WHITE);
    
    // Draw main quadrant dots
    if(i==0 || i==180) M5.Lcd.fillCircle(x0, yy0, 2, TFT_WHITE);
    if(i==90 || i==270) M5.Lcd.fillCircle(x0, yy0, 2, TFT_WHITE);
  }

  M5.Lcd.fillCircle(160, 121, 3, TFT_WHITE);

  // Only font numbers 2,4,6,7 are valid. Font 6 only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : . - a p m
  // Font 7 is a 7 segment font and only contains characters [space] 0 1 2 3 4 5 6 7 8 9 : .

  targetTime = millis() + 1000; //millis()返回上电启动后的时间
}

void loop() {
  if (targetTime < millis()) {
    targetTime += 1000;
    ss++;              // Advance second
    if (ss==60) {
      ss=0;
      mm++;            // Advance minute
      if(mm>59) {
        mm=0;
        hh++;          // Advance hour
        if (hh>23) {
          hh=0;
        }
      }
    }

    // Pre-compute hand degrees, x & y coords for a fast screen update
    sdeg = ss*6;                  // 0-59 -> 0-354
    mdeg = mm*6+sdeg*0.01666667;  // 0-59 -> 0-360 - includes seconds
    hdeg = hh*30+mdeg*0.0833333;  // 0-11 -> 0-360 - includes minutes and seconds
    hx = cos((hdeg-90)*0.0174532925);    
    hy = sin((hdeg-90)*0.0174532925);
    mx = cos((mdeg-90)*0.0174532925);    
    my = sin((mdeg-90)*0.0174532925);
    sx = cos((sdeg-90)*0.0174532925);    
    sy = sin((sdeg-90)*0.0174532925);

    if (ss==0 || initial) {
      initial = 0;
      // Erase hour and minute hand positions every minute
      //擦除表上的旧时针和分针
      M5.Lcd.drawLine(ohx, ohy, 160, 121, TFT_BLACK);
      ohx = hx*62+161;    
      ohy = hy*62+121;
      M5.Lcd.drawLine(omx, omy, 160, 121, TFT_BLACK);
      omx = mx*84+160;    
      omy = my*84+121;
    }

      // Redraw new hand positions, hour and minute hands not erased here to avoid flicker
      M5.Lcd.drawLine(osx, osy, 160, 121, TFT_BLACK);//擦除旧秒针
      osx = sx*90+161;    
      osy = sy*90+121;
      M5.Lcd.drawLine(osx, osy, 160, 121, TFT_RED);
      M5.Lcd.drawLine(ohx, ohy, 160, 121, TFT_WHITE);
      M5.Lcd.drawLine(omx, omy, 160, 121, TFT_WHITE);
      M5.Lcd.drawLine(osx, osy, 160, 121, TFT_RED);//画了分针可能把秒针给盖住了

    M5.Lcd.fillCircle(160, 121, 3, TFT_RED);
  }
}

static uint8_t conv2d(const char* p) {//将字符型的10-99转为无符号数
  uint8_t v = 0;
  if ('0' <= *p && *p <= '9')
    v = *p - '0';
  return 10 * v + *++p - '0';
}

 

上一篇:No.28-VulnHub-zico2: 1-Walkthrough渗透学习


下一篇:NO.1——VulnHub-GoldenEye-1-Walkthrough