用M5Stack做一个简易秒表
本项目是一个简易秒表程序,程序中设定时间为3分钟,按A键开始计时,再次按A键暂停。#include <M5Stack.h>
#define TFT_GREY 0x5AEB
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
uint8_t hh = conv2d(__TIME__);
uint8_t mm = 3, ss = 0;
byte omm = 99, oss = 99;
byte xcolon = 0, xsecs = 0;
unsigned int colour = 0;
boolean isMeasuring = false;
int xpos = 0;
int ypos = 85; // Top left corner ot clock text, about half way down
int ysecs = ypos;
void setup(void) {
//Serial.begin(115200);
M5.begin();
// M5.Lcd.setRotation(1);
M5.Lcd.fillScreen(TFT_BLACK);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(TFT_YELLOW, TFT_BLACK);
xpos += M5.Lcd.drawChar('0', xpos, ypos, 8); // Add minutes leading zero
xpos += M5.Lcd.drawNumber(mm, xpos, ypos, 8); // Draw minutes
xsecs = xpos; // Sae seconds 'x' position for later display updates
xpos = xsecs;
M5.Lcd.setTextColor(TFT_YELLOW, TFT_BLACK); // Set colour back to yellow
xpos += M5.Lcd.drawChar(':', xsecs, ysecs, 8); // Seconds colon
//Draw seconds
xpos += M5.Lcd.drawChar('0', xpos, ysecs, 8); // Add leading zero
M5.Lcd.drawNumber(ss, xpos, ysecs, 8); // Draw seconds
}
void loop() {
M5.update();
if (M5.BtnA.wasReleased()) {
isMeasuring =! isMeasuring;
delay(5);
}
if (!isMeasuring && M5.BtnB.wasReleased()) {
mm = 3;
ss = 0;
delay(5);
}
if (targetTime < millis() && isMeasuring ) {
// Set next update for 1 second later
targetTime = millis() + 1000;
// Adjust the time values by adding 1 second
if( ss == 0 && mm == 0){
isMeasuring = false; //stop
}else{
ss = ss -1 ; // Advance second
if (ss == 255) { // Check for roll-over
ss = 59; // Reset seconds to zero
mm = mm -1; // Advance minute
if (mm == 255) { // Check for roll-over
mm = 0;
}
}
}
// Update digital time
xpos = 0;
ypos = 85; // Top left corner ot clock text, about half way down
ysecs = ypos;
if (omm != mm) { // Redraw hours and minutes time every minute
omm = mm;
// Draw hours and minutes
if (mm < 10) xpos += M5.Lcd.drawChar('0', xpos, ypos, 8); // Add minutes leading zero
xpos += M5.Lcd.drawNumber(mm, xpos, ypos, 8); // Draw minutes
xsecs = xpos; // Sae seconds 'x' position for later display updates
}
if (oss != ss) { // Redraw seconds time every second
oss = ss;
xpos = xsecs;
if (ss % 2) { // Flash the colons on/off
M5.Lcd.setTextColor(0x39C4, TFT_BLACK); // Set colour to grey to dim colon
xpos += M5.Lcd.drawChar(':', xsecs, ysecs, 8); // Seconds colon
M5.Lcd.setTextColor(TFT_YELLOW, TFT_BLACK); // Set colour back to yellow
}
else {
xpos += M5.Lcd.drawChar(':', xsecs, ysecs, 8); // Seconds colon
}
//Draw seconds
if (ss < 10) xpos += M5.Lcd.drawChar('0', xpos, ysecs, 8); // Add leading zero
M5.Lcd.drawNumber(ss, xpos, ysecs, 8); // Draw seconds
}
}
}
// Function to extract numbers from compile time string
static uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9')
v = *p - '0';
return 10 * v + *++p - '0';
}
页:
[1]