0

I am creating a program to display text on an OLED display and then have it sent to a thermal printer.

The issue is my second char string: word_buffer

When I try to use word_buffer for any reason it crashes the program and nothing will work (the OLED display will not refresh upon uploading the sketch and typing on the keyboard will not update the display).

The program works fine when you omit the code needed for the thermal printer (Adafruit_Thermal.h, SoftwareSerial.h, and other setup).

The sample code is below, I've commented out the section where I try and utilize word_buffer. If I remove the thermal printer setup code and uncomment that section, the program will run just fine.

Let me know what you think, I've been stuck on this for a while now :/

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Adafruit_Thermal.h>
#include <PS2Keyboard.h>
#include <MemoryFree.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 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];


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();
}

void loop() {

  if (!keyboard.available()) return;

  char c = keyboard.read();         

  boolean newline = false;          

 if (c == PS2_DELETE) {      
    if (line_len > 0) {              
      line_len--;                   
      line_buffer[line_start + line_len] = ' '; 
      if (word_len > 0) 
      word_len--;  
    }
  }

//  if (word_len > 0)
//  line_buffer[188] = word_buffer[1];

  if (c >= 33 && c <= 126) {                 
    word_len++;
    line_buffer[line_start + line_len] = c;   
    word_buffer[word_len] = c;
    line_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; 
  }

  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(line_buffer + HIDDEN_BUF); 
  display.display();
}
  • char word_buffer[WRD_BUF + 1] = {' '}; will init the array with spaces – Juraj Mar 4 at 5:38
  • Thanks Juraj, that makes sense and would save some code but I'm still not sure how to get the code to run with word_buffer – no1so Mar 4 at 6:22
  • My guess is you are running out of memory and can't allocate the oled display buffer. – Majenko Mar 4 at 9:32
  • Thanks @Majenko do you have any suggestions? Note that using multiple char arrays works fine without the thermal printer code, so I'm not sure why that affects it so much? – no1so Mar 4 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.