I’m trying to turn my Parallel printer sketch into a library. This is my first effort in making a library, so I’m encountering a few problems.
My full sketch is here http://forum.arduino.cc/index.php?topic=205174.0, and it works fine as far as it goes. My first problem is in setting up the prototype and and getting it to work. In my header I have this prototype:
void printString(char toPrint[]);
void I2C_Thermal_Printer::printString(char toPrint[]){
int wStart = 0, //start of word in string
wEnd = 0; //end of word
int count = 0, //string position counter
wordLen = 0; //length of string
while (toPrint[count] != '\0'){
wStart = count;
wordLen = 0;
wEnd = count;
count ++;
wordLen++;
for (int n = wEnd; n > (wStart - 1); n--){
printChar(toPrint[n]);
}
}
But when I try to use the library, I cannot compile my ketch because I get the following error:
sketch_dec18a.ino:10: warning: deprecated conversion from string constant to 'char*'
Here is my sketch:
#include <I2C_Thermal_Printer_Test.h>
I2C_Thermal_Printer printer(10, 11, 0x20);
void setup(){
}
void loop(){
printer.printString("This is a test of my new Library!");
printer.lineFeed(10);
printer.doCut();
delay(20000);
}
My Header:
/* I2C_Thermal_Printer.h for controlling a Thermal Printer with a PFC8574
Jimmy Patrick, 12/18/2013
*/
#ifndef I2C_Thermal_Printer_h
#define I2C_Thermal_Printer_h
#include "Arduino.h"
class I2C_Thermal_Printer
{
public:
void I2C_Thermal_Printer(int strobePin, int busyPin, byte i2c_addr);
void waitForPrinter(int port);
void doStrobe();
void setData(char text);
void printChar(char toPrint);
void initializePrinter();
void alignCenter();
void alignRight();
void alignLeft();
void underline();
void thickUnderline();
void noUnderline();
void doCut();
void doBuzzer(int repeat);
void fontA();
void fontB();
void lineFeed(int lines);
void boldOn();
void boldOff();
void inverse();
void notInverse();
void printSize(byte fontsize);
void upsideDown();
void notUpsideDown();
void printString(char toPrint[]);
private:
int _busyPin;
int _strobePin;
byte _i2c_addr;
int _strobeWait;
};
#endif
and my library cpp file:
//I2C_Thermal_Printer.cpp
#include "Arduino.h"
#include "I2C_Thermal_Printer.h"
I2C_Thermal_Printer::I2C_Thermal_Printer(int strobePin, int busyPin, byte i2c_addr){
Wire.begin(i2c_addr);
pinMode(strobePin, OUTPUT);
pinMode(busyPin, INPUT);
digitalWrite(strobePin, HIGH);
initializePrinter();
_strobeWait = 10;
_busyPin = busyPin;
_strobePin = strobePin;
_i2c_addr = i2c_addr;
delay(500);
}
void I2C_Thermal_Printer::waitForPrinter(int port){
while(digitalRead(port) == 1){
// wait for busy to go low
}
}
void I2C_Thermal_Printer::doStrobe(){
digitalWrite(strobePin, LOW);
delayMicroseconds(_strobeWait);
digitalWrite(strobePin, HIGH);
}
void I2C_Thermal_Printer::setData(char text){
Wire.beginTransmission(_i2c_addr);
Wire.write(text);
Wire.endTransmission();
doStrobe();
}
void I2C_Thermal_Printer::printChar(char toPrint){
waitForPrinter(_busyPin);
setData(toPrint);
doStrobe();
}
void I2C_Thermal_Printer::initializePrinter(){
printChar((byte)0x1B);
printChar((byte)0x40);
}
void I2C_Thermal_Printer::alignCenter(){
printChar((byte)0x1B);
printChar((byte)0x61);
printChar(1);
}
void I2C_Thermal_Printer::alignRight(){
printChar((byte)0x1B);
printChar((byte)0x61);
printChar(2);
}
void alignLeft(){
printChar((byte)0x1B);
printChar((byte)0x61);
printChar(0);
}
void I2C_Thermal_Printer::underline(){
printChar((byte)0x1B);
printChar((byte)0x2D);
printChar(1);
}
void I2C_Thermal_Printer::thickUnderline(){
printChar((byte)0x1B);
printChar((byte)0x2D);
printChar(2);
}
void I2C_Thermal_Printer::noUnderline(){
printChar((byte)0x1B);
printChar((byte)0x2D);
printChar(0);
}
void I2C_Thermal_Printer::doCut(){
printChar((byte)0x1D);
printChar((byte)0x56);
printChar(49);
}
//Do the buzzer in increments of 200ms
void I2C_Thermal_Printer::doBuzzer(int repeat){
for (int i = 0; i < repeat; i++){
printChar((byte)0x1B);
printChar((byte)0x1E);
printChar(49);
}
}
//Select the font. Valid params are 0 or 1
void I2C_Thermal_Printer::fontA(){
printChar((byte)0x1B);
printChar((byte)0x4D);
printChar(0);
}
void I2C_Thermal_Printer::fontB(){
printChar((byte)0x1B);
printChar((byte)0x4D);
printChar(1);
}
//Line feed (the number of lines to feed)
void I2C_Thermal_Printer::lineFeed(int lines){
printChar((byte)0x1B);
printChar((byte)0x64);
printChar(lines);
}
void I2C_Thermal_Printer::boldOn(){
printChar((byte)0x1B);
printChar((byte)0x45);
printChar(1);
}
void I2C_Thermal_Printer::boldOff(){
printChar((byte)0x1B);
printChar((byte)0x45);
printChar(0);
}
void I2C_Thermal_Printer::inverse(){
printChar((byte)0x1D);
printChar((byte)0x42);
printChar(1);
}
void I2C_Thermal_Printer::notInverse(){
printChar((byte)0x1D);
printChar((byte)0x42);
printChar(0);
}
//I don't understand the font size description in the manual.
// fontsize 00 = standard, and the size changes based on this byte,
// MSB is Horizontal and LSB is Vertical
// 22 is larger, 33 is even larger. Needs work.
void I2C_Thermal_Printer::printSize(byte fontsize){
printChar((byte)0x1D);
printChar((byte)0x21);
printChar(fontsize);
}
void I2C_Thermal_Printer::upsideDown(){
printChar((byte)0x1B);
printChar((byte)0x7B);
printChar(1);
}
void I2C_Thermal_Printer::notUpsideDown(){
printChar((byte)0x1B);
printChar((byte)0x7B);
printChar(0);
}
void I2C_Thermal_Printer::printString(char toPrint[]){
int wStart = 0, //start of word in string
wEnd = 0; //end of word
int count = 0, //string position counter
wordLen = 0; //length of string
while (toPrint[count] != '\0'){
wStart = count;
wordLen = 0;
wEnd = count;
count ++;
wordLen++;
for (int n = wEnd; n > (wStart - 1); n--){
printChar(toPrint[n]);
}
}
}
Can anyone help me with the printString function?
Thanks
Jimmy