// learning to display and modify text by way of simple
 // keyboard and MAX modules
 
#include <MD_MAX72xx.h>
#include <MD_Parola.h>
#include <SPI.h>
// MAX control
#define DIN 51 // data in orange
#define CLK 52 // clock yellow
#define CS01 53 // MAX 79xx 1 green
#define CS02 50 // MAX 79xx 2 blue
#define CS03 49 // MAX 79xx 3 violet
#define CS04 48 // MAX 79xx 4 grey
// row 1
#define PIN1 22 // chr 1
#define PIN2 23 // chr 2
#define PIN3 24 // chr 3
#define PIN4 25 // chr 4
#define PIN5 26 // chr 5
#define PIN6 27 // chr 6
#define PIN7 28 // chr 7
#define PIN8 29 // chr 8
#define PIN9 30 // chr 9
#define PIN0 31 // chr 0
// row 2
#define PINQ 32 // chr Q
#define PINW 33 // chr W
#define PINE 34 // chr E
#define PINR 35 // chr R
#define PINT 36 // chr T
#define PINY 37 // chr Y
#define PINU 38 // chr U
#define PINI 39 // chr I
#define PINO 40 // chr O
#define PINP 41 // chr P
// row 3
#define PINA 42 // chr A
#define PINS 43 // chr S
#define PIND 44 // chr D
#define PINF 45 // chr F
#define PING 46 // chr G
#define PINH 47 // chr H
#define PINJ 2 // chr J
#define PINK 3 // chr K
#define PINL 4 // chr L
// row 4
#define PINZ 5 // chr Z
#define PINX 6 // chr X
#define PINC 7 // chr C
#define PINV 8 // chr V
#define PINB 9 // chr B
#define PINN 10 // chr N
#define PINM 11 // chr M
// function
#define PIN12 12 // comma
#define PIN13 13 // full stop
#define PIN14 14 // shift to upper case
#define PIN15 15 // space
#define PIN16 16 // return/enter
#define PIN17 17 // back

#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
#define MAX_DEVICES 16

String a = "_"; // cursor
String b = ">"; // start of line
String c = " ";

uint8_t n = 0;
uint8_t m = 0;
uint8_t l = 0;
// display rows
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS01, MAX_DEVICES);
MD_Parola Q = MD_Parola(HARDWARE_TYPE, CS02, MAX_DEVICES);
MD_Parola R = MD_Parola(HARDWARE_TYPE, CS03, MAX_DEVICES);
MD_Parola S = MD_Parola(HARDWARE_TYPE, CS04, MAX_DEVICES);
// subr calls
void lower() {
  back1(); b = b + c + a; delay(100); return(b);}
void upperlower() {
  if(m == 1) c.toUpperCase(); back1(); b = b + c + a; m = 0; delay(100); return (b);}
void back1() {
  l = b.length(); l = l - 1; b.remove(l); return(b);}
void back2() {
  l = b.length(); l = l - 2; b.remove(l); return(b);}

