Orignal AVR Tiny USI Based I2C slave Charlieplexing 7SEG LED Driver.

Dependents:   My7SEG_test

Committer:
bant62
Date:
Thu Dec 12 00:35:39 2013 +0000
Revision:
2:e7362bcb9e1f
Parent:
1:1ae1e21ba67f
Fixed the software description;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bant62 0:1c9f9768e990 1 /**
bant62 0:1c9f9768e990 2 *****************************************************************************
bant62 0:1c9f9768e990 3 * File Name : My7SEG.cpp
bant62 0:1c9f9768e990 4 *
bant62 0:1c9f9768e990 5 * Title : ORGINAL I2C LCD Display Claass Source File
bant62 0:1c9f9768e990 6 * Revision : 0.1
bant62 0:1c9f9768e990 7 * Notes :
bant62 0:1c9f9768e990 8 * Target Board : mbed NXP LPC1768, mbed LPC1114FN28 etc
bant62 0:1c9f9768e990 9 * Tool Chain : ????
bant62 0:1c9f9768e990 10 *
bant62 0:1c9f9768e990 11 * Revision History:
bant62 0:1c9f9768e990 12 * When Who Description of change
bant62 0:1c9f9768e990 13 * ----------- ----------- -----------------------
bant62 2:e7362bcb9e1f 14 * 2013/12/11 Hiroshi M init
bant62 0:1c9f9768e990 15 *****************************************************************************
bant62 0:1c9f9768e990 16 *
bant62 0:1c9f9768e990 17 * Copyright (C) 2013 Hiroshi M, MIT License
bant62 0:1c9f9768e990 18 *
bant62 0:1c9f9768e990 19 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
bant62 0:1c9f9768e990 20 * and associated documentation files (the "Software"), to deal in the Software without restriction,
bant62 0:1c9f9768e990 21 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
bant62 0:1c9f9768e990 22 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
bant62 0:1c9f9768e990 23 * furnished to do so, subject to the following conditions:
bant62 0:1c9f9768e990 24 *
bant62 0:1c9f9768e990 25 * The above copyright notice and this permission notice shall be included in all copies or
bant62 0:1c9f9768e990 26 * substantial portions of the Software.
bant62 0:1c9f9768e990 27 *
bant62 0:1c9f9768e990 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
bant62 0:1c9f9768e990 29 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
bant62 0:1c9f9768e990 30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
bant62 0:1c9f9768e990 31 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bant62 0:1c9f9768e990 32 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
bant62 0:1c9f9768e990 33 *
bant62 0:1c9f9768e990 34 **/
bant62 0:1c9f9768e990 35
bant62 0:1c9f9768e990 36 /* Includes ----------------------------------------------------------------- */
bant62 0:1c9f9768e990 37 #include "mbed.h"
bant62 0:1c9f9768e990 38 #include "My7SEG.h"
bant62 0:1c9f9768e990 39
bant62 0:1c9f9768e990 40 /* Private typedef ---------------------------------------------------------- */
bant62 0:1c9f9768e990 41 /* Private define ----------------------------------------------------------- */
bant62 0:1c9f9768e990 42 /* Private macro ------------------------------------------------------------ */
bant62 0:1c9f9768e990 43 /* Private variables -------------------------------------------------------- */
bant62 0:1c9f9768e990 44 /* member fanctions --------------------------------------------------------- */
bant62 0:1c9f9768e990 45
bant62 0:1c9f9768e990 46 // Constractor
bant62 0:1c9f9768e990 47 My7SEG::My7SEG(I2C *i2c): _i2c(i2c)
bant62 0:1c9f9768e990 48 {
bant62 0:1c9f9768e990 49 }
bant62 0:1c9f9768e990 50
bant62 0:1c9f9768e990 51 int My7SEG::writeBytes(const char *data, int length, bool repeated)
bant62 0:1c9f9768e990 52 {
bant62 0:1c9f9768e990 53 wait_us(i2c_bit_wait_us);
bant62 0:1c9f9768e990 54 _i2c->start();
bant62 0:1c9f9768e990 55
bant62 0:1c9f9768e990 56 wait_us(i2c_bit_wait_us);
bant62 0:1c9f9768e990 57 if (_i2c->write(i2c_addr) != 1)
bant62 0:1c9f9768e990 58 {
bant62 0:1c9f9768e990 59 wait_us(i2c_bit_wait_us);
bant62 0:1c9f9768e990 60 _i2c->stop();
bant62 0:1c9f9768e990 61 return _i2cFAILURE;
bant62 0:1c9f9768e990 62 }
bant62 0:1c9f9768e990 63
bant62 0:1c9f9768e990 64 for (int i = 0; i < length; i++)
bant62 0:1c9f9768e990 65 {
bant62 0:1c9f9768e990 66 wait_us(i2c_bit_wait_us);
bant62 0:1c9f9768e990 67 if (_i2c->write(data[i]) != 1)
bant62 0:1c9f9768e990 68 {
bant62 0:1c9f9768e990 69 wait_us(i2c_bit_wait_us);
bant62 0:1c9f9768e990 70 _i2c->stop();
bant62 0:1c9f9768e990 71 return _i2cFAILURE;
bant62 0:1c9f9768e990 72 }
bant62 0:1c9f9768e990 73 }
bant62 0:1c9f9768e990 74 if (!repeated)
bant62 0:1c9f9768e990 75 {
bant62 0:1c9f9768e990 76 wait_us(i2c_bit_wait_us);
bant62 0:1c9f9768e990 77 _i2c->stop();
bant62 0:1c9f9768e990 78 }
bant62 0:1c9f9768e990 79 return _i2cSUCCESS;
bant62 0:1c9f9768e990 80 }
bant62 0:1c9f9768e990 81
bant62 0:1c9f9768e990 82 int My7SEG::clear(void)
bant62 0:1c9f9768e990 83 {
bant62 0:1c9f9768e990 84 char buff[2] = { ESC, 'C' };
bant62 0:1c9f9768e990 85
bant62 0:1c9f9768e990 86 return writeBytes(buff, sizeof(buff));
bant62 0:1c9f9768e990 87 }
bant62 0:1c9f9768e990 88
bant62 0:1c9f9768e990 89 int My7SEG::shift_left(void)
bant62 0:1c9f9768e990 90 {
bant62 0:1c9f9768e990 91 char buff[2] = { ESC, '<' };
bant62 0:1c9f9768e990 92
bant62 0:1c9f9768e990 93 return writeBytes(buff, sizeof(buff));
bant62 0:1c9f9768e990 94 }
bant62 0:1c9f9768e990 95
bant62 0:1c9f9768e990 96 int My7SEG::shift_right(void)
bant62 0:1c9f9768e990 97 {
bant62 0:1c9f9768e990 98 char buff[2] = { ESC, '>' };
bant62 0:1c9f9768e990 99
bant62 0:1c9f9768e990 100 return writeBytes(buff, sizeof(buff));
bant62 0:1c9f9768e990 101 }
bant62 0:1c9f9768e990 102
bant62 0:1c9f9768e990 103 int My7SEG::rotato_left(void)
bant62 0:1c9f9768e990 104 {
bant62 0:1c9f9768e990 105 char buff[2] = { ESC, '[' };
bant62 0:1c9f9768e990 106
bant62 0:1c9f9768e990 107 return writeBytes(buff, sizeof(buff));
bant62 0:1c9f9768e990 108 }
bant62 0:1c9f9768e990 109
bant62 0:1c9f9768e990 110 int My7SEG::rotato_right(void)
bant62 0:1c9f9768e990 111 {
bant62 0:1c9f9768e990 112 char buff[2] = { ESC, ']' };
bant62 0:1c9f9768e990 113
bant62 0:1c9f9768e990 114 return writeBytes(buff, sizeof(buff));
bant62 0:1c9f9768e990 115 }
bant62 0:1c9f9768e990 116
bant62 0:1c9f9768e990 117 // 一文字表示
bant62 0:1c9f9768e990 118 int My7SEG::printChar(char c)
bant62 0:1c9f9768e990 119 {
bant62 0:1c9f9768e990 120 return writeBytes(&c, 1);
bant62 0:1c9f9768e990 121 }
bant62 0:1c9f9768e990 122
bant62 0:1c9f9768e990 123 int My7SEG::printPosChar(int pos, char chr)
bant62 0:1c9f9768e990 124 {
bant62 0:1c9f9768e990 125 char buff[4];
bant62 0:1c9f9768e990 126
bant62 1:1ae1e21ba67f 127 if( pos < 0 && 7 < pos )
bant62 0:1c9f9768e990 128 {
bant62 0:1c9f9768e990 129 return _i2cFAILURE;
bant62 0:1c9f9768e990 130 }
bant62 0:1c9f9768e990 131
bant62 0:1c9f9768e990 132 buff[0] = ESC;
bant62 0:1c9f9768e990 133 buff[1] = 'L';
bant62 0:1c9f9768e990 134 buff[2] = '0' + pos;
bant62 0:1c9f9768e990 135 buff[3] = chr;
bant62 0:1c9f9768e990 136
bant62 0:1c9f9768e990 137 return writeBytes(buff, sizeof(buff));
bant62 0:1c9f9768e990 138 }
bant62 0:1c9f9768e990 139
bant62 0:1c9f9768e990 140 int My7SEG::printStr(const char *s)
bant62 0:1c9f9768e990 141 {
bant62 0:1c9f9768e990 142 int ret;
bant62 0:1c9f9768e990 143 char c;
bant62 0:1c9f9768e990 144
bant62 0:1c9f9768e990 145 ret = _i2cSUCCESS;
bant62 0:1c9f9768e990 146 while ((c = *s++)!=NULL)
bant62 0:1c9f9768e990 147 {
bant62 0:1c9f9768e990 148 ret = writeBytes(&c, 1);
bant62 0:1c9f9768e990 149 if (ret == _i2cFAILURE)
bant62 0:1c9f9768e990 150 {
bant62 0:1c9f9768e990 151 return ret;
bant62 0:1c9f9768e990 152 }
bant62 0:1c9f9768e990 153 }
bant62 0:1c9f9768e990 154
bant62 0:1c9f9768e990 155 return ret;
bant62 0:1c9f9768e990 156 }
bant62 0:1c9f9768e990 157
bant62 0:1c9f9768e990 158 ///////////////////////////////////////////////