Orignal AVR Tiny USI Based I2C slave Charlieplexing 7SEG LED Driver.
Revision 0:1c9f9768e990, committed 2013-12-11
- Comitter:
- bant62
- Date:
- Wed Dec 11 13:19:46 2013 +0000
- Child:
- 1:1ae1e21ba67f
- Commit message:
- first commit
Changed in this revision
My7SEG.cpp | Show annotated file Show diff for this revision Revisions of this file |
My7SEG.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/My7SEG.cpp Wed Dec 11 13:19:46 2013 +0000 @@ -0,0 +1,158 @@ +/** + ***************************************************************************** + * File Name : My7SEG.cpp + * + * Title : ORGINAL I2C LCD Display Claass Source File + * Revision : 0.1 + * Notes : + * Target Board : mbed NXP LPC1768, mbed LPC1114FN28 etc + * Tool Chain : ???? + * + * Revision History: + * When Who Description of change + * ----------- ----------- ----------------------- + * 2012/12/11 Hiroshi M init + ***************************************************************************** + * + * Copyright (C) 2013 Hiroshi M, MIT License + * + * 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. + * + **/ + +/* Includes ----------------------------------------------------------------- */ +#include "mbed.h" +#include "My7SEG.h" + +/* Private typedef ---------------------------------------------------------- */ +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private variables -------------------------------------------------------- */ +/* member fanctions --------------------------------------------------------- */ + +// Constractor +My7SEG::My7SEG(I2C *i2c): _i2c(i2c) +{ +} + +int My7SEG::writeBytes(const char *data, int length, bool repeated) +{ + wait_us(i2c_bit_wait_us); + _i2c->start(); + + wait_us(i2c_bit_wait_us); + if (_i2c->write(i2c_addr) != 1) + { + wait_us(i2c_bit_wait_us); + _i2c->stop(); + return _i2cFAILURE; + } + + for (int i = 0; i < length; i++) + { + wait_us(i2c_bit_wait_us); + if (_i2c->write(data[i]) != 1) + { + wait_us(i2c_bit_wait_us); + _i2c->stop(); + return _i2cFAILURE; + } + } + if (!repeated) + { + wait_us(i2c_bit_wait_us); + _i2c->stop(); + } + return _i2cSUCCESS; +} + +int My7SEG::clear(void) +{ + char buff[2] = { ESC, 'C' }; + + return writeBytes(buff, sizeof(buff)); +} + +int My7SEG::shift_left(void) +{ + char buff[2] = { ESC, '<' }; + + return writeBytes(buff, sizeof(buff)); +} + +int My7SEG::shift_right(void) +{ + char buff[2] = { ESC, '>' }; + + return writeBytes(buff, sizeof(buff)); +} + +int My7SEG::rotato_left(void) +{ + char buff[2] = { ESC, '[' }; + + return writeBytes(buff, sizeof(buff)); +} + +int My7SEG::rotato_right(void) +{ + char buff[2] = { ESC, ']' }; + + return writeBytes(buff, sizeof(buff)); +} + +// 一文字表示 +int My7SEG::printChar(char c) +{ + return writeBytes(&c, 1); +} + +int My7SEG::printPosChar(int pos, char chr) +{ + char buff[4]; + + if( pos < 0 && 7 < pos ) + { + return _i2cFAILURE; + } + + buff[0] = ESC; + buff[1] = 'L'; + buff[2] = '0' + pos; + buff[3] = chr; + + return writeBytes(buff, sizeof(buff)); +} + +int My7SEG::printStr(const char *s) +{ + int ret; + char c; + + ret = _i2cSUCCESS; + while ((c = *s++)!=NULL) + { + ret = writeBytes(&c, 1); + if (ret == _i2cFAILURE) + { + return ret; + } + } + + return ret; +} + +///////////////////////////////////////////////
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/My7SEG.h Wed Dec 11 13:19:46 2013 +0000 @@ -0,0 +1,79 @@ +/** + ***************************************************************************** + * File Name : My7SEG.h + * + * Title : ORGINAL I2C LCD Display Claass Header File + * Revision : 0.1 + * Notes : + * Target Board : mbed NXP LPC1768, mbed LPC1114FN28 etc + * Tool Chain : ???? + * + * Revision History: + * When Who Description of change + * ----------- ----------- ----------------------- + * 2012/12/06 Hiroshi M init + ***************************************************************************** + * + * Copyright (C) 2013 Hiroshi M, MIT License + * + * 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. + * + **/ + +#ifndef __My7SEG_H_ +#define __My7SEG_H_ + +/* Includes ----------------------------------------------------------------- */ +#include "mbed.h" +/* typedef ------------------------------------------------------------------ */ + +/* define ------------------------------------------------------------------- */ +#define ESC 0x1B +#define I2C_ADDR_MY7SEG 0x33 +#define _i2cSUCCESS 0 +#define _i2cFAILURE 1 + +/* macro -------------------------------------------------------------------- */ +/* variables ---------------------------------------------------------------- */ +/* class -------------------------------------------------------------------- */ +class My7SEG +{ +private: + static const int i2c_addr = I2C_ADDR_MY7SEG << 1; + static const int i2c_bit_wait_us = 20; + static const int i2c_command_wait_ms = 4; + + I2C *_i2c; + int writeBytes(const char *data, int length, bool repeated=false); + +protected: + + +public: + My7SEG(I2C *i2c); + + int clear(void); + int shift_left(void); + int shift_right(void); + int rotato_left(void); + int rotato_right(void); + int printChar(char c); + int printPosChar(int pos, char chr); + int printStr(const char *s); + +}; + +#endif /* __My7SEG_H_ */