This library is for the control of NJE-105/106. (which is the digital signage device made by Nagano Japan Radio Co., Ltd.)

Dependents:   NJE10XCtrlSample TrainInfoSample

Files at this revision

API Documentation at this revision

Comitter:
rinosh2
Date:
Tue Nov 16 16:56:04 2010 +0000
Parent:
0:3aa62049d1de
Commit message:
Delete debug print at the message sending.

Changed in this revision

NJE10XCtrl.cpp Show annotated file Show diff for this revision Revisions of this file
NJE10XCtrl.h Show annotated file Show diff for this revision Revisions of this file
diff -r 3aa62049d1de -r fb1109a73ce9 NJE10XCtrl.cpp
--- a/NJE10XCtrl.cpp	Sun Nov 14 15:56:52 2010 +0000
+++ b/NJE10XCtrl.cpp	Tue Nov 16 16:56:04 2010 +0000
@@ -4,39 +4,39 @@
 // Local funcs
 
 static bool myputc(Serial& port, int ch){
-	while(!port.writeable());
-	return port.putc(ch) != EOF;
+    while(!port.writeable());
+    return port.putc(ch) != EOF;
 }
 
 static bool myputs(Serial& port, const char* str, size_t len){
-	while(len--){
-		if(!myputc(port, *str++)) return false;
-	}
-	return true;
+    while(len--){
+        if(!myputc(port, *str++)) return false;
+    }
+    return true;
 }
 
 static int myCheckMsg(const char*& msg, const char* buf, int size){
-	if(msg) return strlen(msg);
-	msg = buf;
-	return size;
+    if(msg) return strlen(msg);
+    msg = buf;
+    return size;
 }
 
 static char GetAttr1Char(NJE10XCtrl::Attr1 a1){
-	return (char(a1) & 3) + 'A';
-	
+    return (char(a1) & 3) + 'A';
+    
 }
 
 static char GetAttr2Char(NJE10XCtrl::Attr2 a2, NJE10XCtrl::Attr3 a3){
-	const char A2[4] = {'A', 'E', 'I', 'W'};
-	return A2[a2 & 3] + (a3 & 3);
+    const char A2[4] = {'A', 'E', 'I', 'W'};
+    return A2[a2 & 3] + (a3 & 3);
 }
