/* Copyright (C) 2011 by Jack W. Kern Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include // for malloc and free #include void* operator new( size_t size ) { return malloc(size); } void operator delete( void* ptr ) { free(ptr); } // ----------------------------------------------------------------- // static char* printb( char* pBuff, char* pString ) { // Copy string while( pString && *pString != 0 ) *(pBuff++) = *(pString++); *(pBuff) = 0; // Null term return pBuff; } // ----------------------------------------------------------------- // static char* printb( char* pBuff, int nDec ) { int nCount = 0; // Calculate the decimal as string if( nDec > 0 ) { for( ; nCount < 7 && nDec != 0; ++nCount, nDec /= 10 ) pBuff[nCount] = '0' + nDec % 10; } else if( nDec < 0 ) { pBuff[nCount++] = '-'; nDec = 0 - nDec; for( ; nCount < 7 && nDec != 0; ++nCount, nDec /= 10 ) pBuff[nCount] = '0' + nDec % 10; } else pBuff[nCount++] = '0'; pBuff[nCount] = 0; // Null term // Print the decimal string return pBuff; } // ----------------------------------------------------------------- // static char* printbhex( char* pBuff, unsigned char nByte ) { const static char nHexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; char* pBuffRef = pBuff; *( pBuffRef++ ) = nHexDigits[ nByte >> 4 ]; *( pBuffRef++ ) = nHexDigits[ nByte & 0x0F ]; *pBuffRef = 0; // Null term return pBuff; } // ----------------------------------------------------------------- // void LCD117::initDisplaySize( int nCharW, int nCharH ) { // Clap the parameters to supported values int nW = min( 20, max( 0, nCharW ) ); int nH = min( 4, max( 0, nCharH ) ); char* pOut = szBuffer; pOut = printb( pOut, "?G" ); pOut = printb( pOut, nH ); pOut = printb( pOut, nW ); mySerial->print( "?G420" ); // set display geometry, 4 x 20 characters in this case delay(500); // pause to allow LCD EEPROM to program } // ----------------------------------------------------------------- // LCD117::LCD117( int nCharW, int nCharH ) : nCharWidth ( nCharW ) , nCharHeight ( nCharH ) { } // ----------------------------------------------------------------- // LCD117::~LCD117() { delete mySerial; } // ----------------------------------------------------------------- // void LCD117::begin( int txPin, int rxPin, int nBaud ) { mySerial = new NewSoftSerial( rxPin, txPin ); initDisplaySize( nCharWidth, nCharHeight ); pinMode( txPin, OUTPUT ); mySerial->begin( nBaud ); // 9600 baud is chip comm speed } // ----------------------------------------------------------------- // void LCD117::clearLCD ( void ) { mySerial->print( "?f" ); delay( 10 ); } void LCD117::showCursor ( void ) { mySerial->print( "?c1" ); delay( 300 ); } void LCD117::hideCursor ( void ) { mySerial->print( "?c0" ); delay( 300 ); } void LCD117::setCursor ( int x, int y ) { nCx = x; nCy = y; } void LCD117::printCustom ( int nIndex ) { char* pOut = szBuffer; pOut = printb( pOut, "?" ); pOut = printb( pOut, nIndex ); printCursor(); mySerial->print( szBuffer ); ++nCx; wrapCursor(); delay(10); } void LCD117::printCursor() { mySerial->print( "?y" ); mySerial->print( nCy, DEC ); if( nCx < 10 ) { mySerial->print( "?x0" ); mySerial->print( nCx, DEC ); } else { mySerial->print( "?x" ); mySerial->print( nCx, DEC ); } } // ----------------------------------------------------------------- // void LCD117::defineCharacter( int nIndex, const char* pBitArray ) { char newChar[8] = {0}; int nByte = 0; int nBit = 4; for( int i=0; i<40 && nByte < 8; ++i ) { newChar[nByte] |= pBitArray[i] ? 1 << nBit: 0; if( !nBit ) { nBit = 4; ++nByte; } else --nBit; } char* pOut = szBuffer; pOut = printb ( pOut, "?D" ); pOut = printb ( pOut, nIndex ); pOut = printbhex ( pOut, newChar[0] ); pOut = printbhex ( pOut, newChar[1] ); pOut = printbhex ( pOut, newChar[2] ); pOut = printbhex ( pOut, newChar[3] ); pOut = printbhex ( pOut, newChar[4] ); pOut = printbhex ( pOut, newChar[5] ); pOut = printbhex ( pOut, newChar[6] ); pOut = printbhex ( pOut, newChar[7] ); mySerial->print( szBuffer ); delay(300); // Delay for EEPROM write time } // ----------------------------------------------------------------- // void LCD117::updateCursor( const char* pszText ) { int nTextLen = 0; while( *(pszText++) > 0 ) ++nTextLen; nCy += nTextLen / 20; nCx += nTextLen % 20; } // ----------------------------------------------------------------- // void LCD117::wrapCursor() { if( nCx >= nCharWidth ) ++nCy; nCx %= nCharWidth; nCy %= nCharHeight; } void LCD117::print( int nValue, int nFormat ) { char buffer[16]; if ( nFormat == DECIMAL ) print( printb ( buffer, nValue ) ); else if ( nFormat == HEX ) print( printbhex ( buffer, nValue ) ); } // ----------------------------------------------------------------- // void LCD117::print( const char* pszText ) { printCursor(); mySerial->print( pszText ); updateCursor( pszText ); wrapCursor(); delay(10); } void LCD117::println( int nValue, int nFormat ) { } // ----------------------------------------------------------------- // void LCD117::println( const char* pszText ) { printCursor(); mySerial->print( pszText ); updateCursor( pszText ); wrapCursor(); // Finish the line with spaces char* pOut = szBuffer; int nSpaces = nCharWidth - nCx; while( nSpaces-- > 0 ) pOut = printb( pOut, " " ); mySerial->print( szBuffer ); nCx = 0; ++nCy; wrapCursor(); delay(10); }