Embedded system Fun Blog
























































Find out all the best information, libraries and circuit about the latest Embedded systems.

Saturday 7 January 2012

MBED Example: How to use Smart Label Printers SLP440 libraries

.from: https://github.com/leroilion/mbed/blob/master/SLP440/trunk/SLP440.cpp

XxXxXxXxXxXxXxXxXxXxXxXx SLP440.cpp XxXxXxXxXxXxXxXxXxXxXxXx


#include "SLP440.h"
#include "string.h"

SLP440::SLP440(int tx, int rx) : m_printer(tx,rx), bufferfull(false)
{
 m_printer.baud(115200);
}

void SLP440::WaitForEmptyBuffer()
{
 if(m_printer.readable() && m_printer.getc() == XOFF)
 {
  bufferfull = true;
 }
 while(bufferfull)
 {
  bufferfull = m_printer.getc() == XON;
 } 
}

int SLP440::GetStatus()
{
 m_printer.putc(CMD_STATUS);
 return m_printer.getc();
}

int SLP440::GetFirmwareVersion()
{
 m_printer.putc(CMD_VERSION);
 return m_printer.getc()-0x80;
}
void SLP440::SetBaudrate(int baud)
{
 int key = 5;
 switch(baud)
 {
 case 9600:
  key = 0;
  break;
 case 19200:
  key = 1;
  break;
 case 38400:
  key = 2;
  break;
 case 57600:
  key = 3;
  break;
 case 115200:
  key = 4;
  break;
 }
 if(key == 5) return;
 else
 {
  m_printer.putc(CMD_BAUDRATE);
  m_printer.putc(key);
  m_printer.baud(baud);
 }
}

void SLP440::Print(char count, char *data)
{
 WaitForEmptyBuffer();
 m_printer.putc(CMD_PRINT);
 WaitForEmptyBuffer();
 count = count > MAXWIDTH/8 ? MAXWIDTH/8 : count;
 m_printer.putc(count);
 for(int i=0; i<count; i++)
 {
  WaitForEmptyBuffer();
  m_printer.putc(data[i]);
 }
}


void SLP440::PrintRLE(int count, char *data)
{

}

void SLP440::SetMargin(int margin)
{
 WaitForEmptyBuffer();
}

void SLP440::RepeatPrint()
{
 WaitForEmptyBuffer();
 m_printer.putc(CMD_REPEAT);
}
 
void SLP440::Tab(int pixels)
{
 WaitForEmptyBuffer();
 m_printer.putc(CMD_TAB);
 WaitForEmptyBuffer();
 m_printer.putc(pixels);
}

void SLP440::LineFeed()
{
 WaitForEmptyBuffer();
 m_printer.putc(CMD_LINEFEED);
}

void SLP440::VTab(int pixels)
{
 WaitForEmptyBuffer();
 m_printer.putc(CMD_VERTTAB);
 WaitForEmptyBuffer();
 m_printer.putc(pixels);
}

 
void SLP440::FeedLabel()
{
 m_printer.putc(CMD_FORMFEED);
}

void SLP440::SetSpeed(int speed)
{
 m_printer.putc(CMD_SETSPEED);
 m_printer.putc(speed);
}

void SLP440::SetDensity(int density)
{
 m_printer.putc(CMD_DENSITY);
 m_printer.putc(density);
}

void SLP440::Reset()
{
 m_printer.putc(CMD_RESET);
}
 
void SLP440::ReverseFeed(int pixels)
{
 WaitForEmptyBuffer();
 m_printer.putc(CMD_REVFEED);
 WaitForEmptyBuffer();
 m_printer.putc(pixels);
}
 
void SLP440::Checkpoint()
{

}

int SLP440::GetModelNum()
{
 
}
 
void SLP440::SetIndent(int pixels)
{

}

void SLP440::SetFineMode(bool finemode)
{
 WaitForEmptyBuffer();
 m_printer.putc(CMD_FINEMODE);
 WaitForEmptyBuffer();
 m_printer.putc(finemode);
}
 
void SLP440::SetXOFFThreshold(int threshold)
{
 m_printer.putc(CMD_XOFF_THRESH);
 m_printer.putc(threshold);

}

void SLP440::SetXONThreshold(int threshold)
{
 m_printer.putc(CMD_XON_THRESH);
 m_printer.putc(threshold);
}
 
void SLP440::EnableDiagnostics()
{

}

void SLP440::SetSerialNo(char* serial)
{

}
 
void SLP440::SetOptions(int options)
{

}

int SLP440::GetOptions()
{

}
 
void SLP440::SetMode(int mode)
{

}

int SLP440::GetMode()
{

}

 
void SLP440::SetLabelLength()
{

}

int SLP440::CheckBaudRate()
{

}

XxXxXxXxXxXxXxXxXxXxXxXx SLP440.h XxXxXxXxXxXxXxXxXxXxXxXx


#include "mbed.h"

