Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 66 additions & 4 deletions examples/serial_binboard/serial_binboard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. */

#include "LiBoard.h"
#include <string.h>

// !!! choose mode: 1 = global single value, 0 = per-square array !!!
#define USE_GLOBAL_THRESHOLD 0

#if USE_GLOBAL_THRESHOLD
// Option 1 - Global Threshold (Applies to all photoresistors)
const unsigned short THRESHOLD = 100;
unsigned short THRESHOLD = 100;

#else
// Option 2 - Per-square threshold (Applies to each individual photoresistor)
/*
unsigned short THRESHOLD[64] = {
// A1..H1
150, 150, 150, 150, 150, 150, 150, 150,
Expand All @@ -36,9 +41,11 @@ unsigned short THRESHOLD[64] = {
// A7..H7
300, 300, 300, 300, 300, 300, 300, 300,
// A8..H8
300, 300, 300, 300, 300, 300, 300, 300
300, 300, 300, 300, 300, 300, 300, 300,
};
*/
#endif

bool calibrating = false;

LiBoard board = LiBoard();
unsigned long long lastBinBoard = 0;
Expand All @@ -49,12 +56,67 @@ void writeBinaryBoard(unsigned long long binBoard) {
Serial.write((unsigned char)(binBoard>>(8*(7-i))));
}

// print a single CSV snapshot of raw ADC values (A1..H8 in LiBoard order)
void printRawSnapshotCSV() {
board.getData(); // fills board.values[0..63]
for (int i = 0; i < 64; ++i) {
Serial.print(board.values[i]);
if (i < 63) Serial.print(',');
}
Serial.println();
}

void setup() {
delay(3000);
Serial.begin(9600);
}

void loop() {
// Serial Monitor arguements
if (Serial.available()) {
int c = Serial.read();
// if host sends '?', reply with one CSV line of raw ADC values
if (c == '?') {
printRawSnapshotCSV();
}

// Calibration Mode for editing THRESHOLD
if (c == 'c') {
calibrating = true;
Serial.println("Calibration Mode Activated!");
}

while (calibrating) {
if (Serial.available()){
// read a whole line the user pasted
String line = Serial.readStringUntil('\n');
line.trim();
if (line.length() == 0) continue;

#if USE_GLOBAL_THRESHOLD
// Global Threshold Calibration
long newThreshold = line.toInt();
THRESHOLD = (unsigned short)newThreshold;
#else
// Per-Square Calibration
char buf[1024];
line.toCharArray(buf, 1024);
char* tok = strtok(buf, ",");

int i = 0;
while (tok && i < 64) {
THRESHOLD[i++] = (unsigned short)atoi(tok);
tok = strtok(nullptr, ",");
}
#endif
calibrating = false;
// eat non-numeric to avoid getting stuck
while (Serial.available()) Serial.read();
}
}
}

// Bitboard logic for normal use
currentBinBoard = board.getBinaryBoard(THRESHOLD);
if (currentBinBoard != lastBinBoard) {
writeBinaryBoard(currentBinBoard);
Expand Down