Skip to content

Instantly share code, notes, and snippets.

@new5558
Last active October 17, 2018 17:02
Show Gist options
  • Save new5558/704eccad11b6b4aa678e970f5604976a to your computer and use it in GitHub Desktop.
Save new5558/704eccad11b6b4aa678e970f5604976a to your computer and use it in GitHub Desktop.
lab8
// Another bug fixed
// final2
#include <M5Stack.h>
// for next 1 second timeout
uint32_t targetTime = 0;
uint32_t tempTime = 1000;
uint8_t hh = 0, mm = 0, ss = 1; // Get H, M, S from compile time
byte omm = 99, oss = 99;
byte xcolon = 0, xsecs = 0;
unsigned int colour = 0;
boolean isRunning = false;
boolean mustDraw = false;
boolean loopTerminator = false;
boolean isReseted = true;
void setup(void) {
//Serial.begin(115200);
M5.begin();
// M5.Lcd.setRotation(1);
M5.Lcd.fillScreen(TFT_WHITE);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(TFT_BLACK, TFT_WHITE);
drawTime();
}
void loop() {
// if(isReseted) {
// targetTime = millis() + 1000;
// }
if (M5.BtnA.wasPressed() && !isRunning) {
hh++;
if (hh == 100) hh = 0;
mustDraw = true;
drawTime();
mustDraw = false;
// drawTime();
}
if (M5.BtnB.wasPressed() && !isRunning) {
mm++;
if (mm == 60) {
hh++;
mm = 0;
}
drawTime();
}
if (M5.BtnC.pressedFor(5000) && !isRunning) {
hh = 0;
mm = 0;
drawTime();
isRunning = false;
loopTerminator = true;
isReseted = true;
tempTime = 1000;
}
if (M5.BtnC.wasReleased()) {
if (loopTerminator) loopTerminator = false;
else if (mm != 0 || hh != 0) {
if (isRunning) {
if (isReseted) tempTime = 1000;
else tempTime = 1000 - (millis() - (targetTime - 1000));
}
isRunning = !isRunning;
M5.Lcd.setTextColor(TFT_BLACK, TFT_WHITE);
int ypos = 60;
M5.Lcd.drawChar(':', xcolon + 3, ypos - 8, 8);
isReseted = false;
}
targetTime = millis();
drawTime();
}
if (!isRunning) {
if (M5.BtnA.pressedFor(5000)) {
hh = 0;
mustDraw = true;
drawTime();
mustDraw = false;
isReseted = true;
// tempTime = 1000; // No need to reset miliseconds counter for minutes reset
}
if (M5.BtnB.pressedFor(5000)) {
mm = 0;
drawTime();
isReseted = true;
tempTime = 1000;
}
}
if (isRunning) {
// Set next update for 1 second later
if (targetTime + tempTime < millis()) {
targetTime = millis() + 1000;
// Adjust the time values by adding 1 second
ss--; // Advance second
if (ss == 0) { // Check for roll-over
ss = 1; // Reset seconds to zero
omm = mm; // Save last minute time for display update
mm--; // Advance minute
if (hh == 0 && mm < 1) {
isRunning = false;
mm = 0;
drawTime();
M5.Speaker.tone(440);
while (true) {
boolean tempA = M5.BtnA.read(), tempB = M5.BtnB.read(), tempC = M5.BtnC.read();
if (M5.BtnA.read() != tempA || M5.BtnB.read() != tempB || M5.BtnC.read() != tempC ) {
M5.Speaker.mute();
isReseted = true;
break;
}
}
// M5.Speaker.mute();
}
if (mm == 255 && isRunning) { // Check for roll-over
mm = 59;
hh--; // Advance hour
if (hh < 0) { // Check for 24hr roll-over (could roll-over on 13)
hh = 59; // 0 for 24 hour clock, set to 1 for 12 hour clock
}
}
}
tempTime = 0;
}
drawTime();
if(isReseted) tempTime = 1000;
}
M5.update();
}
static void drawTime() {
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(TFT_BLACK, TFT_WHITE);
// Update digital time
int xpos = 35;
int ypos = 60; // Top left corner ot clock text, about half way down
int ysecs = ypos + 24;
if (omm != mm || mustDraw) { // Redraw hours and minutes time every minute
omm = mm;
// Draw hours and minutes
if (hh < 10) xpos += M5.Lcd.drawChar('0', xpos - 10, ypos, 8); // Add hours leading zero for 24 hr clock
xpos += M5.Lcd.drawNumber(hh, xpos - 10, ypos, 8); // Draw hours
xcolon = xpos; // Save colon coord for later to flash on/off later
xpos += M5.Lcd.drawChar(':', xpos + 3, ypos - 8, 8);
if (mm < 10) xpos += M5.Lcd.drawChar('0', xpos + 10, ypos, 8); // Add minutes leading zero
xpos += M5.Lcd.drawNumber(mm, xpos + 10, ypos, 8); // Draw minutes
xsecs = xpos + 10; // Sae seconds 'x' position for later display updates
}
if (isRunning) { // Redraw seconds time every second
// oss = ss;
// xpos = xsecs;
if (mm % 2) { // Flash the colons on/off
M5.Lcd.setTextColor(0xF79E, TFT_WHITE); // Set colour to grey to dim colon
M5.Lcd.drawChar(':', xcolon + 3, ypos - 8, 8); // Hour:minute colon
// xpos += M5.Lcd.drawChar(':', xsecs, ysecs, 6); // Seconds colon
M5.Lcd.setTextColor(TFT_BLACK, TFT_WHITE); // Set colour back to yellow
}
else {
M5.Lcd.drawChar(':', xcolon + 3, ypos - 8, 8); // Hour:minute colon
// xpos += M5.Lcd.drawChar(':', xsecs, ysecs, 6); // Seconds colon
}
}
// //Draw seconds
// if (ss < 10) xpos += M5.Lcd.drawChar('0', xpos, ysecs, 6); // Add leading zero
// M5.Lcd.drawNumber(ss, xpos, ysecs, 6); // Draw seconds
M5.Lcd.setTextSize(3);
M5.Lcd.setCursor(55, 200);
M5.Lcd.printf("M"); // draw M
M5.Lcd.setCursor(150, 200);
M5.Lcd.printf("S"); // draw S
M5.Lcd.setCursor(230, 200);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("START");
M5.Lcd.setCursor(230, 210);
M5.Lcd.printf("/STOP");// draw S// draw start/stop
}
@new5558
Copy link
Author

new5558 commented Oct 17, 2018

Bug1 …This code don’t execute at all: solved
line 77 : if(M5.BtnC.pressedFor(5000)) { hh = 0; mm = 0; drawTime(); isRunning = false; }

** fixing bug 1 also fix bug 3

Bug2…Should be the better way of excuting this code instead of read() (sometimes read() doesn’twork) : solved
line 100 : if(M5.BtnA.read() != tempA || M5.BtnB.read() != tempB || M5.BtnC.read() != tempC ) { M5.Speaker.mute(); break; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment