#include "storage.h"
// *Note: Don't use PROGMEM, because it'll make the code not get the proper values!!!
int lsindex = 0; // index of read letter in template
int letpos = 0;
int pixpos = 0;
long steppos = 0;
byte letter = 0;
byte letter2 = 0;
byte pixval = 0;
String received = "";
byte cmsht = 29;
byte pmsht = 59;
byte stcpx = 27;
byte stppx = 61;
byte pxln = 9;
long lnfsa = 0;
char overflow = 0;
byte printmode = 0; // defaults to text
byte pxpos = 0;
long dlzka = 0; // length of message
byte waitforbytes = 0; // is it supposed to wait for bytes?
byte iscommand = 0;
void setup() {
DDRB = 255;
PORTB = 0;
pinMode(6, INPUT_PULLUP); // koncový kalibračný spínač (limit switch for homing)
pinMode(A1, INPUT_PULLUP); // posuv papiera von (move paper out of the output(blank line feed))
pinMode(A2, INPUT_PULLUP); // posuv papiera dnu (move paper into the output(blank line unfeed))
pinMode(2, OUTPUT); // stepper 1 step
pinMode(3, OUTPUT); // stepper 1 dir
pinMode(4, OUTPUT); // stepper 2 step
pinMode(5, OUTPUT); // stepper 2 dir
Serial.begin(76800);
digitalWrite(3, LOW);
while (digitalRead(6) == HIGH) {
digitalWrite(2, HIGH);
delayMicroseconds(125);
digitalWrite(2, LOW);
delayMicroseconds(125);
}
digitalWrite(3, HIGH);
for (int s = 0; s < (stcpx * 26); s = s + 1) {
digitalWrite(2, HIGH);
delayMicroseconds(125);
digitalWrite(2, LOW);
delayMicroseconds(125);
}
delay(250);
Serial.write(0x0A); // tento riadok môžete vynechať
}
void loop() {
while (!Serial.available()) {
if (digitalRead(A1) == LOW) {
delay(500);
bplf();
}
if (digitalRead(A2) == LOW) {
delay(500);
bpluf();
}
}
received = sstringr();
for (int f = 0; f < dlzka; f = f + 1) {
letter = received[f];
letter2 = received[f + 1];
if (waitforbytes == 1 && iscommand == 1) {
if (letter == 'G') {
waitforbytes = 0;
lnfsa = pxln * stppx;
crlf();
printmode = 1; // graphics mode
}
if (letter == 'T') {
waitforbytes = 0;
lnfsa = pxln * stppx;
crlf();
printmode = 0; // text mode
}
if (letter == 'L') { // load paper to top margin
waitforbytes = 0;
pxln = 4;
for (byte r = 0; r < 9; r = r + 1) {
bplf();
}
pxln = 9;
}
if (letter == 'E') { // eject paper
waitforbytes = 0;
for (byte r = 0; r < 20; r = r + 1) {
bpluf();
}
}
iscommand = 0;
continue;
}
if (printmode == 0) {
if (letpos > 19) {
letpos = 0;
if (letter != 10 || letter != 13 || letter != 27) {
lnfsa = pxln * stppx;
crlf();
overflow = letter;
dotmatrixtext(overflow);
} else {
lnfsa = pxln * stppx;
crlf();
}
} else if (letter == 10) {
letpos = 0;
lnfsa = pxln * stppx;
crlf();
}
else if (letter == 13) {
letpos = 0;
lnfsa = pxln * stppx;
crlf();
}
else if (iscommand == 0) {
dotmatrixtext(letter);
letpos = letpos + 1;
}
}
if (letter == 150) {
iscommand = 1;
if (letter2 == 'G') {
waitforbytes = 0;
printmode = 1; // graphics mode
}
if (letter2 == 'T') {
waitforbytes = 0;
printmode = 0; // text mode
}
if (letter2 == 'L') { // load paper to top margin
waitforbytes = 0;
pxln = 4;
for (byte r = 0; r < 9; r = r + 1) {
bplf();
}
pxln = 9;
}
if (letter2 == 'E') { // eject paper
waitforbytes = 0;
for (byte r = 0; r < 20; r = r + 1) {
bpluf();
}
}
else {
waitforbytes = 1;
}
f = f + 1;
continue;
}
if (printmode == 1) {
if (pxpos > 119) {
pxpos = 0;
lnfsa = pxln * stppx;
crlf();
}
else {
dotmatrixgraphics(letter);
}
}
}
}
void dotmatrixgraphics(byte graphdat) {
pxln = 7;
digitalWrite(3, HIGH);
pxpos = pxpos + 1;
for (byte y = 0; y < stcpx; y = y + 1) {
steppos = steppos + 1;
digitalWrite(2, HIGH);
delayMicroseconds(cmsht);
digitalWrite(2, LOW);
delayMicroseconds(cmsht);
if (y > 10) {
PORTB = 0;
}
else {
PORTB = graphdat;
}
}
}
void dotmatrixtext(byte character) {
pxln = 9;
digitalWrite(3, HIGH);
lsindex = (character - 32) * 6;
for (byte i = 0; i < 6; i = i + 1) {
pixval = 0;
pixval = font[lsindex + i];
pixpos = pixpos + 1;
pxpos = pxpos + 1;
for (byte y = 0; y < stcpx; y = y + 1) {
steppos = steppos + 1;
digitalWrite(2, HIGH);
delayMicroseconds(cmsht);
digitalWrite(2, LOW);
delayMicroseconds(cmsht);
if (y > 10) {
PORTB = 0;
}
else {
PORTB = pixval;
}
}
}
}
void crlf() {
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
while (steppos > 0) {
steppos = steppos - 1;
digitalWrite(2, HIGH);
delayMicroseconds(cmsht);
digitalWrite(2, LOW);
delayMicroseconds(cmsht);
}
while (lnfsa > 0) {
lnfsa = lnfsa - 1;
digitalWrite(4, HIGH);
delayMicroseconds(pmsht);
digitalWrite(4, LOW);
delayMicroseconds(pmsht);
}
}
void bplf() {
digitalWrite(5, HIGH);
lnfsa = pxln * stppx;
while (lnfsa > 0) {
lnfsa = lnfsa - 1;
digitalWrite(4, HIGH);
delayMicroseconds(pmsht);
digitalWrite(4, LOW);
delayMicroseconds(pmsht);
}
}
void bpluf() {
digitalWrite(5, LOW);
lnfsa = pxln * stppx;
while (lnfsa > 0) {
lnfsa = lnfsa - 1;
digitalWrite(4, HIGH);
delayMicroseconds(pmsht);
digitalWrite(4, LOW);
delayMicroseconds(pmsht);
}
}
String sstringr() {
char message = 0;
dlzka = 0;
String content = "";
while (Serial.available() < 5) {}
while (Serial.available()) {
if (Serial.available() > 31) {
Serial.write(0x13);
}
if (Serial.available() < 7) {
Serial.write(0x11);
}
message = Serial.read();
content += message;
dlzka = dlzka + 1;
}
if (content != "" && !Serial.available()) {
return content;
}
else {
return "";
}
}
Homing switch
Line feed
Carriage move
Paper feed out of printer
Paper feed into printer