DDU_Exammen/ModuleMorse/MorsePart.h

94 lines
2.2 KiB
C

#include <M5StickCPlus2.h>
#define nextItemDelayTime 500
#define dotDisplayTime 500 + nextItemDelayTime
#define dashDisplayTime 1500 + nextItemDelayTime
#define nextLetterDelayTime 1000
#define restartDelayTime 5000
const String morseCodes[11] = {
"-.../---/-./-..;", // "bond"
".../.--/.-/.--.;", // "swap"
"-.-./.-/.-../.-..;", // "call"
".-././.--./---;", // "repo"
"-.--/./../.-../-..;", // "yield"
"../-./-.././-..-;", // "index"
".-/.../..././-;", // "asset"
"-..././.-/.-.;", // "bear"
"-.../..-/.-../.-..;", // "bull"
"--./.-/../-.;", // "gain"
".-../---/.../...;" // "loss"
};
int x, y, w, h;
int morseDisplayIndex = -1; // Which char iscurrently going to be displayed
int morseCodeLength;
int morseCorrectIndex;
void morseInit(int CorrectIndex, int X, int Y, int W, int H)
{
x = X;
y = Y;
w = W;
h = H;
morseCorrectIndex = CorrectIndex;
morseCodeLength = strlen(morseCodes[CorrectIndex].c_str());
}
bool isLit = false;
long getDisplayTime(char morseChar)
{
switch (morseChar)
{
case '.':
isLit = true;
Serial.println("dot");
return dotDisplayTime;
case '-':
isLit = true;
Serial.println("dash");
return dashDisplayTime;
case '/':
isLit = false;
Serial.println("space");
return nextLetterDelayTime;
case ';':
isLit = false;
Serial.println("stop");
return restartDelayTime;
default:
return 0;
}
}
long actionStartTime = 0;
long actionDurationTime = 0;
void morseUpdate()
{
long currentTime = millis();
if (actionStartTime + actionDurationTime < currentTime)
{
morseDisplayIndex++;
actionStartTime = currentTime;
// get next display time
char nextChar = morseCodes[morseCorrectIndex][morseDisplayIndex % morseCodeLength];
actionDurationTime = getDisplayTime(nextChar);
}
if (isLit && actionStartTime + actionDurationTime - nextItemDelayTime < currentTime)
{
isLit = false;
}
if (isLit)
M5.Lcd.fillRect(x, y, w, h, WHITE);
else
M5.Lcd.fillRect(x, y, w, h, BLACK);
}