/*
 Printer command definitions
*/
#define CMD_NOP    0x00 // No operation
#define CMD_STATUS   0x01 // Request printer status 
#define CMD_VERSION   0x02 // Request firmware version 
#define CMD_BAUDRATE  0x03 // Change baud rate to nn 
#define CMD_PRINT   0x04 // Print literal (binary) data 
#define CMD_PRINTRLE  0x05 // Print compressed (RLE) data 
#define CMD_MARGIN   0x06 // Image offset (in mm) from left side 
#define CMD_REPEAT   0x07 // Repeat last print command 
#define CMD_TAB    0x09 // Tab nn dots to the right 
#define CMD_LINEFEED  0x0A // Feed label one line (one dot) 
#define CMD_VERTTAB   0x0B // Feed label nn lines (dots) 
#define CMD_FORMFEED  0x0C // Feed to top of next label 
#define CMD_SETSPEED  0x0D // Set print speed mode 
#define CMD_DENSITY   0x0E // Set print density to nn 
#define CMD_RESET   0x0F // Reset printer 
#define CMD_CHECKPOINT  0x10 // Send checkpoint response 
#define CMD_REVFEED   0x11 // Feed paper backward nn lines (dots) 
#define CMD_MODEL   0x12 // Request printer model number 
#define CMD_INDENT   0x16 // Set image offset (in dots) from left side 
#define CMD_FINEMODE  0x17 // Set anti-banding mode on/off 
#define CMD_XOFF_THRESH  0x18 // Set XOFF threshold 
#define CMD_XON_THRESH  0x19 // Set XON threshold 
#define CMD_DIAGNOSTIC  0x1A // Enable diagnostic mode. Followed by 0x01
#define CMD_SETSERIALNUM 0x1B // Set serial number 
#define CMD_SETOPTIONS  0x1C // Change option settings 
#define CMD_GETOPTIONS  0x1D // Request option settings 
#define CMD_SETMODE   0x1E // Set printer mode (nn = 0, 1, or 2) 
#define CMD_GETMODE   0x1E // Get printer mode. Then send 0xFF
#define CMD_LENGTH   0x1F // Set label length to nn millimeters 
#define CMD_CHECK   0xA5 // Check for correct baud rate 


#define STAT_PAPER_OUT  0x01
#define STAT_PAPER_JAM  0x02
#define STAT_HARD_ERR  0x04
#define STAT_COMM_ERR  0x08
#define STAT_IDLE   0x10
#define STAT_PLATEN_OPEN 0x20

#define STAT_ERROR   0x2F

#define XON     0x11
#define XOFF    0x13

#define MAXWIDTH   566
#define MAXHEIGHT   1098

/* Class: SLP440
* A class to use an SLP440 label printer.
*/

class SLP440
{
private:
 /*Member: m_printer
 * Contains the serial port for the printer.*/
 Serial m_printer;
 
 /*Member: bufferfull
 * Whether the printer's buffer is full.*/
 bool bufferfull;
 
 /*Function: WaitForEmptyBuffer
 * Waits for the printer to accept data. Only needed for buffered commands. */ 
 void WaitForEmptyBuffer();
 
public:
 /*Constructor: SLP440
 * Creates an instance of the SLP440 class.
 *
 * Parameters:
 *  tx - the tx pin on the mbed
 *  rx - the rx pin on the mbed
 */
 SLP440(int tx, int rx);


 int GetStatus();
 char *GetStatusMsg();
 int GetFirmwareVersion();
 void SetBaudrate(int baud);

 /*
 * Functions: Printer commands
 *
 *  Print - Print literal (binary) data
 *  PrintRLE - Print compressed (RLE) data 
 *  SetMargin - Set offset (in mm) from left side
 *  RepeatPrint - Repeat last print command
 *
 *  Tab - Tab a number of dots to the right
 *  LineFeed - Feed label one dot
 *  VTab - Feed label a number of dots
 *
 *  FeedLabel - feed to top of next label
 *  SetSpeed - Set printer speed mode
 *  SetDensity - Set printer density
 *
 *  Reset - Reset printer
 *
 *  ReverseFeed - Feed paper backwards a number of dots
 *
 *  Checkpoint - send checkpoint response
 *  GetModelNum - Request printer model number
 *
 *  SetIndent - Set image offset in dots from left side
 *  SetFineMode - Set anti-banding mode on/off
 *
 *  SetXOFFThreshold - Set XOFF threshold
 *  SetXONThreshold - Set XON threshold
 *
 *  EnableDiagnostics - Enable diagnostic mode
 *  SetSerialNo - Set serial number
 *
 *  SetOptions - Change option settings
 *  GetOptions - Request option settings
 *
 *  SetMode - Set printer mode
 *  GetMode - Get printer mode
 *
 *  SetLabelLength - Set the label length in millimeters
 *  CheckBaudRate - Check for correct baud rate
 */
 
 void Print(char count, char *data);
 void PrintRLE(int count, char *data); //Not implemented
 void SetMargin(int margin);
 void RepeatPrint();
 
 void Tab(int pixels);
 void LineFeed();
 void VTab(int pixels);
 
 void FeedLabel();
 void SetSpeed(int speed);
 void SetDensity(int density);
 
 void Reset();
 
 void ReverseFeed(int pixels);
 
 void Checkpoint();    //Not implemented
 int GetModelNum();    //Not implemented
 
 void SetIndent(int pixels);   //Not implemented
 void SetFineMode(bool finemode);
 
 void SetXOFFThreshold(int threshold);
 void SetXONThreshold(int threshold);
 
 void EnableDiagnostics();   //Not implemented
 void SetSerialNo(char* serial);   //Not implemented
 
 void SetOptions(int options);   //Not implemented
 int GetOptions();    //Not implemented
 
 void SetMode(int mode);    //Not implemented
 int GetMode();     //Not implemented

 void SetLabelLength();    //Not implemented
 int CheckBaudRate();    //Not implemented
};

XxXxXxXxXxXxXxXxXxXxXxXx EOF XxXxXxXxXxXxXxXxXxXxXxXx

No comments:

Post a Comment