Skip to content

Instantly share code, notes, and snippets.

@adlerweb
Created October 19, 2018 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adlerweb/639672b13353715673716074f350297e to your computer and use it in GitHub Desktop.
Save adlerweb/639672b13353715673716074f350297e to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <SPI.h>
#include <MCP23S17.h>
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
extern "C" {
#include "user_interface.h"
}
const char* kbdmap = "K##.0CRE321MSO654FTe987U";
const char* cmdmap = " +-*/";
#define ROWS 4
#define COLS 6
LiquidCrystal_PCF8574 lcd(0x27);
MCP gpio(0, 15);
int8_t last = 0;
String num1, num2;
byte cmd = 0;
void setPinMode(void) {
uint16_t out = 0xFFFF;
out <<= (ROWS);
gpio.pinMode(out);
gpio.pullupMode(out);
gpio.inputInvert(out);
out = ~out;
gpio.digitalWrite(out);
}
void startLCD(void) {
int error;
Serial.println("LCD...");
Wire.begin(D1, D2);
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Return: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
} else {
Serial.println(": LCD not found.");
}
lcd.begin(20, 2); // initialize the lcd
lcd.home();
lcd.clear();
lcd.print("BitBa // POS-Matrix");
}
void setup() {
Serial.begin(115200);
Serial.print("\nInit...");
gpio.begin();
setPinMode();
Serial.println("OK");
startLCD();
}
int8_t checkKeys(void) {
byte row, col;
int input;
for(row = 0; row < ROWS; row++) {
gpio.digitalWrite(row, LOW);
delay(1);
input = gpio.digitalRead();
gpio.digitalWrite(row, HIGH);
for(col=0; col < COLS; col++) {
if(input & (1<<(ROWS + col))) {
return (byte)(COLS * row + col);
}
}
}
return -1;
}
void loop() {
int8_t input = checkKeys();
int8_t check = 0xFF;
float result = num2.toFloat();
bool change = false;
if(result == 0) result = num1.toFloat();
if(input < (COLS*ROWS) && input >= 0 && last != input) {
Serial.print(input);
Serial.print(": ");
last = input;
if(input >= 0) {
check = kbdmap[input];
Serial.print(check);
}else{
check = 0xFF;
}
Serial.println();
delay(15); //Poor maker debounce
}else{
check = 0xFF;
}
if(check != 0xFF) {
switch(check) {
//K##.0CRE321MSO654FTe987U
case 'K':
cmd = 4;
change = true;
break;
case 'R':
cmd = 3;
change = true;
break;
case 'S':
cmd = 2;
change = true;
break;
case 'T':
cmd = 1;
change = true;
break;
case 'C':
cmd = 0;
num1 = "";
num2 = "";
change = true;
break;
case 'U':
cmd = 0;
num2 = "";
change = true;
break;
case 'F':
if(cmd == 0) {
if(num1.length() > 0) num1.remove(num1.length()-1);
}else{
if(num2.length() > 0) num2.remove(num2.length()-1);
}
change = true;
break;
case '#':
switch(cmd) {
case 1:
result = num1.toFloat() + num2.toFloat();
break;
case 2:
result = num1.toFloat() - num2.toFloat();
break;
case 3:
result = num1.toFloat() * num2.toFloat();
break;
case 4:
result = num1.toFloat() / num2.toFloat();
break;
}
cmd = 0;
num1 = result;
num2 = "";
change = true;
break;
case '.':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if(cmd == 0) {
num1 += (char)check;
}else{
num2 += (char)check;
}
change = true;
}
if(change) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(num1);
if(cmd > 0) {
lcd.setCursor(18, 0);
lcd.print(" ");
lcd.print(cmdmap[cmd]);
lcd.setCursor(0, 1);
lcd.print(num2);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment