work fine

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Microduino_MatrixBase.cpp Source File

Microduino_MatrixBase.cpp

00001 // 本作品采用知识共享 署名-非商业性使用-相同方式共享 3.0 未本地化版本 许可协议进行许可
00002 // 访问 http://creativecommons.org/licenses/by-nc-sa/3.0/ 查看该许可协议
00003 // ==============
00004 
00005 // 版权所有:
00006 // @老潘orz  wasdpkj@hotmail.com
00007 // ==============
00008 
00009 // Microduino-IDE
00010 // ==============
00011 // Microduino Getting start:
00012 // http://www.microduino.cc/download/
00013 
00014 // Microduino IDE Support:
00015 // https://github.com/wasdpkj/Microduino-IDE-Support/
00016 
00017 // ==============
00018 // Microduino wiki:
00019 // http://wiki.microduino.cc
00020 
00021 // ==============
00022 // E-mail:
00023 // Kejia Pan
00024 // pankejia@microduino.cc
00025 
00026 // ==============
00027 // Weibo:
00028 // @老潘orz
00029 
00030 #define MICRODUINO_MATRIXBASE_CPP
00031 #include "Microduino_MatrixBase.h"
00032 #include "MicroduinoPinNames.h"
00033 extern Serial pc;
00034 uint8_t MatrixCount = 0;                                     // the total number of attached keys
00035 
00036 I2C g_i2c(P0_11, P0_10);
00037 
00038 uint8_t pgm_read_byte(const uint8_t *p)
00039 {
00040     return *p;
00041 }
00042 
00043 LedControl::LedControl()
00044 {
00045     g_i2c.frequency(400000);
00046     if ( MatrixCount < 64) {
00047         this->matrixIndex = MatrixCount++;                    // assign a key index to this instance
00048         this->Devices_addr = 64 - MatrixCount;
00049     } else {
00050         this->matrixIndex = 255 ;  // too many keys
00051     }
00052 
00053     this->Fast_mode = false;
00054     this->Font_mode = true;
00055     clearColor();
00056 }
00057 
00058 uint8_t LedControl::getDeviceAddr()
00059 {
00060     return (this->Devices_addr + 1);
00061 }
00062 
00063 void LedControl::setDeviceAddr(uint8_t _addr)
00064 {
00065     this->Devices_addr = _addr - 1;
00066 }
00067 
00068 void LedControl::clearFastMode()
00069 {
00070     this->Fast_mode = false;
00071 }
00072 
00073 void LedControl::setFastMode()
00074 {
00075     this->Fast_mode = true;
00076 }
00077 
00078 void LedControl::setFontMode(bool _Mode)
00079 {
00080     this->Font_mode = _Mode;
00081 }
00082 
00083 void LedControl::clearColor()
00084 {
00085     this->value_color[0] = 255;
00086     this->value_color[1] = 255;
00087     this->value_color[2] = 255;
00088 }
00089 
00090 void LedControl::setColor(uint8_t value_r, uint8_t value_g, uint8_t value_b)
00091 {
00092     this->value_color[0] = value_r;
00093     this->value_color[1] = value_g;
00094     this->value_color[2] = value_b;
00095 }
00096 
00097 void LedControl::clearDisplay()
00098 {
00099 #if 0
00100     Wire.beginTransmission(this->Devices_addr + 1); // transmit to device #4
00101     Wire.write(0x60);       // sends five bytes
00102     Wire.endTransmission();    // stop transmitting
00103 #else
00104     char cmd = 0x60;
00105     int ret = g_i2c.write((this->Devices_addr + 1)<<1, &cmd, 1);
00106     //pc.printf("clear led ret = %d\r\n", ret);
00107 #endif
00108 }
00109 
00110 void LedControl::setLedColor(uint8_t _row, uint8_t _col, uint8_t _value_r, uint8_t _value_g, uint8_t _value_b)
00111 {
00112     if (_row < 0 || _row > 7 || _col < 0 || _col > 7 || _value_r > 255 || _value_r < 0 || _value_g > 255 || _value_g < 0 || _value_b > 255 || _value_b < 0)
00113         return;
00114     uint8_t temp[4];
00115     temp[0] = 0x80 | (_row << 3) | _col;
00116     temp[1] = _value_b / 8;
00117     temp[2] = 0x20 | _value_g / 8;
00118     temp[3] = 0x40 | _value_r / 8;
00119 #if 0
00120     Wire.beginTransmission(this->Devices_addr + 1); // transmit to device #4
00121     Wire.write(temp, 4);       // sends five bytes
00122     Wire.endTransmission();    // stop transmitting
00123 #else
00124     g_i2c.write((this->Devices_addr + 1)<<1, (char*)temp, 4, false);
00125     //pc.printf("ret = %d\r\n", ret);
00126 #endif
00127 }
00128 
00129 void LedControl::setLedColorFast(uint8_t _row, uint8_t _col, uint8_t _value_r, uint8_t _value_g, uint8_t _value_b)
00130 {
00131     if (_row < 0 || _row > 7 || _col < 0 || _col > 7 || _value_r > 255 || _value_r < 0 || _value_g > 255 || _value_g < 0 || _value_b > 255 || _value_b < 0)
00132         return;
00133     uint8_t temp[2];
00134     temp[0] = 0xC0 | (_row << 3) | _col;
00135     temp[1] = ((_value_b / 64) << 4) | ((_value_g / 64) << 2) | (_value_r / 64);
00136 #if 0
00137     Wire.beginTransmission(this->Devices_addr + 1); // transmit to device #4
00138     Wire.write(temp, 2);       // sends five bytes
00139     Wire.endTransmission();    // stop transmitting
00140 #else
00141     g_i2c.write((this->Devices_addr + 1) << 1, (char*)temp, 2);
00142 #endif
00143 }
00144 
00145 void LedControl::setLed(uint8_t _row, uint8_t _col, bool _state)
00146 {
00147     if (_row < 0 || _row > 7 || _col < 0 || _col > 7)
00148         return;
00149 
00150     if (_state) {
00151         if (this->Fast_mode)
00152             this->setLedColorFast(_row, _col, this->value_color[0], this->value_color[1], this->value_color[2]);
00153         else
00154             this->setLedColor(_row, _col, this->value_color[0], this->value_color[1], this->value_color[2]);
00155     } else
00156         this->setLedColorFast(_row, _col, 0, 0, 0);
00157 }
00158 
00159 void LedControl::setRow(uint8_t _row, uint8_t _value)
00160 {
00161     if (_row < 0 || _row > 7)
00162         return;
00163 
00164     uint8_t val;
00165     for (uint8_t _col = 0; _col < 8; _col++) {
00166         val = _value >> (_col);
00167         val = val & 0x01;
00168         this->setLed(_row, _col, val);
00169     }
00170 }
00171 
00172 void LedControl::setColumn(uint8_t _col, uint8_t _value)
00173 {
00174     if (_col > 7)
00175         return;
00176 
00177     uint8_t val;
00178     for (uint8_t _row = 0; _row < 8; _row++) {
00179         //val = _value >> (7 - _row);
00180         val = _value >> (_row);
00181         val = val & 0x01;
00182         this->setLed(_row, _col, val);
00183     }
00184 }
00185 
00186 void LedControl::writeString(int _time, char * _displayString)
00187 {
00188     int16_t _leng = 0;
00189     int16_t _wight = 0;
00190     while (_displayString[_leng] != NULL) {
00191         _wight += 1 + pgm_read_byte((uint8_t*)alphabetBitmap[CharToInt(_displayString[_leng++])] + FONE_SIZE_X);
00192     }
00193 //  Serial.println(_wight);
00194 
00195     for (int16_t a = 8; a > -_wight; a--) {
00196         setCursor(a, 0);
00197         print(_displayString);
00198         //delay(_time);
00199         wait_ms(_time);
00200     }
00201 }
00202 
00203 void LedControl::setCursor(int16_t _x, int16_t _y)
00204 {
00205     this->cursor_x = _x;
00206     this->cursor_y = _y;
00207 }
00208 
00209 size_t LedControl::write(uint8_t c)
00210 {
00211     if (CharToInt(c) > 94 || CharToInt(c) < 0)
00212         return 0;
00213 
00214     this->displayChar((this->cursor_x), (this->cursor_y), c);
00215     if (this->Font_mode)
00216         this->cursor_x += 1 + pgm_read_byte((uint8_t*)(alphabetBitmap[CharToInt(c)] + FONE_SIZE_X));
00217     else
00218         this->cursor_y += 1 + FONE_SIZE_Y;
00219     return 1;
00220 }
00221 
00222 
00223 void LedControl::displayChar(int8_t row, int8_t col, char _charIndex)
00224 {
00225     if (row < 0 - 8 || row > 7 + 8 || col < 0 - 8 || col > 7 + 8 || CharToInt(_charIndex) > 94 || CharToInt(_charIndex) < 0)
00226         return;
00227 
00228     uint8_t n = CharToInt(_charIndex);
00229     uint8_t m = (this->Font_mode ? FONE_SIZE_X - pgm_read_byte((uint8_t*)alphabetBitmap[n] + FONE_SIZE_X) : 0);
00230 
00231     uint8_t val;
00232     for (int8_t i = m; i < FONE_SIZE_X + 1; i++) {
00233         for (int8_t _col = col; col < 0 ? _col < 8 + col : _col < 8; _col++) {
00234             if (i - m + row < 0 || i - m + row > 7)
00235                 break;
00236             if (i != FONE_SIZE_X)
00237                 val = pgm_read_byte((uint8_t*)alphabetBitmap[n] + i) >> (_col - col);
00238             else
00239                 val = 0x00 >> (_col - col);
00240             val = val & 0x01;
00241             this->setLed(i - m + row, _col, val);
00242         }
00243     }
00244 }