Skip to content

Commit

Permalink
Remove delay() and replace with timer for character generati
Browse files Browse the repository at this point in the history
on interval
  • Loading branch information
chrisb2 committed Jan 23, 2019
1 parent bb1df69 commit f815859
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ There are three adjustable parameters available via the buttons:
- Characters - the number of characters in each line (5, 13, 26, 52, 104). The default is 26 (a-z).
- Rate - the rate of character generation in characters/minute (100, 200, 400, 1000, 6000). The default is 200 (average typist). 100 is a slow typist, 400 is a professional typist and anything higher is not humanly possible!

Character generation is started and stopped with the toggle switch.
These parameters may be adjusted while characters are being generated. Character generation is started and stopped with the toggle switch.

![Picture](./docs/key-typer.jpg)

Expand Down
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ lib_deps =
[email protected]
[email protected]
[email protected]
[email protected]
72 changes: 47 additions & 25 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Arduino.h>
#include <Keyboard.h>
#include <spi.h>
#include <Ticker.h>
#include <TFT.h>
#include <Bounce2.h>

Expand Down Expand Up @@ -30,6 +31,8 @@ const int LINES_Y_OFFSET = 35;
const int LINE_LEN_Y_OFFSET = 60;
const int RATE_Y_OFFSET = 85;

const long MILLIS_IN_MINUTE = 60000;

char character = A;

int charCount = 0;
Expand All @@ -45,11 +48,19 @@ Bounce linesButton = Bounce();
Bounce lineLenButton = Bounce();
Bounce rateButton = Bounce();

void generateCharacter();
int getInterval();
Ticker characterTimer(generateCharacter, getInterval());

void displayNumber(int number, int x, int y) {
char cstr[5];
screen.text(itoa(number, cstr, 10), x, y);
}

int getInterval() {
return MILLIS_IN_MINUTE / RATE_VALUES[rateIndex];
}

void clearNumber(int number, int x, int y) {
screen.stroke(ST7735_WHITE);
displayNumber(number, x, y);
Expand Down Expand Up @@ -120,6 +131,40 @@ void handleRateButton() {
rateIndex = 0;
}
setRateValue(RATE_VALUES[rateIndex]);
characterTimer.interval(getInterval());
}
}

void handleStartButton() {
startButton.update();
if (startButton.read() == LOW) {
if (characterTimer.state() == STOPPED) {
characterTimer.start();
}
} else {
if (characterTimer.state() == RUNNING) {
characterTimer.stop();
lineCount = 0;
}
}
}

void generateCharacter() {
if (LINES_VALUES[linesIndex] == -1 || lineCount < LINES_VALUES[linesIndex]) {
if (charCount >= LINE_LEN_VALUES[lineLenIndex]) {
// Start a new line and start again at letter a
Keyboard.println();
character = A;
charCount = 0;
lineCount++;
} else {
// Write a single character
Keyboard.write(character++);
charCount++;
if (character > Z) {
character = A;
}
}
}
}

Expand Down Expand Up @@ -148,31 +193,8 @@ void setup() {
}

void loop() {
startButton.update();

if (startButton.read() == LOW) {
if (LINES_VALUES[linesIndex] == -1 || lineCount < LINES_VALUES[linesIndex]) {
if (charCount == LINE_LEN_VALUES[lineLenIndex]) {
// Start a new line and start again at letter a
Keyboard.println();
character = A;
charCount = 0;
lineCount++;
} else {
// Write a single character
Keyboard.write(character++);
charCount++;
if (character > Z) {
character = A;
}
}
}

delay(60000 / RATE_VALUES[rateIndex]);
} else {
lineCount = 0;
}

characterTimer.update();
handleStartButton();
handleLinesButton();
handleLineLenButton();
handleRateButton();
Expand Down

0 comments on commit f815859

Please sign in to comment.