/*
piOLED-KP1 plus a lithium battery check
one off old keypad
*/
# include <stdio.h>
# include "wtf.h"
/* keypad stuff */
# define ROW0 6
# define ROW1 7
# define ROW2 8
# define ROW3 9
# define COLM0 3
# define COLM1 4
# define COLM2 5
# define BMASK 7
# define KEYUP 13
# define KEYDOWN 14
# define KEYSTILL 15
# define TSTILL 33 /* ticks until 'still' event */
int evtTimer;
unsigned char theKey, theEvent;
# define BLACK 0x0000
# define BLUE 0x001F
# define RED 0xF800
# define GREEN 0x07E0
# define CYAN 0x07FF
# define MAGENTA 0xF81F
# define YELLOW 0xFFE0
# define WHITE 0xFFFF
extern int tsEvent, tsChar;
unsigned char oledDirt;
/* untnagle this! */
mInt piIdx, runLength;
mInt tempIdx;
mChar nextDigit;
void advance(void);
mChar keyState;
# define EAT 12
# define SPEED0 4 /* clock ticks per anim frame */
# define SPEED1 43 /* hangs in last place before being fed */
# define BLAMK 0xd /* is in font I hope */
# define KTEN 10 /* number of slots */
# define TOPSLOT 9 /* good grief */
# define KSEVEN 8 /* past to dredge in errors */
int incomingByte;
int rtFlag; /* set by clock mechanism */
unsigned char mRunning;
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void printf_begin(void)
{
fdevopen( &serial_putc, 0 );
}
unsigned char jPre;
void setup() {
int wtf;
Serial.begin(9600); jPre = 0;
printf_begin();
printf("\n\rPi Device Primitice Hackdown\n\r");
// testPtr = test;
// just testing here the basics
rtFlag = 0;
mRunning = 1;
setupRTOS();
setupKeypad();
initLED();
initRail();
oledDirt = 1;
pinMode(A6, INPUT);
/* LOW LIPO VOLTAGE WARNING/FORK */
/* A6 measure vIn to boost regulator */
wtf = analogRead(A6);
printf("analog 6 %d\n", wtf);
/*
if (wtf < 710) {
errorStop(901);
printf("error stop.\n"); // if errorStop stops...
}
*/
xmain(0xf7); /* ? */
}
// unsigned char nc; /* number to retread */
// the loop routine runs over and over again forever:
void loop()
{
int tColor;
unsigned char ii, jj;
loopRTOS(); /* does loopTS which could theEvent and ... */
// delay(300);
if (Serial.available() > 0) {
ii = Serial.read();
//... serial input fakes touch screen event, as did (?) checkkey in that module
if ('0' <= ii && ii <= '9' || ii == '*' || ii == '#') {
tsEvent = 1;
tsChar = ii;
Serial.print("char >"); Serial.print((char) tsChar); Serial.println("<");
}
}
/*
if (theEvent == KEYDOWN) { // this looks like where the pressed keypad gets seen...
Serial.println("event of KEYDOWN");
theEvent = 0; tsEvent = 1; tsChar = theKey;
}
*/
if (tsEvent) {
tsEvent = 0; /* handling it right here and now Mom! */
incomingByte = tsChar;
//... let the machine handle the '*' TEST
kmain(incomingByte);
}
}
void initRail()
{
Serial.println("init rail");
}
void kmain(mChar theChar)
{
unsigned char ii, jj, ie;
if ((theChar >= '0') && (theChar <= '9')) theChar -= '0';
else if (theChar == '#') theChar = HASHMARK;
else if (theChar == '*') theChar = ASTERISK;
else {
//...
Serial.println(" undisposed character");
bzzzt = 0xff;
return;
}
//.. short out the rail
theKey = theChar; //... argh!
// Serial.print("short feeding "); Serial.println((int) theKey);
//...
keyFSM("short feed");
return;
}
/* this is supplied in one version each large and small */
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include "wtf.h"
mChar red, green, yellow, bzzzt;
mChar hideChar;
void myOut(mChar theChar, int color)
{
// smart10(theChar, color);
printf(" %d] ", theChar);
}
/*
just a pi machine
*/
# include "pi1000.h"
mChar piAt(mInt);
mInt find(mChar, mInt);
mInt findStr(mInt, mChar, mInt);
/*
# define CT 0x20 add to 0..31 for seven sgment char to DS0
# define LT 0x40 add to 0..31 for literal LEDs DS1
*/
# define PI 17
# define EQ 16
# define ABAR 18
# define GBAR 19
/*
# define DBAR 20
# define BLANK 22 just the DP
*/
# define E 14
# define ASTERISK 0xb /* 11th and 12th keypad keys */
# define HASHMARK 0xc
void myOut(mChar, int color);
/*
mInt piIdx, runLength;
mChar nextDigit;
void advance(void);
*/
void reset(void);
void doIt(void);
unsigned char errState, errFlag;
void errFSM(void); /* records feats and faux pas */
# define IDLE 0
# define NOERR 1
# define ERR1 2 /* one misstep, can he fix it ? */
# define ERRN 3 /* ooops! you really are wrong */
//...
// void key FSM(void);
//sorry Mom! mChar keyState;
# define SHOW 11
//sorry Mom! # define EAT 12
# define ERROR 13
# define FINDI 14
# define FERROR 15
# define IDLE 0
# define START 1
# define FIND 2
mChar nLong;
mInt aPtr, bPtr, xPtr;
void xmain(mChar theChar)
{
if (theChar == 0xf7) { /* '*' still */
reset();
return;
}
if (theChar == 0xf6) { /* '#' still */
keyState = EAT;
return;
}
if ((theChar >= '0') && (theChar <= '9')) theChar -= '0';
else if (theChar == '#') theChar = HASHMARK;
else if (theChar == '*') theChar = ASTERISK;
else {
/* printf(" undisposed "); */
errState = ERROR;
bzzzt = 0xff;
return;
}
theKey = theChar;
//...
keyFSM("from xmain");
}
/* machine runs on diet of #s with errFlag for erroneous entries */
/*?? count = 0;**/
/*?? errState = NOERR; **//* and log a digit */
void errFSM()
{
static mInt count = 0;
unsigned char temp;
switch (errState) {
case IDLE :
if (errFlag)
break;
errState = NOERR;
count = 1;
/* dOut = 'T'; send_ata(); grab start time
*/
//grabStart();
break;
case NOERR :
count++; /* internal run length independent/redundant */
if (errFlag) {
errState = ERR1;
//writeEvent(count + 4096, piIdx + 8192);
/* dOut = 'E'; send_ata(); record a run length
dOut = '1'; send_ata(); count up [an] error at this digit
*/
//eRead(6000 + piIdx, &temp);
//eWrite(6000 + piIdx, temp + 1);
}
break;
case ERR1 :
if (errFlag) {
errState = ERRN;
/* dOut = 'N'; send_ata(); count up [multiple] error[s] at this digit */
//eRead(7000 + piIdx, &temp);
//eWrite(7000 + piIdx, temp + 1);
break;
}
else errState = IDLE; /* and IDLE */
case ERRN :
if (!errFlag)
errState = IDLE; /* GACK! misses counting the correcting digit */
break;
}
}
/* reset - add reset errFSM concept */
void reset()
{
piIdx = 0;
advance();
runLength = 0;
errState = IDLE;
errFlag = 0;
// bzzzt = 23;
red = 23; yellow = 33; green = 23;
keyState = EAT;
theEvent = NOEVENT;
theKey = '?'; /* very funny */
}
void keyFSM(char *msg)
{
printf("keyFSM %s\n", msg);
yellow = 3; /* key ACK */
/* first do '*' and '#' by state */
/* '*' reset one error if error, else do that reveal thing OR ignore*/
// busyIt(1); /* i be busy */
if (theKey == ASTERISK) {
/* hacked away for 10Up */
if (keyState == ERROR) {
keyState = EAT;
errState = IDLE; errFlag = 0;
// myOut(DBAR); /* DBAR so we can see this path */
myOut(ASTERISK, 0x1800); /* DBAR so we can see this path */
hideChar = 100;
bzzzt = 0;
}
else if (keyState == FERROR) {
keyState = FIND;
aPtr = findStr(xPtr, nLong, 0);
/* errState = IDLE; errFLag = 0; NO! not here - we are in an error free scenario. */
// myOut(ABAR); /* 'k' for error! ABAR so we can see this path */
myOut(ASTERISK, 0x1800); /* 'k' for error! ABAR so we can see this path */
hideChar = 100;
bzzzt = 0;
}
// busyIt(0); /* i be done */
return;
}
/* "else" '#' handled into sleep or FIND, but on KEYUP not! */
/* here '#' always invokes FINDI initialize find mode */
if (theKey == HASHMARK) {
/* kludge */
if (keyState == FINDI) errorStop(907);
bzzzt = 0;
keyState = FINDI;
red = 15;
green = 15;
// myOut(EQ); hideChar = 12;
myOut(HASHMARK, WHITE);
theEvent = NOEVENT;
// busyIt(0); /* i be done */
return;
}
/* now it is a digit... */
switch (keyState) {
case SHOW :
case EAT : /* ...in the context of a test */
keyState = EAT; /* if we are coming out of SHOW */
if (theKey == nextDigit) {
//myOut(GBAR); hideChar = 12;
myOut(theKey, GREEN); hideChar = 12;
green = 9;
advance();
errFlag = 0; errFSM();
runLength++;
if (runLength == 10) { /* 10? 50! */
yellow = 20;
runLength = 0;
}
}
else {
//myOut(E); /* E error */
myOut(ASTERISK, RED);
/* red = 20; */
bzzzt = 0xff;
errFlag = 1; errFSM();
red = 0; green = 0;
keyState = ERROR;
}
break;
case ERROR : /* ...in the context of an error */
case FERROR : /* ...in the context of an error */
break; /* yes, ignoring digits */
case FINDI : /* ...in the context of a search initiation */
green = 12;
red = 12;
// myOut(EQ); hideChar = 12;
myOut(theKey, MAGENTA); hideChar = 12;
nLong = 1;
aPtr = find(theKey, 0);
keyState = FIND;
errState = IDLE; errFlag = 0; errFSM();
break;
case FIND : /* ...in the context of a search continuation */
while (aPtr != MAX) {
if (piAt(aPtr + nLong) == theKey) {
nLong++; /* a remains valid */
bPtr = findStr(aPtr, nLong, aPtr + 1);
if (bPtr == MAX) {
/*printf(" ! "); green flash
stop = 1;*/
green = 12;
// myOut(GBAR); hideChar = 12;
myOut(theKey, GREEN); hideChar = 12;
piIdx = aPtr + nLong; advance();
/* disambiguous. errFSM count low by nLong verify */
keyState = EAT; errFlag = 0; errFSM();
/* errState = IDLE; errFlag = 0; errFSM(); */
}
else {
/*printf(" ~ "); yellow flash */
/* still ambiguous */
green = 12;
red = 12;
// myOut(EQ); hideChar = 12;
myOut(theKey, MAGENTA); hideChar = 12;
}
break;
}
else {
xPtr = aPtr; /* preserve a valid aPtr */
aPtr = findStr(aPtr, nLong, aPtr + 1);
continue;
}
}
if (aPtr == MAX) {
/* FIX THIS BETTER - I think I did. */
/*printf(" x "); red flash
stop = 1;*/
/* red = 30; */
myOut(ASTERISK, RED); /* E error */
bzzzt = 0xff; /* lamp just ON */
keyState = FERROR; /* kludge for coming out */
/* piIdx = xPtr + nLong; advance();
errState = IDLE; okay? */
}
break;
}
// busyIt(0); /* i be done */
return;
}
void advance() /* will use piAt()? */
{
if (piIdx == (MAX + 1)) /* not 1000! */
halt(); /* seems harsh */
nextDigit = piAt(piIdx);
piIdx++;
}
void halt()
{
mChar nx;
nx = 7;
while (--nx)
Serial.println("The End."), delay(230);
printf(" halt. ");
piIdx = 0;
keyState = ERROR;
bzzzt = 0xff;
}
/**/
mChar piAt(mInt idx)
{
mChar ix;
if (idx > MAX) return (0xf);
ix = pi[idx >> 1];
return ((idx & 1) ? (ix & 0xf) : (ix >> 4));
}
mInt find(mChar thisDigit, mInt atOrAfter)
{
mInt ix;
ix = atOrAfter;
while (piAt(ix) != thisDigit) {
/* ... this seemed to fix an error, but should not have!
except it did, the error was found */
if (ix == MAX) break;
ix++;
}
return (ix);
}
mInt findStr(mInt atHere, mChar length, mInt atOrAfter)
{
mInt ix;
mChar ii;
if ((atOrAfter + length) > MAX)
return (MAX); /* we need a "not found"? */
ix = atOrAfter;
for (; ; ) {
ix = find(piAt(atHere), ix);
if (ix == MAX) return (MAX);
for (ii = 1; ii < length; ii++)
if (piAt(atHere + ii) != piAt(ix + ii))
break;
if (ii == length) return (ix);
else ix++;
}
/* doesn't return (0); from here ever FIX THIS SOMEHOW */
}
/**/
// removed from loop, part of handling the '*' and the rail
/*
if (tsChar == '*') { // all interpretation...
Serial.println(" * in tsEvent");
// printf(" i see '*' and ");
if (bzzzt) {
theKey = ASTERISK;
//...
keyFSM("tsEvent '*' and bzzzt");
//...
printf("nChars = %d\n", nChars);
piIdx -= nChars + 1;
advance();
content[nChars] = BLAMK;
// myArea10Char();
// printf(" wrote an '*' at %d\n", nChars);
return;
}
// not bzzzt, eat last char if
if (nChars) {
nChars--;
content[nChars] = BLAMK;
// myArea10Char();
// printf(" over wrote the char at %d\n", nChars);
return;
}
else return;
} // ...of '*' is right here
// so for the '#' reveal kludge 10 APR 2017 we go into k/main (yikes)
//...
kmain(incomingByte);
}
}
TEXT A
TEXT B
TEXT C
TEXT D