void setup() {
  P.begin(); // MAX row 1
  Q.begin(); // MAX row 2
  R.begin(); // MAX row 3 
  S.begin(); // MAX row 4

  pinMode(PIN1, INPUT_PULLUP); // chr 1
  pinMode(PIN2, INPUT_PULLUP); // chr 2
  pinMode(PIN3, INPUT_PULLUP); // chr 3
  pinMode(PIN4, INPUT_PULLUP); // chr 4
  pinMode(PIN5, INPUT_PULLUP); // chr 5
  pinMode(PIN6, INPUT_PULLUP); // chr 6
  pinMode(PIN7, INPUT_PULLUP); // chr 7
  pinMode(PIN8, INPUT_PULLUP); // chr 8
  pinMode(PIN9, INPUT_PULLUP); // chr 9
  pinMode(PIN0, INPUT_PULLUP); // chr 0

  pinMode(PINQ, INPUT_PULLUP); // chr Q
  pinMode(PINW, INPUT_PULLUP); // chr W
  pinMode(PINE, INPUT_PULLUP); // chr E
  pinMode(PINR, INPUT_PULLUP); // chr R
  pinMode(PINT, INPUT_PULLUP); // chr T
  pinMode(PINY, INPUT_PULLUP); // chr Y
  pinMode(PINU, INPUT_PULLUP); // chr U
  pinMode(PINI, INPUT_PULLUP); // chr I
  pinMode(PINO, INPUT_PULLUP); // cdr O
  pinMode(PINP, INPUT_PULLUP); // chr P

  pinMode(PINA, INPUT_PULLUP); // chr A
  pinMode(PINS, INPUT_PULLUP); // chr S
  pinMode(PIND, INPUT_PULLUP); // chr D
  pinMode(PINF, INPUT_PULLUP); // chr F
  pinMode(PING, INPUT_PULLUP); // chr G
  pinMode(PINH, INPUT_PULLUP); // chr H
  pinMode(PINJ, INPUT_PULLUP); // chr J
  pinMode(PINK, INPUT_PULLUP); // chr K
  pinMode(PINL, INPUT_PULLUP); // chr L

  pinMode(PINZ, INPUT_PULLUP); // chr Z
  pinMode(PINX, INPUT_PULLUP); // chr X
  pinMode(PINC, INPUT_PULLUP); // chr C
  pinMode(PINV, INPUT_PULLUP); // chr V
  pinMode(PINB, INPUT_PULLUP); // chr B
  pinMode(PINN, INPUT_PULLUP); // chr N
  pinMode(PINM, INPUT_PULLUP); // chr M

  pinMode(PIN12, INPUT_PULLUP); // comma
  pinMode(PIN13, INPUT_PULLUP); // full stop
  pinMode(PIN14, INPUT_PULLUP); // shift to upper case
  pinMode(PIN15, INPUT_PULLUP); // space bar
  pinMode(PIN16, INPUT_PULLUP); // return/enter
  pinMode(PIN17, INPUT_PULLUP); // back

  pinMode(DIN, OUTPUT); // DIN
  pinMode(CLK, OUTPUT); // CLK
  pinMode(CS01, OUTPUT); // CS01
  pinMode(CS02, OUTPUT); // CS02
  pinMode(CS03, OUTPUT); // CS03
  pinMode(CS04, OUTPUT); // CS04
}
void loop() {
 // read buttons in sequence of use
  if(digitalRead(PINE) == LOW) {c = "e"; upperlower(); goto more;}
  if(digitalRead(PINT) == LOW) {c = "t"; upperlower(); goto more;}
  if(digitalRead(PINA) == LOW) {c = "a"; upperlower(); goto more;}
  if(digitalRead(PINO) == LOW) {c = "o"; upperlower(); goto more;}
  if(digitalRead(PINI) == LOW) {c = "i"; upperlower(); goto more;}
  if(digitalRead(PINN) == LOW) {c = "n"; upperlower(); goto more;}
  if(digitalRead(PINS) == LOW) {c = "s"; upperlower(); goto more;}
  if(digitalRead(PINH) == LOW) {c = "h"; upperlower(); goto more;}
  if(digitalRead(PINR) == LOW) {c = "r"; upperlower(); goto more;}
  if(digitalRead(PIND) == LOW) {c = "d"; upperlower(); goto more;}
  if(digitalRead(PINL) == LOW) {c = "l"; upperlower(); goto more;}
  if(digitalRead(PINC) == LOW) {c = "c"; upperlower(); goto more;}
  if(digitalRead(PINU) == LOW) {c = "u"; upperlower(); goto more;}
  if(digitalRead(PINM) == LOW) {c = "m"; upperlower(); goto more;}
  if(digitalRead(PINW) == LOW) {c = "w"; upperlower(); goto more;}
  if(digitalRead(PINF) == LOW) {c = "f"; upperlower(); goto more;}
  if(digitalRead(PING) == LOW) {c = "g"; upperlower(); goto more;}
  if(digitalRead(PINY) == LOW) {c = "y"; upperlower(); goto more;}
  if(digitalRead(PINP) == LOW) {c = "p"; upperlower(); goto more;}
  if(digitalRead(PINB) == LOW) {c = "b"; upperlower(); goto more;}
  if(digitalRead(PINV) == LOW) {c = "v"; upperlower(); goto more;}
  if(digitalRead(PINK) == LOW) {c = "k"; upperlower(); goto more;}
  if(digitalRead(PINJ) == LOW) {c = "j"; upperlower(); goto more;}
  if(digitalRead(PINX) == LOW) {c = "x"; upperlower(); goto more;}
  if(digitalRead(PINQ) == LOW) {c = "q"; upperlower(); goto more;}
  if(digitalRead(PINZ) == LOW) {c = "z"; upperlower(); goto more;}
  if(digitalRead(PIN1) == LOW) {c = "1"; lower(); goto more;} 
  if(digitalRead(PIN2) == LOW) {c = "2"; lower(); goto more;}
  if(digitalRead(PIN3) == LOW) {c = "3"; lower(); goto more;}
  if(digitalRead(PIN4) == LOW) {c = "4"; lower(); goto more;}
  if(digitalRead(PIN5) == LOW) {c = "5"; lower(); goto more;}
  if(digitalRead(PIN6) == LOW) {c = "6"; lower(); goto more;}
  if(digitalRead(PIN7) == LOW) {c = "7"; lower(); goto more;}
  if(digitalRead(PIN8) == LOW) {c = "8"; lower(); goto more;}
  if(digitalRead(PIN9) == LOW) {c = "9"; lower(); goto more;}
  if(digitalRead(PIN0) == LOW) {c = "0"; lower(); goto more;}
  more:
  // function buttons
  if(digitalRead(PIN12) == LOW) {c = ","; upperlower();} // comma
  if(digitalRead(PIN13) == LOW) {c = "."; upperlower();} // full stop
  if(digitalRead(PIN14) == LOW) m = 1; // upper case
  if(digitalRead(PIN15) == LOW) {c = " "; lower();} //space
  if(digitalRead(PIN16) == LOW) {n = n + 1; b = ">"; delay(100);} // return/enter
  if(digitalRead(PIN17) == LOW) {back2(); b = b + a; delay(500);} // back
 // display text
  if(n == 0) P.print(b); // row 1
  if(n == 1) Q.print(b); // row 2
  if(n == 2) R.print(b); // row 3
  if(n == 3) S.print(b); // row 4
  if(n == 4) {n = 0; b = ">";}
  }