-	
+    
 ////////////////////////////////////////////////////////////////////////////////
 // NJE10XCtrl class
 NJE10XCtrl::NJE10XCtrl(PinName tx_pin, PinName rx_pin) : m_port(tx_pin,  rx_pin), m_size(0) {
-	m_port.baud(9600);
-	m_port.format(8, Serial::None, 1);
-	m_buf[0] = 0;
+    m_port.baud(9600);
+    m_port.format(8, Serial::None, 1);
+    m_buf[0] = 0;
 }
 
 NJE10XCtrl::~NJE10XCtrl(){
@@ -46,167 +46,168 @@
 // NJE10XCtrl private funcs
 
 const char NJE_DATA_HEAD[10] = {
-	0x0d, 0x0a, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
+    0x0d, 0x0a, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
 };
 
 NJE10XCtrl::Status NJE10XCtrl::sendCmd(const char * str, int size){
-	
-	char buf[128];
-	memcpy(buf, str, size);
-	buf[size] = 0;
-	printf("Send '%s'\n", buf);
-	
-	if(	myputs(m_port, NJE_DATA_HEAD,	sizeof(NJE_DATA_HEAD)) &&
-		myputs(m_port, str,			size) &&
-		myputs(m_port, NJE_DATA_HEAD,	2)){
-			return S_OK;
-	}
-	return S_WRITE_FAILED;
+    /* for debug
+    char buf[128];
+    memcpy(buf, str, size);
+    buf[size] = 0;
+    printf("Send '%s'\n", buf);
+	*/
+    
+    if(    myputs(m_port, NJE_DATA_HEAD,    sizeof(NJE_DATA_HEAD)) &&
+        myputs(m_port, str,            size) &&
+        myputs(m_port, NJE_DATA_HEAD,    2)){
+            return S_OK;
+    }
+    return S_WRITE_FAILED;
 }
 
 NJE10XCtrl::Status NJE10XCtrl::sendCmd(const char * str){
-	return sendCmd(str, strlen(str));
+    return sendCmd(str, strlen(str));
 };
 
 
 // Append Str/Attr to the internal buffer ///////////////////////////////////////
 
 NJE10XCtrl::Status NJE10XCtrl::add(char ch){
-	// TODO: Escape command ( '~', "NJE...")
-	if(m_size >= MAX_DATA_BUF) return S_BUFFER_OVERFLOW;
-	m_buf[m_size++] = ch;
-	m_buf[m_size]   = 0;
-	return S_OK;
+    // TODO: Escape command ( '~', "NJE...")
+    if(m_size >= MAX_DATA_BUF) return S_BUFFER_OVERFLOW;
+    m_buf[m_size++] = ch;
+    m_buf[m_size]   = 0;
+    return S_OK;
 }
 
 NJE10XCtrl::Status NJE10XCtrl::add(const char* str){
-	while(*str) if(add(*str++)) return S_BUFFER_OVERFLOW;
-	return S_OK;
+    while(*str) if(add(*str++)) return S_BUFFER_OVERFLOW;
+    return S_OK;
 }
 
 NJE10XCtrl::Status NJE10XCtrl::addAttr(Attr1 a1, Attr2 a2, Attr3 a3){
-	if(	add('~') == S_OK &&
-		add(GetAttr1Char(a1)) == S_OK &&
-		add(GetAttr2Char(a2, a3)) == S_OK &&
-		add('~') == S_OK) return S_OK;
+    if(    add('~') == S_OK &&
+        add(GetAttr1Char(a1)) == S_OK &&
+        add(GetAttr2Char(a2, a3)) == S_OK &&
+        add('~') == S_OK) return S_OK;
 
-	return S_BUFFER_OVERFLOW; // faile
+    return S_BUFFER_OVERFLOW; // faile
 }
 NJE10XCtrl::Status NJE10XCtrl::clear(){
-	m_size = 0;
-	m_buf[0] = 0;
-	return S_OK;
+    m_size = 0;
+    m_buf[0] = 0;
+    return S_OK;
 }
 
 
 // Set/Del Normal message (01-99) //////////////////////////////////////////////
 NJE10XCtrl::Status NJE10XCtrl::setMessage(int id, const char* msg, Attr1 a1, Attr2 a2, Attr3 a3){
-	if(id < 1 || 99 < id) return S_INVALID_ID;
-	int len = myCheckMsg(msg, m_buf, m_size);
-	
-	const char CMD[]	= "]011A110%02d01010000%c%c%s";
-	const int  CMD_SIZE	= 8 + 2 + 8 + 2;
-	if(len > MAX_DATA_BUF - CMD_SIZE) return S_BUFFER_OVERFLOW;
+    if(id < 1 || 99 < id) return S_INVALID_ID;
+    int len = myCheckMsg(msg, m_buf, m_size);
+    
+    const char CMD[]    = "]011A110%02d01010000%c%c%s";
+    const int  CMD_SIZE    = 8 + 2 + 8 + 2;
+    if(len > MAX_DATA_BUF - CMD_SIZE) return S_BUFFER_OVERFLOW;
 
-	char buf[MAX_DATA_BUF];
-	char c1 = GetAttr1Char(a1);
-	char c2 = GetAttr2Char(a2, a3);
+    char buf[MAX_DATA_BUF];
+    char c1 = GetAttr1Char(a1);
+    char c2 = GetAttr2Char(a2, a3);
 
-	return sendCmd(buf, sprintf(buf, CMD, id, c1, c2, msg));
+    return sendCmd(buf, sprintf(buf, CMD, id, c1, c2, msg));
 }
 NJE10XCtrl::Status NJE10XCtrl::delMessage(int id){
-	if(id < 1 || 99 < id) return S_INVALID_ID;
+    if(id < 1 || 99 < id) return S_INVALID_ID;
 
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, "]011A210%02d", id));
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, "]011A210%02d", id));
 }
 
 NJE10XCtrl::Status NJE10XCtrl::setTitle(const char* msg){
-	int len = myCheckMsg(msg, m_buf, m_size);
-	
-	const char CMD[]	= "]011A11000%s";
-	const int  CMD_SIZE	= 8 + 2;
-	if(len > MAX_DATA_BUF - CMD_SIZE) return S_BUFFER_OVERFLOW;
+    int len = myCheckMsg(msg, m_buf, m_size);
+    
+    const char CMD[]    = "]011A11000%s";
+    const int  CMD_SIZE    = 8 + 2;
+    if(len > MAX_DATA_BUF - CMD_SIZE) return S_BUFFER_OVERFLOW;
 
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, CMD, msg));
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, CMD, msg));
 }
 NJE10XCtrl::Status NJE10XCtrl::delTitle(){
-	return sendCmd("]011A210");
+    return sendCmd("]011A210");
 }
 
 // Add/Clear free message //////////////////////////////////////////////////////
 NJE10XCtrl::Status NJE10XCtrl::addFreeMessage(const char* msg){
-	int len = myCheckMsg(msg, m_buf, m_size);
-	return sendCmd(msg, len);
+    int len = myCheckMsg(msg, m_buf, m_size);
+    return sendCmd(msg, len);
 }
 NJE10XCtrl::Status NJE10XCtrl::clearFreeMessage(){
-	return sendCmd("NJEM2");
+    return sendCmd("NJEM2");
 }
 
 NJE10XCtrl::Status NJE10XCtrl::addFreeCredit(const char* msg){
-	int len = myCheckMsg(msg, m_buf, m_size);
+    int len = myCheckMsg(msg, m_buf, m_size);
 
-	const char CMD[]	= "NJEC1%s";
-	const int  CMD_SIZE	= 5;
-	
-	if(len > MAX_DATA_BUF - CMD_SIZE) return S_BUFFER_OVERFLOW;
+    const char CMD[]    = "NJEC1%s";
+    const int  CMD_SIZE    = 5;
+    
+    if(len > MAX_DATA_BUF - CMD_SIZE) return S_BUFFER_OVERFLOW;
 
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, CMD, msg));
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, CMD, msg));
 }
 NJE10XCtrl::Status NJE10XCtrl::clearFreeCredit(){
-	return sendCmd("NJEC2");
+    return sendCmd("NJEC2");
 }
 
 // NJE control commands ////////////////////////////////////////////////////////
 
 NJE10XCtrl::Status NJE10XCtrl::reset(){
-	return sendCmd("NJER");
+    return sendCmd("NJER");
 }
 
 // NJE show status /////////////////////////////////////////////////////////////
 NJE10XCtrl::Status NJE10XCtrl::showTime(){
-	return sendCmd("NJET");
+    return sendCmd("NJET");
 }
 NJE10XCtrl::Status NJE10XCtrl::showContactCredit(){
-	return sendCmd("NJEV1");
+    return sendCmd("NJEV1");
 }
 NJE10XCtrl::Status NJE10XCtrl::showNewsCredit(){
-	return sendCmd("NJEV2");
+    return sendCmd("NJEV2");
 }
 NJE10XCtrl::Status NJE10XCtrl::showFreeCredit(){
-	return sendCmd("NJEV3");
+    return sendCmd("NJEV3");
 }
 
 // NJED commands ///////////////////////////////////////////////////////////////
 NJE10XCtrl::Status NJE10XCtrl::setOpMode(OpMode mode){
-	const char CMD[]	= "NJES02%02d";
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, CMD, mode));
+    const char CMD[]    = "NJES02%02d";
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, CMD, mode));
 }
 
 NJE10XCtrl::Status NJE10XCtrl::setFreeMode(FreeMode mode){
-	const char CMD[]	= "NJES05%02d";
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, CMD, mode));
+    const char CMD[]    = "NJES05%02d";
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, CMD, mode));
 }
 
 NJE10XCtrl::Status NJE10XCtrl::setScrollSpeed(ScrollSpeed speed){
-	const char CMD[]	= "NJES06%02d";
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, CMD, speed));
+    const char CMD[]    = "NJES06%02d";
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, CMD, speed));
 }
 
 NJE10XCtrl::Status NJE10XCtrl::setBlinkSpeed(BlinkSpeed speed){
-	const char CMD[]	= "NJES07%02d";
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, CMD, speed));
+    const char CMD[]    = "NJES07%02d";
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, CMD, speed));
 }
 
 NJE10XCtrl::Status NJE10XCtrl::setStopTime(int sec){
-	if(sec > 10) return S_INVALID_PARAM;
-	const char CMD[]	= "NJES08%02d";
-	char buf[MAX_DATA_BUF];
-	return sendCmd(buf, sprintf(buf, CMD, sec));
+    if(sec > 10) return S_INVALID_PARAM;
+    const char CMD[]    = "NJES08%02d";
+    char buf[MAX_DATA_BUF];
+    return sendCmd(buf, sprintf(buf, CMD, sec));
 }
