I'm writing a program to output text, word wrap it on an OLED, and then output it to a thermal printer.
I've successfully written code to work with the OLED and printer via separate sketches, but my problem is when I try and combine them.
What should happen when I upload the code to the Arduino is the screen should clear and then when I type on a connected PS/2 keyboard it will output the words on the screen.
The "problem" I'm having is nothing is happening on the OLED and the Arduino seems like its frozen because whatever text was on the screen before the new upload is still there.
When I tore apart my code to debug and find out what was, I noticed the introduction of SoftwareSerial.h made my char array: word_buffer not work.
Note: word_buffer does not depend on SoftwareSerial, as it is only for word wrapping in the OLED.
when I comment out the use of word_buffer in the code (in the area under the //word wrap comment), the program will run and I will be able to type on the OLED (of course, since I removed some code it doesn't work perfectly but at least it responds).
I know word_buffer is giving me the issues because if I substitute it for line_buffer (another char array) the code will be responsive on the OLED.
When I try and use word_buffer at all the program won't work, unless I remove the aspects of the thermal printer (libraries and setup code).
Once again, this code works if I take out the code required for the thermal printer.
I've tried to look into this for a few days and found nothing for my specific question.
Please me know if you can help,
Here is my code:
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Adafruit_Thermal.h>
#include <PS2Keyboard.h>
#include <SPI.h>
#include <Wire.h>
#define LED_PIN 13
// OLED display
#define OLED_MOSI A4
#define OLED_CLK A3
#define OLED_DC A2
#define OLED_CS A0
#define OLED_RESET A1
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
// printer
#include "SoftwareSerial.h"
#define GND_PIN 7
Adafruit_Thermal printer(&Serial, 4);
// keyboard
const int DataPin = 8;
const int IRQpin = 3;
PS2Keyboard keyboard;
#define CHR_PER_LINE 21
#define HIDDEN_BUF 21
#define VIS_BUF 168
#define WRD_BUF 32
char line_buffer[HIDDEN_BUF + VIS_BUF + 1];
int line_start = HIDDEN_BUF;
int line_len = 0;
int word_len = 0;
char word_buffer[WRD_BUF + 1];
int g = 0;
int row_len = 0;
int neg = 0;
void setup() {
keyboard.begin(DataPin, IRQpin);
pinMode(GND_PIN, OUTPUT);
digitalWrite(GND_PIN, LOW);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
for (int i = 0; i < HIDDEN_BUF + VIS_BUF; i++)
line_buffer[i] = ' ';
line_buffer[HIDDEN_BUF + VIS_BUF] = 0;
for (int i = 0; i < WRD_BUF; i++)
word_buffer[i] = ' ';
word_buffer[WRD_BUF] = 0;
display.begin(SSD1306_SWITCHCAPVCC);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(line_buffer + HIDDEN_BUF);
display.display();
Serial.begin(9600);
printer.begin();
for (int i = 0; i < word_buffer; i++)
printer.print(word_buffer[i]);
printer.print('\n');
}
void loop() {
if (!keyboard.available()) return;
char c = keyboard.read();
boolean newline = false;
if (c == PS2_ENTER) {
newline = true;
for (int i = 0; i < 22 - row_len; i++)
line_buffer[line_start + line_len + i] = ' ';
line_len += 21 - row_len;
row_len = 0;
}
else if (c == PS2_DELETE) {
if (line_len > 0) {
line_len--;
line_buffer[line_start + line_len] = ' ';
row_len--;
if (word_len > 0)
word_len--;
}
}
if (c >= 33 && c <= 126) {
word_len++;
line_buffer[line_start + line_len] = c;
word_buffer[word_len] = c;
line_len++;
row_len++;
}
if (c == 32) {
line_buffer[line_start + line_len] = c;
line_len++;
for (int i = 0; i < 32; i++)
word_buffer[i] = ' ';
word_len = 0;
row_len++;
}
//word wrap
if (word_len > 21 - (row_len - word_len) && row_len > 0){
for (int i = row_len - word_len; i < 21; i++)
g++;
// for (int i = 0; i < word_len + 1; i++)
// line_buffer[line_start + line_len - word_len + g - 1 + i] = word_buffer[i];
for (int i = 0; i < g; i++)
line_buffer[line_start + line_len - word_len + i] = ' ';
row_len = word_len;
line_len += word_len - 1;
g = 0;
}
if (row_len == -1)
neg++;
if (word_len == 21)
word_len = 0;
if (neg > 0 && row_len == 0){
if (word_len > 21 - (22 - word_len)){
for (int i = 21 - word_len; i < 21; i++)
g++;
// for (int i = 0; i < word_len + 1; i++)
// line_buffer[line_start + line_len - word_len + g - 1 + i] = word_buffer[i];
for (int i = 0; i < g; i++)
line_buffer[line_start + line_len - word_len + i] = ' ';
row_len = word_len;
line_len += word_len - 0;
g = 0;
neg = 0;
}
}
// scroll up
if (line_start + line_len > HIDDEN_BUF + VIS_BUF - 5) {
for (int i = 0; i < HIDDEN_BUF + VIS_BUF - CHR_PER_LINE; i++)
line_buffer[i] = line_buffer[i + CHR_PER_LINE];
for (int i = HIDDEN_BUF + VIS_BUF - CHR_PER_LINE; i < HIDDEN_BUF + VIS_BUF; i++)
line_buffer[i] = ' ';
line_start -= CHR_PER_LINE;
}
display.clearDisplay();
display.setCursor(0, 0);
display.println(line_buffer + HIDDEN_BUF);
display.display();
}