This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// include the necessary libraries | |
#include <Esplora.h> | |
#include <TFT.h> // Arduino LCD library | |
#include <SPI.h> | |
#define STR_SIZE 16 | |
char outputStr[STR_SIZE]; | |
char lapOutputStr[STR_SIZE]; | |
int timerSpeed = 1; // default value | |
const int TICK_MAX = -15625; // Ticks for one second | |
int tickCnt = TICK_MAX / timerSpeed; | |
int curSecond; | |
int curMin; | |
int curHour; | |
int lapIdx = 1; | |
enum STATE { | |
STOP, | |
RUNNING, | |
RESET | |
}; | |
STATE watchState = RESET; | |
void setupTimer() { | |
TCCR1A = 0x00; // Normal mode for Timer1 | |
TCCR1B |= _BV(CS12); // prescaler = CPU clock/1024 | |
TCCR1B &= ~_BV(CS11); | |
TCCR1B |= _BV(CS10); | |
TIMSK1 |= _BV(TOIE1); // enable timer overflow interrupt | |
TCNT1 = tickCnt; // Ticks for 1 second @16 MHz,prescale=1024 | |
} | |
void setupDisplay() { | |
// Put this line at the beginning of every sketch that uses the GLCD | |
EsploraTFT.begin(); | |
EsploraTFT.setTextSize(1); | |
clearScreen(); | |
} | |
void clearScreen() { | |
// clear the screen with a black background | |
EsploraTFT.background(0, 0, 0); | |
EsploraTFT.stroke(0, 255, 255); | |
EsploraTFT.text("Press Left To Start.", 0, 0); | |
} | |
void setup() { | |
setupTimer(); | |
setupDisplay(); | |
} | |
void readTimerSpeed() { | |
timerSpeed = Esplora.readSlider(); | |
timerSpeed = map(timerSpeed, 0, 1023, 1, 100); | |
tickCnt = TICK_MAX / timerSpeed; | |
} | |
void resetWatchTimer() { | |
curSecond = 0; | |
curMin = 0; | |
curHour = 0; | |
lapIdx = 1; | |
clearScreen(); | |
} | |
void checkSwitchState() { | |
int startBtnState = Esplora.readButton(SWITCH_LEFT); | |
int stopBtnState = Esplora.readButton(SWITCH_DOWN); | |
int resetBtnState = Esplora.readButton(SWITCH_RIGHT); | |
if( resetBtnState == LOW ) { | |
watchState = RESET; | |
resetWatchTimer(); | |
} else { | |
if( startBtnState == LOW ) { | |
watchState = RUNNING; | |
displayWatchStateMessage(); | |
} else if( stopBtnState == LOW ) { | |
watchState = STOP; | |
displayWatchStateMessage(); | |
} | |
} | |
} | |
void recordWatch() { | |
int recordBtnState = Esplora.readButton(SWITCH_UP); | |
if( recordBtnState == LOW ) { | |
updateDisplay(true); | |
} | |
} | |
void updateDisplay(bool isRecord) { | |
String str = String(curHour); | |
str += ":"; | |
str += curMin; | |
str += ":"; | |
str += curSecond; | |
if( !isRecord ) { | |
// convert the string to a char array | |
str.toCharArray(outputStr, STR_SIZE); | |
// set the text color to white | |
EsploraTFT.stroke(255, 255, 255); | |
EsploraTFT.text(outputStr, 0, 10); | |
// erase the text for the next loop | |
EsploraTFT.stroke(0, 0, 0); | |
EsploraTFT.text(outputStr, 0, 10); | |
} else { | |
str = " " + str; | |
str = lapIdx + str; | |
str = "Lap " + str; | |
EsploraTFT.fill(0, 0, 0); | |
EsploraTFT.rect(0, lapIdx * 10 + 10, 200, 9); | |
EsploraTFT.noFill(); | |
str.toCharArray(lapOutputStr, STR_SIZE); | |
EsploraTFT.stroke(255, 255, 255); | |
EsploraTFT.text(lapOutputStr, 0, lapIdx * 10 + 10); | |
lapIdx += 1; | |
if( lapIdx > 10 ) { | |
lapIdx = 1; | |
} | |
} | |
} | |
void displayWatchStateMessage() { | |
EsploraTFT.fill(0, 0, 0); | |
EsploraTFT.rect(0, 0, 200, 10); | |
EsploraTFT.stroke(0, 255, 255); | |
if( watchState == STOP ) | |
EsploraTFT.text("Press Left To Resume.", 0, 0); | |
else if( watchState == RUNNING ) | |
EsploraTFT.text("Press Down To Pause.", 0, 0); | |
} | |
void loop() { | |
checkSwitchState(); | |
if( watchState == RUNNING ) { | |
readTimerSpeed(); | |
updateDisplay(false); | |
recordWatch(); | |
} | |
} | |
// Timer interrupt service routine | |
ISR (TIMER1_OVF_vect) | |
{ | |
TCNT1 = tickCnt; // Ticks for 1 second @16 MHz,prescale=1024 | |
if( watchState == RUNNING ) { | |
curSecond = curSecond + 1; | |
if( curSecond >= 60 ) { | |
curSecond = 0; | |
curMin += 1; | |
if( curMin >= 60 ) { | |
curMin = 0; | |
curHour += 1; | |
} | |
} | |
} | |
} |
沒有留言:
張貼留言