brotherax_teletype/brotherax240_cntrl/src/main.cpp

721 lines
23 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "Arduino.h"
bool use_rn=false; //if use_rn=true, CR and LF only do what they say. if use_rn=false \r is ignored and \n does CRLF(=Enter)
//Arduino 1.8.3
//STM32F103C, 64k flash
//upload method: serial (A9 to RX, A10 to TX)
//To upload set Boot0 jumper to 1 (the one further away from reset btn) and press reset (stm will boot from flash wich contains uart to flash uploader)
//To boot program after restart set Boot0 jumper to 0
//#define DEBUG
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
void checkSerial();
bool textAvailable();
bool checkTextbuffer();
void sendChar(char c,char c2);
void coilInterrupt();
//pins die nicht gehn als output:
/* PA15
* PB3
* PB4
* PA12
*/
#define SCAN_0 PA1
#define SCAN_1 PA2
#define SCAN_2 PA3
#define SCAN_3 PA4
#define SCAN_4 PA5
#define SCAN_5 PA6
#define SCAN_6 PA7
#define SCAN_7 PB0
#define SCAN_8 PB1
#define IN_A PB13 //geht
#define IN_B PB14 //geht
#define IN_C PB15 //geht
#define IN_D PB5 //geht
#define IN_E PB6 //geht
#define IN_F PB7 //geht
#define IN_G PB8 //geht
#define IN_H PB9 //geht
#define PIN_LED PC13
#define PIN_COIL_SENSE PB11
#define LEDON LOW
#define LEDOFF HIGH
#define NUM_SCANPINS 9
int scanaddress[]={SCAN_0,SCAN_1,SCAN_2,SCAN_3,SCAN_4,SCAN_5,SCAN_6,SCAN_7,SCAN_8};
int inaddress[]={IN_A,IN_B,IN_C,IN_D,IN_E,IN_F,IN_G,IN_H};
byte keymatrix[128][NUM_SCANPINS]={
//0 1 2 3 4 5 6 7 8 <- see rows (left side) in docs/brotherax240_keyboard.JPG
//content at this array position is pressed keys 2^0 - 2^7=A-H. If Column 0(A) and 7(H) should be used, 2^0+2^7=1+128=129 is entered
{0,0,0,0,0,0,0,0,0}, //0 = [NULL]
{0,0,0,0,0,0,0,0,0}, //1 = [START OF HEADING]
{0,0,0,0,0,0,0,0,0}, //2 = [START OF TEXT]
{0,0,0,0,0,0,0,0,0}, //3 = [END OF TEXT]
{0,0,0,0,0,0,0,0,0}, //4 = [END OF TRANSMISSION]
{0,0,0,0,0,0,0,0,0}, //5 = [ENQUIRY]
{0,0,0,0,0,0,0,0,0}, //6 = [ACKNOWLEDGE]
{0,0,0,0,0,0,0,0,0}, //7 = [BELL]
{0,64,0,0,0,0,0,0,0}, //8 = [BACKSPACE]
{0,0,0,0,0,0,0,0,0}, //9 = [HORIZONTAL TAB]
{0,4,0,0,0,0,0,0,0}, //10 = [LINE FEED]
{0,0,0,0,0,0,0,0,0}, //11 = [VERTICAL TAB]
{0,0,0,0,0,0,0,0,0}, //12 = [FORM FEED]
{0,0,0,0,0,0,0,0,0}, //13 = [CARRIAGE RETURN]
{0,0,0,0,0,0,0,0,0}, //14 = [SHIFT OUT]
{0,0,0,0,0,0,0,0,0}, //15 = [SHIFT IN]
{0,0,0,0,0,0,0,0,0}, //16 = [DATA LINK ESCAPE]
{0,0,0,0,0,0,0,0,0}, //17 = [DEVICE CONTROL 1]
{0,0,0,0,0,0,0,0,0}, //18 = [DEVICE CONTROL 2]
{0,0,0,0,0,0,0,0,0}, //19 = [DEVICE CONTROL 3]
{0,0,0,0,0,0,0,0,0}, //20 = [DEVICE CONTROL 4]
{0,0,0,0,0,0,0,0,0}, //21 = [NEGATIVE ACKNOWLEDGE]
{0,0,0,0,0,0,0,0,0}, //22 = [SYNCHRONOUS IDLE]
{0,0,0,0,0,0,0,0,0}, //23 = [END OF TRANS. BLOCK]
{0,0,0,0,0,0,0,0,0}, //24 = [CANCEL]
{0,0,0,0,0,0,0,0,0}, //25 = [END OF MEDIUM]
{0,0,0,0,0,0,0,0,0}, //26 = [SUBSTITUTE]
{0,0,0,0,0,0,0,0,0}, //27 = SCAPE]
{0,0,0,0,0,0,0,0,0}, //28 = [FILE SEPARATOR]
{0,0,0,0,0,0,0,0,0}, //29 = [GROUP SEPARATOR]
{0,0,0,0,0,0,0,0,0}, //30 = [RECORD SEPARATOR]
{0,0,0,0,0,0,0,0,0}, //31 = [UNIT SEPARATOR]
{0,128,0,0,0,0,0,0,0}, //32 = [SPACE] //H7 H=1 = 7
{128,0,0,0,0,0,0,64,0}, //33 = !
{128,0,0,0,0,0,0,0,32}, //34 = "
{0,0,0,0,0,0,0,32,0}, //35 = #
{128,0,0,0,0,0,0,0,16}, //36 = $
{128,0,0,0,0,0,0,8,0}, //37 = %
{128,0,0,0,0,0,0,0,8}, //38 = &
{128,0,0,0,0,0,0,32,0}, //39 = '
{128,0,0,0,0,0,0,0,1}, //40 = (
{128,0,0,0,0,0,0,2,0}, //41 = )
{128,0,0,0,0,0,0,0,128}, //42 = *
{0,0,0,0,0,0,0,0,128}, //43 = +
{0,0,0,0,128,0,0,0,0}, //44 = ,
{0,0,0,128,0,0,0,0,0}, //45 = -
{0,0,0,0,0,128,0,0,0}, //46 = .
{128,0,0,0,0,0,0,1,0}, //47 = /
{0,0,0,0,0,0,0,0,2}, //48 = 0
{0,0,0,0,0,0,0,64,0}, //49 = 1
{0,0,0,0,0,0,0,0,32}, //50 = 2
{0,0,0,0,0,0,0,16,0}, //51 = 3
{0,0,0,0,0,0,0,0,16}, //52 = 4
{0,0,0,0,0,0,0,8,0}, //53 = 5
{0,0,0,0,0,0,0,0,8}, //54 = 6
{0,0,0,0,0,0,0,1,0}, //55 = 7
{0,0,0,0,0,0,0,0,1}, //56 = 8
{0,0,0,0,0,0,0,2,0}, //57 = 9
{128,0,0,0,0,128,0,0,0}, //58 = :
{128,0,0,0,128,0,0,0,0}, //59 = ;
{0,0,0,0,0,0,0,0,0}, //60 = <
{128,0,0,0,0,0,0,0,2}, //61 = =
{0,0,0,0,0,0,0,0,0}, //62 = >
{128,0,0,0,0,0,0,4,0}, //63 = ?
{0,0,0,0,0,0,0,0,0}, //64 = @
{128,0,0,0,0,4,0,0,0}, //65 = A
{128,0,0,0,64,0,0,0,0}, //66 = B
{128,0,0,0,32,0,0,0,0}, //67 = C
{128,0,0,0,0,8,0,0,0}, //68 = D
{128,0,0,0,0,0,32,0,0}, //69 = E
{128,0,0,0,0,32,0,0,0}, //70 = F
{128,0,0,0,0,16,0,0,0}, //71 = G
{128,0,0,0,0,64,0,0,0}, //72 = H
{128,0,0,64,0,0,0,0,0}, //73 = I
{128,0,0,0,0,1,0,0,0}, //74 = J
{128,0,0,0,0,0,1,0,0}, //75 = K
{128,0,0,0,0,0,2,0,0}, //76 = L
{128,0,0,0,1,0,0,0,0}, //77 = M
{128,0,0,0,4,0,0,0,0}, //78 = N
{128,0,0,0,0,0,4,0,0}, //79 = O
{128,0,0,4,0,0,0,0,0}, //80 = P
{128,0,0,0,0,0,8,0,0}, //81 = Q
{128,0,0,32,0,0,0,0,0}, //82 = R
{128,0,0,0,0,2,0,0,0}, //83 = S
{128,0,0,0,0,0,16,0,0}, //84 = T
{128,0,0,0,0,0,64,0,0}, //85 = U
{128,0,0,0,16,0,0,0,0}, //86 = V
{128,0,0,8,0,0,0,0,0}, //87 = W
{128,0,0,0,8,0,0,0,0}, //88 = X
{128,0,0,0,2,0,0,0,0}, //89 = Y
{128,0,0,16,0,0,0,0,0}, //90 = Z
{0,0,0,0,0,0,0,0,0}, //91 = [
{0,0,0,0,0,0,0,0,0}, //92 = '\'
{0,0,0,0,0,0,0,0,0}, //93 = ]
{0,0,0,0,0,0,0,0,0}, //94 = ^
{128,0,0,128,0,0,0,0,0}, //95 = _
{128,0,0,0,0,0,0,0,4}, //96 = `
{0,0,0,0,0,4,0,0,0}, //97 = a
{0,0,0,0,64,0,0,0,0}, //98 = b
{0,0,0,0,32,0,0,0,0}, //99 = c
{0,0,0,0,0,8,0,0,0}, //100 = d
{0,0,0,0,0,0,32,0,0}, //101 = e
{0,0,0,0,0,32,0,0,0}, //102 = f
{0,0,0,0,0,16,0,0,0}, //103 = g
{0,0,0,0,0,64,0,0,0}, //104 = h
{0,0,0,64,0,0,0,0,0}, //105 = i
{0,0,0,0,0,1,0,0,0}, //106 = j
{0,0,0,0,0,0,1,0,0}, //107 = k
{0,0,0,0,0,0,2,0,0}, //108 = l
{0,0,0,0,1,0,0,0,0}, //109 = m
{0,0,0,0,4,0,0,0,0}, //110 = n
{0,0,0,0,0,0,4,0,0}, //111 = o
{0,0,0,4,0,0,0,0,0}, //112 = p
{0,0,0,0,0,0,8,0,0}, //113 = q
{0,0,0,32,0,0,0,0,0}, //114 = r
{0,0,0,0,0,2,0,0,0}, //115 = s
{0,0,0,0,0,0,16,0,0}, //116 = t
{0,0,0,0,0,0,64,0,0}, //117 = u
{0,0,0,0,16,0,0,0,0}, //118 = v
{0,0,0,8,0,0,0,0,0}, //119 = w
{0,0,0,0,8,0,0,0,0}, //120 = x
{0,0,0,0,2,0,0,0,0}, //121 = y
{0,0,0,16,0,0,0,0,0}, //122 = z
{0,0,0,0,0,0,0,0,0}, //123 = {
{0,32,0,0,0,0,64,0,0}, //124 = | CODE+u
{0,0,0,0,0,0,0,0,0}, //125 = }
{0,0,0,0,0,0,0,0,0}, //126 = ~
{0,0,0,0,0,0,0,0,0} //127 = [DEL}
};
byte keymatrix195[256][NUM_SCANPINS]= {//matrix for 2 byte characters starting with 195
{0,0,0,0,0,0,0,0,0}, // 195,0 =
{0,0,0,0,0,0,0,0,0}, // 195,1 =
{0,0,0,0,0,0,0,0,0}, // 195,2 =
{0,0,0,0,0,0,0,0,0}, // 195,3 =
{0,0,0,0,0,0,0,0,0}, // 195,4 =
{0,0,0,0,0,0,0,0,0}, // 195,5 =
{0,0,0,0,0,0,0,0,0}, // 195,6 =
{0,0,0,0,0,0,0,0,0}, // 195,7 =
{0,0,0,0,0,0,0,0,0}, // 195,8 =
{0,0,0,0,0,0,0,0,0}, // 195,9 =
{0,0,0,0,0,0,0,0,0}, // 195,10 =
{0,0,0,0,0,0,0,0,0}, // 195,11 =
{0,0,0,0,0,0,0,0,0}, // 195,12 =
{0,0,0,0,0,0,0,0,0}, // 195,13 =
{0,0,0,0,0,0,0,0,0}, // 195,14 =
{0,0,0,0,0,0,0,0,0}, // 195,15 =
{0,0,0,0,0,0,0,0,0}, // 195,16 =
{0,0,0,0,0,0,0,0,0}, // 195,17 =
{0,0,0,0,0,0,0,0,0}, // 195,18 =
{0,0,0,0,0,0,0,0,0}, // 195,19 =
{0,0,0,0,0,0,0,0,0}, // 195,20 =
{0,0,0,0,0,0,0,0,0}, // 195,21 =
{0,0,0,0,0,0,0,0,0}, // 195,22 =
{0,0,0,0,0,0,0,0,0}, // 195,23 =
{0,0,0,0,0,0,0,0,0}, // 195,24 =
{0,0,0,0,0,0,0,0,0}, // 195,25 =
{0,0,0,0,0,0,0,0,0}, // 195,26 =
{0,0,0,0,0,0,0,0,0}, // 195,27 =
{0,0,0,0,0,0,0,0,0}, // 195,28 =
{0,0,0,0,0,0,0,0,0}, // 195,29 =
{0,0,0,0,0,0,0,0,0}, // 195,30 =
{0,0,0,0,0,0,0,0,0}, // 195,31 =
{0,0,0,0,0,0,0,0,0}, // 195,32 =
{0,0,0,0,0,0,0,0,0}, // 195,33 =
{0,0,0,0,0,0,0,0,0}, // 195,34 =
{0,0,0,0,0,0,0,0,0}, // 195,35 =
{0,0,0,0,0,0,0,0,0}, // 195,36 =
{0,0,0,0,0,0,0,0,0}, // 195,37 =
{0,0,0,0,0,0,0,0,0}, // 195,38 =
{0,0,0,0,0,0,0,0,0}, // 195,39 =
{0,0,0,0,0,0,0,0,0}, // 195,40 =
{0,0,0,0,0,0,0,0,0}, // 195,41 =
{0,0,0,0,0,0,0,0,0}, // 195,42 =
{0,0,0,0,0,0,0,0,0}, // 195,43 =
{0,0,0,0,0,0,0,0,0}, // 195,44 =
{0,0,0,0,0,0,0,0,0}, // 195,45 =
{0,0,0,0,0,0,0,0,0}, // 195,46 =
{0,0,0,0,0,0,0,0,0}, // 195,47 =
{0,0,0,0,0,0,0,0,0}, // 195,48 =
{0,0,0,0,0,0,0,0,0}, // 195,49 =
{0,0,0,0,0,0,0,0,0}, // 195,50 =
{0,0,0,0,0,0,0,0,0}, // 195,51 =
{0,0,0,0,0,0,0,0,0}, // 195,52 =
{0,0,0,0,0,0,0,0,0}, // 195,53 =
{0,0,0,0,0,0,0,0,0}, // 195,54 =
{0,0,0,0,0,0,0,0,0}, // 195,55 =
{0,0,0,0,0,0,0,0,0}, // 195,56 =
{0,0,0,0,0,0,0,0,0}, // 195,57 =
{0,0,0,0,0,0,0,0,0}, // 195,58 =
{0,0,0,0,0,0,0,0,0}, // 195,59 =
{0,0,0,0,0,0,0,0,0}, // 195,60 =
{0,0,0,0,0,0,0,0,0}, // 195,61 =
{0,0,0,0,0,0,0,0,0}, // 195,62 =
{0,0,0,0,0,0,0,0,0}, // 195,63 =
{0,0,0,0,0,0,0,0,0}, // 195,64 =
{0,0,0,0,0,0,0,0,0}, // 195,65 =
{0,0,0,0,0,0,0,0,0}, // 195,66 =
{0,0,0,0,0,0,0,0,0}, // 195,67 =
{0,0,0,0,0,0,0,0,0}, // 195,68 =
{0,0,0,0,0,0,0,0,0}, // 195,69 =
{0,0,0,0,0,0,0,0,0}, // 195,70 =
{0,0,0,0,0,0,0,0,0}, // 195,71 =
{0,0,0,0,0,0,0,0,0}, // 195,72 =
{0,0,0,0,0,0,0,0,0}, // 195,73 =
{0,0,0,0,0,0,0,0,0}, // 195,74 =
{0,0,0,0,0,0,0,0,0}, // 195,75 =
{0,0,0,0,0,0,0,0,0}, // 195,76 =
{0,0,0,0,0,0,0,0,0}, // 195,77 =
{0,0,0,0,0,0,0,0,0}, // 195,78 =
{0,0,0,0,0,0,0,0,0}, // 195,79 =
{0,0,0,0,0,0,0,0,0}, // 195,80 =
{0,0,0,0,0,0,0,0,0}, // 195,81 =
{0,0,0,0,0,0,0,0,0}, // 195,82 =
{0,0,0,0,0,0,0,0,0}, // 195,83 =
{0,0,0,0,0,0,0,0,0}, // 195,84 =
{0,0,0,0,0,0,0,0,0}, // 195,85 =
{0,0,0,0,0,0,0,0,0}, // 195,86 =
{0,0,0,0,0,0,0,0,0}, // 195,87 =
{0,0,0,0,0,0,0,0,0}, // 195,88 =
{0,0,0,0,0,0,0,0,0}, // 195,89 =
{0,0,0,0,0,0,0,0,0}, // 195,90 =
{0,0,0,0,0,0,0,0,0}, // 195,91 =
{0,0,0,0,0,0,0,0,0}, // 195,92 =
{0,0,0,0,0,0,0,0,0}, // 195,93 =
{0,0,0,0,0,0,0,0,0}, // 195,94 =
{0,0,0,0,0,0,0,0,0}, // 195,95 =
{0,0,0,0,0,0,0,0,0}, // 195,96 =
{0,0,0,0,0,0,0,0,0}, // 195,97 =
{0,0,0,0,0,0,0,0,0}, // 195,98 =
{0,0,0,0,0,0,0,0,0}, // 195,99 =
{0,0,0,0,0,0,0,0,0}, // 195,100 =
{0,0,0,0,0,0,0,0,0}, // 195,101 =
{0,0,0,0,0,0,0,0,0}, // 195,102 =
{0,0,0,0,0,0,0,0,0}, // 195,103 =
{0,0,0,0,0,0,0,0,0}, // 195,104 =
{0,0,0,0,0,0,0,0,0}, // 195,105 =
{0,0,0,0,0,0,0,0,0}, // 195,106 =
{0,0,0,0,0,0,0,0,0}, // 195,107 =
{0,0,0,0,0,0,0,0,0}, // 195,108 =
{0,0,0,0,0,0,0,0,0}, // 195,109 =
{0,0,0,0,0,0,0,0,0}, // 195,110 =
{0,0,0,0,0,0,0,0,0}, // 195,111 =
{0,0,0,0,0,0,0,0,0}, // 195,112 =
{0,0,0,0,0,0,0,0,0}, // 195,113 =
{0,0,0,0,0,0,0,0,0}, // 195,114 =
{0,0,0,0,0,0,0,0,0}, // 195,115 =
{0,0,0,0,0,0,0,0,0}, // 195,116 =
{0,0,0,0,0,0,0,0,0}, // 195,117 =
{0,0,0,0,0,0,0,0,0}, // 195,118 =
{0,0,0,0,0,0,0,0,0}, // 195,119 =
{0,0,0,0,0,0,0,0,0}, // 195,120 =
{0,0,0,0,0,0,0,0,0}, // 195,121 =
{0,0,0,0,0,0,0,0,0}, // 195,122 =
{0,0,0,0,0,0,0,0,0}, // 195,123 =
{0,0,0,0,0,0,0,0,0}, // 195,124 =
{0,0,0,0,0,0,0,0,0}, // 195,125 =
{0,0,0,0,0,0,0,0,0}, // 195,126 =
{0,0,0,0,0,0,0,0,0}, // 195,127 =
{0,0,0,0,0,0,0,0,0}, // 195,128 =
{0,0,0,0,0,0,0,0,0}, // 195,129 =
{0,0,0,0,0,0,0,0,0}, // 195,130 =
{0,0,0,0,0,0,0,0,0}, // 195,131 =
{128,0,0,0,0,0,0,128,0}, // 195,132 = Ä
{0,0,0,0,0,0,0,0,0}, // 195,133 =
{0,0,0,0,0,0,0,0,0}, // 195,134 =
{0,0,0,0,0,0,0,0,0}, // 195,135 =
{0,0,0,0,0,0,0,0,0}, // 195,136 =
{0,0,0,0,0,0,0,0,0}, // 195,137 =
{0,0,0,0,0,0,0,0,0}, // 195,138 =
{0,0,0,0,0,0,0,0,0}, // 195,139 =
{0,0,0,0,0,0,0,0,0}, // 195,140 =
{0,0,0,0,0,0,0,0,0}, // 195,141 =
{0,0,0,0,0,0,0,0,0}, // 195,142 =
{0,0,0,0,0,0,0,0,0}, // 195,143 =
{0,0,0,0,0,0,0,0,0}, // 195,144 =
{0,0,0,0,0,0,0,0,0}, // 195,145 =
{0,0,0,0,0,0,0,0,0}, // 195,146 =
{0,0,0,0,0,0,0,0,0}, // 195,147 =
{0,0,0,0,0,0,0,0,0}, // 195,148 =
{0,0,0,0,0,0,0,0,0}, // 195,149 =
{128,0,0,0,0,0,128,0,0}, // 195,150 = Ö
{0,0,0,0,0,0,0,0,0}, // 195,151 =
{0,0,0,0,0,0,0,0,0}, // 195,152 =
{0,0,0,0,0,0,0,0,0}, // 195,153 =
{0,0,0,0,0,0,0,0,0}, // 195,154 =
{0,0,0,0,0,0,0,0,0}, // 195,155 =
{128,0,0,0,0,0,0,0,64}, // 195,156 = Ü
{0,0,0,0,0,0,0,0,0}, // 195,157 =
{0,0,0,0,0,0,0,0,0}, // 195,158 =
{0,0,0,0,0,0,0,4,0}, // 195,159 = ß
{0,0,0,0,0,0,0,0,0}, // 195,160 =
{0,0,0,0,0,0,0,0,0}, // 195,161 =
{0,0,0,0,0,0,0,0,0}, // 195,162 =
{0,0,0,0,0,0,0,0,0}, // 195,163 =
{0,0,0,0,0,0,0,128,0}, // 195,164 = ä
{0,0,0,0,0,0,0,0,0}, // 195,165 =
{0,0,0,0,0,0,0,0,0}, // 195,166 =
{0,0,0,0,0,0,0,0,0}, // 195,167 =
{0,0,0,0,0,0,0,0,0}, // 195,168 =
{0,0,0,0,0,0,0,0,0}, // 195,169 =
{0,0,0,0,0,0,0,0,0}, // 195,170 =
{0,0,0,0,0,0,0,0,0}, // 195,171 =
{0,0,0,0,0,0,0,0,0}, // 195,172 =
{0,0,0,0,0,0,0,0,0}, // 195,173 =
{0,0,0,0,0,0,0,0,0}, // 195,174 =
{0,0,0,0,0,0,0,0,0}, // 195,175 =
{0,0,0,0,0,0,0,0,0}, // 195,176 =
{0,0,0,0,0,0,0,0,0}, // 195,177 =
{0,0,0,0,0,0,0,0,0}, // 195,178 =
{0,0,0,0,0,0,0,0,0}, // 195,179 =
{0,0,0,0,0,0,0,0,0}, // 195,180 =
{0,0,0,0,0,0,0,0,0}, // 195,181 =
{0,0,0,0,0,0,128,0,0}, // 195,182 = ö
{0,0,0,0,0,0,0,0,0}, // 195,183 =
{0,0,0,0,0,0,0,0,0}, // 195,184 =
{0,0,0,0,0,0,0,0,0}, // 195,185 =
{0,0,0,0,0,0,0,0,0}, // 195,186 =
{0,0,0,0,0,0,0,0,0}, // 195,187 =
{0,0,0,0,0,0,0,0,64}, // 195,188 = ü
{0,0,0,0,0,0,0,0,0}, // 195,189 =
{0,0,0,0,0,0,0,0,0}, // 195,190 =
{0,0,0,0,0,0,0,0,0}, // 195,191 =
{0,0,0,0,0,0,0,0,0}, // 195,192 =
{0,0,0,0,0,0,0,0,0}, // 195,193 =
{0,0,0,0,0,0,0,0,0}, // 195,194 =
{0,0,0,0,0,0,0,0,0}, // 195,195 =
{0,0,0,0,0,0,0,0,0}, // 195,196 =
{0,0,0,0,0,0,0,0,0}, // 195,197 =
{0,0,0,0,0,0,0,0,0}, // 195,198 =
{0,0,0,0,0,0,0,0,0}, // 195,199 =
{0,0,0,0,0,0,0,0,0}, // 195,200 =
{0,0,0,0,0,0,0,0,0}, // 195,201 =
{0,0,0,0,0,0,0,0,0}, // 195,202 =
{0,0,0,0,0,0,0,0,0}, // 195,203 =
{0,0,0,0,0,0,0,0,0}, // 195,204 =
{0,0,0,0,0,0,0,0,0}, // 195,205 =
{0,0,0,0,0,0,0,0,0}, // 195,206 =
{0,0,0,0,0,0,0,0,0}, // 195,207 =
{0,0,0,0,0,0,0,0,0}, // 195,208 =
{0,0,0,0,0,0,0,0,0}, // 195,209 =
{0,0,0,0,0,0,0,0,0}, // 195,210 =
{0,0,0,0,0,0,0,0,0}, // 195,211 =
{0,0,0,0,0,0,0,0,0}, // 195,212 =
{0,0,0,0,0,0,0,0,0}, // 195,213 =
{0,0,0,0,0,0,0,0,0}, // 195,214 =
{0,0,0,0,0,0,0,0,0}, // 195,215 =
{0,0,0,0,0,0,0,0,0}, // 195,216 =
{0,0,0,0,0,0,0,0,0}, // 195,217 =
{0,0,0,0,0,0,0,0,0}, // 195,218 =
{0,0,0,0,0,0,0,0,0}, // 195,219 =
{0,0,0,0,0,0,0,0,0}, // 195,220 =
{0,0,0,0,0,0,0,0,0}, // 195,221 =
{0,0,0,0,0,0,0,0,0}, // 195,222 =
{0,0,0,0,0,0,0,0,0}, // 195,223 =
{0,0,0,0,0,0,0,0,0}, // 195,224 =
{0,0,0,0,0,0,0,0,0}, // 195,225 =
{0,0,0,0,0,0,0,0,0}, // 195,226 =
{0,0,0,0,0,0,0,0,0}, // 195,227 =
{0,0,0,0,0,0,0,0,0}, // 195,228 =
{0,0,0,0,0,0,0,0,0}, // 195,229 =
{0,0,0,0,0,0,0,0,0}, // 195,230 =
{0,0,0,0,0,0,0,0,0}, // 195,231 =
{0,0,0,0,0,0,0,0,0}, // 195,232 =
{0,0,0,0,0,0,0,0,0}, // 195,233 =
{0,0,0,0,0,0,0,0,0}, // 195,234 =
{0,0,0,0,0,0,0,0,0}, // 195,235 =
{0,0,0,0,0,0,0,0,0}, // 195,236 =
{0,0,0,0,0,0,0,0,0}, // 195,237 =
{0,0,0,0,0,0,0,0,0}, // 195,238 =
{0,0,0,0,0,0,0,0,0}, // 195,239 =
{0,0,0,0,0,0,0,0,0}, // 195,240 =
{0,0,0,0,0,0,0,0,0}, // 195,241 =
{0,0,0,0,0,0,0,0,0}, // 195,242 =
{0,0,0,0,0,0,0,0,0}, // 195,243 =
{0,0,0,0,0,0,0,0,0}, // 195,244 =
{0,0,0,0,0,0,0,0,0}, // 195,245 =
{0,0,0,0,0,0,0,0,0}, // 195,246 =
{0,0,0,0,0,0,0,0,0}, // 195,247 =
{0,0,0,0,0,0,0,0,0}, // 195,248 =
{0,0,0,0,0,0,0,0,0}, // 195,249 =
{0,0,0,0,0,0,0,0,0}, // 195,250 =
{0,0,0,0,0,0,0,0,0}, // 195,251 =
{0,0,0,0,0,0,0,0,0}, // 195,252 =
{0,0,0,0,0,0,0,0,0}, // 195,253 =
{0,0,0,0,0,0,0,0,0}, // 195,254 =
{0,0,0,0,0,0,0,0,0} // 195,255 =
};
#define TEXTBUFFERSIZE 8192
byte textbuffer[TEXTBUFFERSIZE]; //ca 60-65 Zeilen pro DinA4, a 65 characters (10 Pica), 78 characters (12 Elite) or 97 characters (15 Mikron)
uint16_t bufferpos_write=0;
uint16_t bufferpos_read=0;
#define DELAY_KEYPRESS 100 //delay in ms between keypresses
long last_keypress=0;
bool linefeed_used=false; //if Linefeed key was used CR (CODE+Enter) must be pressed twice for full CR
volatile uint16_t count_bufferedchars=0; //keeps track of buffered characters on the typewriter
#define MAX_TYPEWRITER_BUFFERCOUNT 2
#define MAXIMUM_COILWAIT 5000 //if no interrupt from coil received decrease buffer count after this time (in ms)
void setup() {
if (use_rn) { //change matrix mapping if CR and LF are used
byte _km10[NUM_SCANPINS]={0,0,16,0,0,0,0,0,0}; //10 = [LINE FEED]
byte _km13[NUM_SCANPINS]={0,36,0,0,0,0,0,0,0}; //13 = [CARRIAGE RETURN]
for (uint8_t i=0;i<NUM_SCANPINS;i++){ //copy array content
keymatrix[10][i]=_km10[i];
keymatrix[13][i]=_km13[i];
}
}
Serial1.begin(9600);
pinMode(SCAN_0,INPUT_PULLUP);
pinMode(SCAN_1,INPUT_PULLUP);
pinMode(SCAN_2,INPUT_PULLUP);
pinMode(SCAN_3,INPUT_PULLUP);
pinMode(SCAN_4,INPUT_PULLUP);
pinMode(SCAN_5,INPUT_PULLUP);
pinMode(SCAN_6,INPUT_PULLUP);
pinMode(SCAN_7,INPUT_PULLUP);
pinMode(SCAN_8,INPUT_PULLUP);
pinMode(IN_A,OUTPUT);
pinMode(IN_B,OUTPUT);
pinMode(IN_C,OUTPUT);
pinMode(IN_D,OUTPUT);
pinMode(IN_E,OUTPUT);
pinMode(IN_F,OUTPUT);
pinMode(IN_G,OUTPUT);
pinMode(IN_H,OUTPUT);
digitalWrite(IN_A,LOW);
digitalWrite(IN_B,LOW);
digitalWrite(IN_C,LOW);
digitalWrite(IN_D,LOW);
digitalWrite(IN_E,LOW);
digitalWrite(IN_F,LOW);
digitalWrite(IN_G,LOW);
digitalWrite(IN_H,LOW);
pinMode(PIN_COIL_SENSE, INPUT_PULLUP);
attachInterrupt(PIN_COIL_SENSE, coilInterrupt, RISING); //pin pulled low if coil powered
pinMode(PIN_LED,OUTPUT);
digitalWrite(PIN_LED,LEDON);
delay(500);
digitalWrite(PIN_LED,LEDOFF);
Serial1.println("Brother AX-240 Serialmod");
}
void loop() {
checkSerial(); //check serial buffer and write to textbuffer
if (millis()>last_keypress+DELAY_KEYPRESS){
if (count_bufferedchars<MAX_TYPEWRITER_BUFFERCOUNT) //wait if too many chars are send to typewriter buffer
{
bool charsend=checkTextbuffer(); //check one character from serial buffer and send to typewriter
//Serial1.print(6); //Send ACK
if (charsend){ //a character was actually sent
last_keypress=millis();
}
}
if (count_bufferedchars>0 && millis()>last_keypress+MAXIMUM_COILWAIT){ //coil response timeout
count_bufferedchars--; //decrease buffer count
last_keypress=millis();
}
}
if (count_bufferedchars>=MAX_TYPEWRITER_BUFFERCOUNT){
digitalWrite(PIN_LED,LEDON); //led on -> wait for buffer to empty
}else{
digitalWrite(PIN_LED,LEDOFF); //led off -> buffer isnt full
}
}
void checkSerial(){
while(Serial1.available()){
if (Serial1.available()>0){
char c=Serial1.read();
bufferpos_write++;
bufferpos_write%=TEXTBUFFERSIZE;
textbuffer[bufferpos_write]=c; //bufferpos_write points to last written char position
if (use_rn) {
if (c==10 || (c==13 && linefeed_used)) {
//char 10 is LineFeed. If emulated by pressing 2,4 (arrow down, top right of keyboard), key needs to be pressed twice to get one line spacing
//Also CR (CODE+Enter) must be pressed twice, if LineFeed key is used in the same Text Line
bufferpos_write++;
bufferpos_write%=TEXTBUFFERSIZE;
textbuffer[bufferpos_write]=c; //bufferpos_write points to last written char position
}
if (c==13) {
linefeed_used=false;
}else if(c==10){
linefeed_used=true;
}
}
}
}
}
bool textAvailable(){
if (bufferpos_write!=bufferpos_read){
return true;
}
return false;
}
bool checkTextbuffer(){
bool returnvalue=false;
//while(Serial1.available()){
//if (Serial1.available()>0){
if (textAvailable()) { //buffer contains unread chars
bufferpos_read++;//bufferpos_read points to last read char position
bufferpos_read%=TEXTBUFFERSIZE;
char c = textbuffer[bufferpos_read];
#ifdef DEBUG
Serial1.print((uint8_t)c); //echo
Serial1.print("="); //echo
Serial1.println(c); //echo
#endif
if (c==195 || c==194) { //umlaut
#ifdef DEBUG
Serial1.print("Uml:");
#endif
while (!textAvailable()){ checkSerial(); } //wait for next byte and checkSerial in between
bufferpos_read++;//bufferpos_read points to last read char position
bufferpos_read%=TEXTBUFFERSIZE;
char c2=textbuffer[bufferpos_read];
#ifdef DEBUG
Serial1.println((uint8_t)c2); //echo
#endif
sendChar(c,c2); //2 byte character
returnvalue=true;
}else{
sendChar(c,0); //normal 1 byte character
returnvalue=true;
}
}
//}
return returnvalue; //true= character send, false=buffer was empty
}
void sendChar(char c,char c2) { //c2 =0 for 1 byte characters
uint8_t keypress[NUM_SCANPINS]={0,0,0,0,0,0,0,0,0}; //keypress contains id of input pin. 0=not pressed, A=2^0, B=2^1, C=2^3 ...
//check if key is implemented (not all 255,255,255,..)
bool keyimplemented=false;
for (uint8_t i=0;i<NUM_SCANPINS;i++){
if (keymatrix[c][i]!=0){
keyimplemented=true;
}
}
if (keyimplemented && ( (c>=32 && c<127) || c==10 || c==13 ) ) { //1 byte char
#ifdef DEBUG
Serial1.println("1 byte char");
#endif
for (uint8_t i=0;i<NUM_SCANPINS;i++){
keypress[i]=keymatrix[c][i]; //copy keypress connections for current char
}
}else if (c==195){ //umlauts
#ifdef DEBUG
Serial1.println("2 byte char");
#endif
for (uint8_t i=0;i<NUM_SCANPINS;i++){
keypress[i]=keymatrix195[c2][i]; //copy keypress connections for current char
}
}else if(c==194){ //others
if (c2==167){ // §
keypress[0]=128; keypress[7]=16; //shift + 3
}else if (c2==176){ // °
keypress[1]=32; keypress[3]=8; //CODE + W
}
}else if(c>=32){ //not recognized character (everything else) and not a control command
keypress[1]=128; // [SPACE] //print space to keep text aligned
}
//Update count buffered chars
if ((c>32 && c<127) ) { //pressed key uses hammer. (32 is space)
if (keyimplemented){ //keymatrix was implemented for c (not 255,255...)
count_bufferedchars++; //character send to typewriter. Increase character buffer count
}
}
//digitalWrite(PIN_LED,LEDON);
#define HOLDCYCLES 3
for (uint8_t hold=0;hold<HOLDCYCLES;hold++){ //cycles to hold the key
for (uint8_t i=0;i<NUM_SCANPINS;i++){
if (keypress[i]!=0){ //some key on scan i will be pressed
unsigned int scanaddr=scanaddress[i]; //get pin address for i SCAN_i
uint8_t keypresses=keypress[i];
while(digitalRead(scanaddr)){ }//wait while high
for (int b=0;b<8;b++) { //set inaddress outputs to 1 bits from keypresses. Example: if keypresses=129, inaddress[0] and inaddress[7] will be used
if (CHECK_BIT(keypresses,b)) {
digitalWrite(inaddress[b],HIGH);
}
}
while(!digitalRead(scanaddr)){ } //wait while low
for (int b=0;b<8;b++) { //set inaddress outputs to 1 bits from keypresses. Example: if keypresses=129, inaddress[0] and inaddress[7] will be used
if (CHECK_BIT(keypresses,b)) {
digitalWrite(inaddress[b],LOW); //set high -> transistor pulls input of printer to ground
}
}
}
}
}
//digitalWrite(PIN_LED,LEDOFF);
}
void coilInterrupt(){
if (count_bufferedchars>0){
count_bufferedchars--;
}
}