/*
16 bit Shift Register Demo
*/
#include "Seg_LUT.h"
// pin definitions
const int CLOCK_PIN = 4;
const int LATCH_PIN = 3;
const int DATA_PIN = 2;
const int MAX_DIGITS = 8;
const byte NUM_CHARS = 9;
char receivedChars[NUM_CHARS]; // an array to store the received data
char fixedChars[NUM_CHARS];
boolean newData = false;
int msgLen = 0;
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= NUM_CHARS) {
ndx = NUM_CHARS - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
msgLen = ndx;
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
for (int i = 0; i < msgLen; i++) {
char upperCaseChar = toupper(receivedChars[i]) - 55;
fixedChars[i] = upperCaseChar;
}
//sevseg.setChars(receivedChars);
newData = false;
}
}
void updateDisplay(uint32_t value, uint8_t dpPos) {
uint8_t digitVal;
uint8_t powOfTen = MAX_DIGITS - 1;
uint32_t op1 = 1, op2 = 1;
for (uint8_t digit = 0; digit < powOfTen; digit++) {
for (uint8_t p = powOfTen - digit; p >= 1; p--) {
op1 = op1 * 10;
}
op2 = op1 * 10;
// with leading zero blanking (10 is a blank char)
digitVal = value < op1 ? 10 : (value % op2) / op1;
// no blanking (better for clocks)
//digitVal = (value % op2) / op1;
// fixed decimal point
writeShiftDigit(digit, digitVal, ((dpPos - 1) == digit) ? true : false);
// "clock" decimal points
//writeShiftDigit(digit, digitVal, (digit == 3 || digit == 5) ? true : false);
op1 = 1; op2 = 1;
}
// last digit is never blanked or "dotted"
writeShiftDigit(powOfTen, value % 10, false);
}
void writeShiftDigit(uint8_t digit, uint8_t charCode, bool dp) {
uint8_t value = ~digitCodeMap[charCode]; // invert (~) bits for common anode
digitalWrite(LATCH_PIN, LOW);
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, dp ? (value & 0x7f) : value); // OR with 0x80 for common cathode
shiftOut(DATA_PIN, CLOCK_PIN, MSBFIRST, (1 << digit)); // invert bits (~) for common cathode
digitalWrite(LATCH_PIN, HIGH);
}
void setup() {
Serial.begin(115200);
pinMode(DATA_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
pinMode(LATCH_PIN, OUTPUT);
}
void loop() {
//updateDisplay(12345678UL, 6);
recvWithEndMarker();
showNewData();
for (int i = 0; i <= NUM_CHARS; i++) {
writeShiftDigit(i, fixedChars[i], false);
//Serial.println(upperCaseChar);
}
/*
for (int digit = 0; digit < 8; digit++) {
for (int j = 0; j < 42; j++) {
//writeShiftValue(1 << i);
writeShiftDigit(0, 90 - 55, false);
delay(500);
}
delay(500);
}
*/
//writeShiftValue(0xAAAA);
//delay(1000);
//writeShiftValue(0x5555);
//delay(1000);
}