diff -r 3aa62049d1de -r fb1109a73ce9 NJE10XCtrl.h
--- a/NJE10XCtrl.h	Sun Nov 14 15:56:52 2010 +0000
+++ b/NJE10XCtrl.h	Tue Nov 16 16:56:04 2010 +0000
@@ -1,118 +1,132 @@
+///////////////////////////////////////////////////////////////////////////////
+// NJE10XCtrl: NJE-105/106 control lib   by rinos 2010
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef __NJE10XCTRL_H__
+#define __NJE10XCTRL_H__
+
 #include "mbed.h"
 
 ////////////////////////////////////////////////////////////////////////////////
 // NJE10XCtrl Lib
 
 class NJE10XCtrl {
-	// defines /////////////////////////////////////////////////////////////////
+    // defines /////////////////////////////////////////////////////////////////
 public:
-	// Attributes
-	typedef enum {
-		ATTR_GREEN,
-		ATTR_RED,
-		ATTR_YELLOW,
-	} Attr1;
+    // Attributes
+    typedef enum {
+        ATTR_GREEN,
+        ATTR_RED,
+        ATTR_YELLOW,
+    } Attr1;
 
-	typedef enum {
-		ATTR_SCROLL,
-		ATTR_SCROLL_R,
-		ATTR_STOP,
-		ATTR_FIX,
-	} Attr2;
+    typedef enum {
+        ATTR_SCROLL,
+        ATTR_SCROLL_R,
+        ATTR_STOP,
+        ATTR_FIX,
+    } Attr2;
 
-	typedef enum {
-		ATTR_NORMAL,
-		ATTR_BLINK,
-		ATTR_REVERSE,
-		ATTR_BLINK_REVERSE,
-	} Attr3;
-	
-	// NJE command parameter
-	typedef enum {
-		OP_NORMAL,
-		OP_AUTO,
-		OP_POWERSAVE,
-	} OpMode;
+    typedef enum {
+        ATTR_NORMAL,
+        ATTR_BLINK,
+        ATTR_REVERSE,
+        ATTR_BLINK_REVERSE,
+    } Attr3;
+    
+    // NJE command parameter
+    typedef enum {
+        OP_NORMAL,
+        OP_AUTO,
+        OP_POWERSAVE,
+    } OpMode;
 
-	typedef enum {
-		FREEMODE_ON,
-		FREEMODE_OFF,
-	} FreeMode;
+    typedef enum {
+        FREEMODE_ON,
+        FREEMODE_OFF,
+    } FreeMode;
 
-	typedef enum {
-		SCROLL_SLOW,
-		SCROLL_MIDDLE,
-		SCROLL_FAST,
-	} ScrollSpeed;
+    typedef enum {
+        SCROLL_SLOW,
+        SCROLL_MIDDLE,
+        SCROLL_FAST,
+    } ScrollSpeed;
 
-	typedef enum {
-		BLINK_OFF,
-		BLINK_SLOW,
-		BLINK_MIDDLE,
-		BLINK_FAST,
-	} BlinkSpeed;
+    typedef enum {
+        BLINK_OFF,
+        BLINK_SLOW,
+        BLINK_MIDDLE,
+        BLINK_FAST,
+    } BlinkSpeed;
 
-	// error code
-	typedef enum {
-		S_OK,
-		S_WRITE_FAILED,
-		S_BUFFER_OVERFLOW,
-		S_INVALID_ID,
-		S_INVALID_PARAM,
-		
-		S_NOT_SUPPORT, // not yet...
-	} Status;
+    // error code
+    typedef enum {
+        S_OK,
+        S_WRITE_FAILED,
+        S_BUFFER_OVERFLOW,
+        S_INVALID_ID,
+        S_INVALID_PARAM,
+        
+        S_NOT_SUPPORT, // not yet...
+    } Status;
 
 private:
-	static const int MAX_DATA_BUF = 128;
+    static const int MAX_DATA_BUF = 128;
+
+    Serial    m_port;
+    char m_buf[MAX_DATA_BUF + 1];
+    int  m_size;
+    
 
-	Serial	m_port;
-	char m_buf[MAX_DATA_BUF + 1];
-	int  m_size;
-	
+    // internal funcs
+    Status sendCmd(const char* str);
+    Status sendCmd(const char* str, int len);
 
-	// internal funcs
-	Status sendCmd(const char* str);
-	Status sendCmd(const char* str, int len);
+	// Invalid method
+protected:
+	NJE10XCtrl(const NJE10XCtrl& v);
+	const NJE10XCtrl& operator =(const NJE10XCtrl& v);
 
 public:
-	NJE10XCtrl(PinName tx_pin = p9, PinName rx_pin = p10);
-	~NJE10XCtrl();
-	
-	// Append Str/Attr to the internal buffer //////////////////////////////////
-	Status add(char ch);				// append char to the internal buffer
-	Status add(const char* str);		// append string to the internal buffer
-	Status addAttr(Attr1 a1 = ATTR_GREEN, Attr2 a2 = ATTR_SCROLL, Attr3 a3 = ATTR_NORMAL);
-	Status clear();						// clear internal buffer
-	int size() const { return m_size; }
-	int left() const { return MAX_DATA_BUF - m_size; }
-	
-	// Set/Del Normal message (01-99) //////////////////////////////////////////
-	Status setMessage(int id, const char* msg = 0,		// use internal buffer if msg is NULL
-					Attr1 a1 = ATTR_GREEN, Attr2 a2 = ATTR_SCROLL, Attr3 a3 = ATTR_NORMAL);
-	Status delMessage(int id);							// del message (1 to 99)
-	Status setTitle(const char* msg = 0);				// use internal buffer if msg is NULL
-	Status delTitle();									// del message title
+    NJE10XCtrl(PinName tx_pin = p9, PinName rx_pin = p10);
+    ~NJE10XCtrl();
+    
+    // Append Str/Attr to the internal buffer //////////////////////////////////
+    Status add(char ch);                // append char to the internal buffer
+    Status add(const char* str);        // append string to the internal buffer
+    Status addAttr(Attr1 a1 = ATTR_GREEN, Attr2 a2 = ATTR_SCROLL, Attr3 a3 = ATTR_NORMAL);
+    Status clear();                        // clear internal buffer
+    int size() const { return m_size; }
+    int left() const { return MAX_DATA_BUF - m_size; }
+    
+    // Set/Del Normal message (01-99) //////////////////////////////////////////
+    Status setMessage(int id, const char* msg = 0,        // use internal buffer if msg is NULL
+                    Attr1 a1 = ATTR_GREEN, Attr2 a2 = ATTR_SCROLL, Attr3 a3 = ATTR_NORMAL);
+    Status delMessage(int id);                            // del message (1 to 99)
+    Status setTitle(const char* msg = 0);                // use internal buffer if msg is NULL
+    Status delTitle();                                    // del message title
 
-	// Add/Clear free message (Max 20) /////////////////////////////////////////
-	Status addFreeMessage(const char* msg = 0);	// use internal buffer if msg is NULL
-	Status clearFreeMessage();					// clear all free message
-	Status addFreeCredit(const char* msg = 0);	// use internal buffer if msg is NULL
-	Status clearFreeCredit();					// clear all free credit
+    // Add/Clear free message (Max 20) /////////////////////////////////////////
+    Status addFreeMessage(const char* msg = 0);    // use internal buffer if msg is NULL
+    Status clearFreeMessage();                    // clear all free message
+    Status addFreeCredit(const char* msg = 0);    // use internal buffer if msg is NULL
+    Status clearFreeCredit();                    // clear all free credit
 
-	// NJE control commands ////////////////////////////////////////////////////
-	Status reset();
-	
-	// NJE show status /////////////////////////////////////////////////////////
-	Status showTime();
-	Status showContactCredit();
-	Status showNewsCredit();
-	Status showFreeCredit();
+    // NJE control commands ////////////////////////////////////////////////////
+    Status reset();
+    
+    // NJE show status /////////////////////////////////////////////////////////
+    Status showTime();
+    Status showContactCredit();
+    Status showNewsCredit();
+    Status showFreeCredit();
 
-	// NJES commands ///////////////////////////////////////////////////////////
-	Status setOpMode(OpMode mode = OP_NORMAL);
-	Status setFreeMode(FreeMode mode = FREEMODE_ON);
-	Status setScrollSpeed(ScrollSpeed speed = SCROLL_MIDDLE);
-	Status setBlinkSpeed(BlinkSpeed speed = BLINK_FAST);
-	Status setStopTime(int sec = 5);
+    // NJES commands ///////////////////////////////////////////////////////////
+    Status setOpMode(OpMode mode = OP_NORMAL);
+    Status setFreeMode(FreeMode mode = FREEMODE_ON);
+    Status setScrollSpeed(ScrollSpeed speed = SCROLL_MIDDLE);
+    Status setBlinkSpeed(BlinkSpeed speed = BLINK_FAST);
+    Status setStopTime(int sec = 5);
 };
+
+#endif