work fine

Dependencies:   mbed

Committer:
lixianyu
Date:
Thu Jun 02 01:07:50 2016 +0000
Revision:
2:487a727d6181
Parent:
1:14b7c3a3ec60
drawBMP work.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:5ca227682ee7 1 // 本作品采用知识共享 署名-非商业性使用-相同方式共享 3.0 未本地化版本 许可协议进行许可
lixianyu 0:5ca227682ee7 2 // 访问 http://creativecommons.org/licenses/by-nc-sa/3.0/ 查看该许可协议
lixianyu 0:5ca227682ee7 3 // ==============
lixianyu 0:5ca227682ee7 4
lixianyu 0:5ca227682ee7 5 // 版权所有:
lixianyu 0:5ca227682ee7 6 // @老潘orz wasdpkj@hotmail.com
lixianyu 0:5ca227682ee7 7 // ==============
lixianyu 0:5ca227682ee7 8
lixianyu 0:5ca227682ee7 9 // Microduino-IDE
lixianyu 0:5ca227682ee7 10 // ==============
lixianyu 0:5ca227682ee7 11 // Microduino Getting start:
lixianyu 0:5ca227682ee7 12 // http://www.microduino.cc/download/
lixianyu 0:5ca227682ee7 13
lixianyu 0:5ca227682ee7 14 // Microduino IDE Support:
lixianyu 0:5ca227682ee7 15 // https://github.com/wasdpkj/Microduino-IDE-Support/
lixianyu 0:5ca227682ee7 16
lixianyu 0:5ca227682ee7 17 // ==============
lixianyu 0:5ca227682ee7 18 // Microduino wiki:
lixianyu 0:5ca227682ee7 19 // http://wiki.microduino.cc
lixianyu 0:5ca227682ee7 20
lixianyu 0:5ca227682ee7 21 // ==============
lixianyu 0:5ca227682ee7 22 // E-mail:
lixianyu 0:5ca227682ee7 23 // Kejia Pan
lixianyu 0:5ca227682ee7 24 // pankejia@microduino.cc
lixianyu 0:5ca227682ee7 25
lixianyu 0:5ca227682ee7 26 // ==============
lixianyu 0:5ca227682ee7 27 // Weibo:
lixianyu 0:5ca227682ee7 28 // @老潘orz
lixianyu 0:5ca227682ee7 29
lixianyu 0:5ca227682ee7 30 #define MICRODUINO_MATRIXBASE_CPP
lixianyu 0:5ca227682ee7 31 #include "Microduino_MatrixBase.h"
lixianyu 0:5ca227682ee7 32 #include "MicroduinoPinNames.h"
lixianyu 1:14b7c3a3ec60 33 extern Serial pc;
lixianyu 0:5ca227682ee7 34 uint8_t MatrixCount = 0; // the total number of attached keys
lixianyu 0:5ca227682ee7 35
lixianyu 1:14b7c3a3ec60 36 I2C g_i2c(P0_11, P0_10);
lixianyu 0:5ca227682ee7 37
lixianyu 1:14b7c3a3ec60 38 uint8_t pgm_read_byte(const uint8_t *p)
lixianyu 0:5ca227682ee7 39 {
lixianyu 0:5ca227682ee7 40 return *p;
lixianyu 0:5ca227682ee7 41 }
lixianyu 0:5ca227682ee7 42
lixianyu 0:5ca227682ee7 43 LedControl::LedControl()
lixianyu 0:5ca227682ee7 44 {
lixianyu 1:14b7c3a3ec60 45 g_i2c.frequency(400000);
lixianyu 0:5ca227682ee7 46 if ( MatrixCount < 64) {
lixianyu 0:5ca227682ee7 47 this->matrixIndex = MatrixCount++; // assign a key index to this instance
lixianyu 0:5ca227682ee7 48 this->Devices_addr = 64 - MatrixCount;
lixianyu 0:5ca227682ee7 49 } else {
lixianyu 0:5ca227682ee7 50 this->matrixIndex = 255 ; // too many keys
lixianyu 0:5ca227682ee7 51 }
lixianyu 0:5ca227682ee7 52
lixianyu 0:5ca227682ee7 53 this->Fast_mode = false;
lixianyu 0:5ca227682ee7 54 this->Font_mode = true;
lixianyu 0:5ca227682ee7 55 clearColor();
lixianyu 0:5ca227682ee7 56 }
lixianyu 0:5ca227682ee7 57
lixianyu 0:5ca227682ee7 58 uint8_t LedControl::getDeviceAddr()
lixianyu 0:5ca227682ee7 59 {
lixianyu 0:5ca227682ee7 60 return (this->Devices_addr + 1);
lixianyu 0:5ca227682ee7 61 }
lixianyu 0:5ca227682ee7 62
lixianyu 0:5ca227682ee7 63 void LedControl::setDeviceAddr(uint8_t _addr)
lixianyu 0:5ca227682ee7 64 {
lixianyu 0:5ca227682ee7 65 this->Devices_addr = _addr - 1;
lixianyu 0:5ca227682ee7 66 }
lixianyu 0:5ca227682ee7 67
lixianyu 0:5ca227682ee7 68 void LedControl::clearFastMode()
lixianyu 0:5ca227682ee7 69 {
lixianyu 0:5ca227682ee7 70 this->Fast_mode = false;
lixianyu 0:5ca227682ee7 71 }
lixianyu 0:5ca227682ee7 72
lixianyu 0:5ca227682ee7 73 void LedControl::setFastMode()
lixianyu 0:5ca227682ee7 74 {
lixianyu 0:5ca227682ee7 75 this->Fast_mode = true;
lixianyu 0:5ca227682ee7 76 }
lixianyu 0:5ca227682ee7 77
lixianyu 0:5ca227682ee7 78 void LedControl::setFontMode(bool _Mode)
lixianyu 0:5ca227682ee7 79 {
lixianyu 0:5ca227682ee7 80 this->Font_mode = _Mode;
lixianyu 0:5ca227682ee7 81 }
lixianyu 0:5ca227682ee7 82
lixianyu 0:5ca227682ee7 83 void LedControl::clearColor()
lixianyu 0:5ca227682ee7 84 {
lixianyu 0:5ca227682ee7 85 this->value_color[0] = 255;
lixianyu 0:5ca227682ee7 86 this->value_color[1] = 255;
lixianyu 0:5ca227682ee7 87 this->value_color[2] = 255;
lixianyu 0:5ca227682ee7 88 }
lixianyu 0:5ca227682ee7 89
lixianyu 0:5ca227682ee7 90 void LedControl::setColor(uint8_t value_r, uint8_t value_g, uint8_t value_b)
lixianyu 0:5ca227682ee7 91 {
lixianyu 0:5ca227682ee7 92 this->value_color[0] = value_r;
lixianyu 0:5ca227682ee7 93 this->value_color[1] = value_g;
lixianyu 0:5ca227682ee7 94 this->value_color[2] = value_b;
lixianyu 0:5ca227682ee7 95 }
lixianyu 0:5ca227682ee7 96
lixianyu 0:5ca227682ee7 97 void LedControl::clearDisplay()
lixianyu 0:5ca227682ee7 98 {
lixianyu 0:5ca227682ee7 99 #if 0
lixianyu 0:5ca227682ee7 100 Wire.beginTransmission(this->Devices_addr + 1); // transmit to device #4
lixianyu 0:5ca227682ee7 101 Wire.write(0x60); // sends five bytes
lixianyu 0:5ca227682ee7 102 Wire.endTransmission(); // stop transmitting
lixianyu 0:5ca227682ee7 103 #else
lixianyu 0:5ca227682ee7 104 char cmd = 0x60;
lixianyu 1:14b7c3a3ec60 105 int ret = g_i2c.write((this->Devices_addr + 1)<<1, &cmd, 1);
lixianyu 1:14b7c3a3ec60 106 //pc.printf("clear led ret = %d\r\n", ret);
lixianyu 0:5ca227682ee7 107 #endif
lixianyu 0:5ca227682ee7 108 }
lixianyu 0:5ca227682ee7 109
lixianyu 0:5ca227682ee7 110 void LedControl::setLedColor(uint8_t _row, uint8_t _col, uint8_t _value_r, uint8_t _value_g, uint8_t _value_b)
lixianyu 0:5ca227682ee7 111 {
lixianyu 0:5ca227682ee7 112 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)
lixianyu 0:5ca227682ee7 113 return;
lixianyu 0:5ca227682ee7 114 uint8_t temp[4];
lixianyu 0:5ca227682ee7 115 temp[0] = 0x80 | (_row << 3) | _col;
lixianyu 0:5ca227682ee7 116 temp[1] = _value_b / 8;
lixianyu 0:5ca227682ee7 117 temp[2] = 0x20 | _value_g / 8;
lixianyu 0:5ca227682ee7 118 temp[3] = 0x40 | _value_r / 8;
lixianyu 0:5ca227682ee7 119 #if 0
lixianyu 0:5ca227682ee7 120 Wire.beginTransmission(this->Devices_addr + 1); // transmit to device #4
lixianyu 0:5ca227682ee7 121 Wire.write(temp, 4); // sends five bytes
lixianyu 0:5ca227682ee7 122 Wire.endTransmission(); // stop transmitting
lixianyu 0:5ca227682ee7 123 #else
lixianyu 1:14b7c3a3ec60 124 g_i2c.write((this->Devices_addr + 1)<<1, (char*)temp, 4, false);
lixianyu 1:14b7c3a3ec60 125 //pc.printf("ret = %d\r\n", ret);
lixianyu 0:5ca227682ee7 126 #endif
lixianyu 0:5ca227682ee7 127 }
lixianyu 0:5ca227682ee7 128
lixianyu 0:5ca227682ee7 129 void LedControl::setLedColorFast(uint8_t _row, uint8_t _col, uint8_t _value_r, uint8_t _value_g, uint8_t _value_b)
lixianyu 0:5ca227682ee7 130 {
lixianyu 0:5ca227682ee7 131 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)
lixianyu 0:5ca227682ee7 132 return;
lixianyu 0:5ca227682ee7 133 uint8_t temp[2];
lixianyu 0:5ca227682ee7 134 temp[0] = 0xC0 | (_row << 3) | _col;
lixianyu 0:5ca227682ee7 135 temp[1] = ((_value_b / 64) << 4) | ((_value_g / 64) << 2) | (_value_r / 64);
lixianyu 0:5ca227682ee7 136 #if 0
lixianyu 0:5ca227682ee7 137 Wire.beginTransmission(this->Devices_addr + 1); // transmit to device #4
lixianyu 0:5ca227682ee7 138 Wire.write(temp, 2); // sends five bytes
lixianyu 0:5ca227682ee7 139 Wire.endTransmission(); // stop transmitting
lixianyu 0:5ca227682ee7 140 #else
lixianyu 1:14b7c3a3ec60 141 g_i2c.write((this->Devices_addr + 1) << 1, (char*)temp, 2);
lixianyu 0:5ca227682ee7 142 #endif
lixianyu 0:5ca227682ee7 143 }
lixianyu 0:5ca227682ee7 144
lixianyu 0:5ca227682ee7 145 void LedControl::setLed(uint8_t _row, uint8_t _col, bool _state)
lixianyu 0:5ca227682ee7 146 {
lixianyu 0:5ca227682ee7 147 if (_row < 0 || _row > 7 || _col < 0 || _col > 7)
lixianyu 0:5ca227682ee7 148 return;
lixianyu 0:5ca227682ee7 149
lixianyu 0:5ca227682ee7 150 if (_state) {
lixianyu 0:5ca227682ee7 151 if (this->Fast_mode)
lixianyu 0:5ca227682ee7 152 this->setLedColorFast(_row, _col, this->value_color[0], this->value_color[1], this->value_color[2]);
lixianyu 0:5ca227682ee7 153 else
lixianyu 0:5ca227682ee7 154 this->setLedColor(_row, _col, this->value_color[0], this->value_color[1], this->value_color[2]);
lixianyu 0:5ca227682ee7 155 } else
lixianyu 0:5ca227682ee7 156 this->setLedColorFast(_row, _col, 0, 0, 0);
lixianyu 0:5ca227682ee7 157 }
lixianyu 0:5ca227682ee7 158
lixianyu 0:5ca227682ee7 159 void LedControl::setRow(uint8_t _row, uint8_t _value)
lixianyu 0:5ca227682ee7 160 {
lixianyu 0:5ca227682ee7 161 if (_row < 0 || _row > 7)
lixianyu 0:5ca227682ee7 162 return;
lixianyu 0:5ca227682ee7 163
lixianyu 0:5ca227682ee7 164 uint8_t val;
lixianyu 0:5ca227682ee7 165 for (uint8_t _col = 0; _col < 8; _col++) {
lixianyu 0:5ca227682ee7 166 val = _value >> (_col);
lixianyu 0:5ca227682ee7 167 val = val & 0x01;
lixianyu 0:5ca227682ee7 168 this->setLed(_row, _col, val);
lixianyu 0:5ca227682ee7 169 }
lixianyu 0:5ca227682ee7 170 }
lixianyu 0:5ca227682ee7 171
lixianyu 0:5ca227682ee7 172 void LedControl::setColumn(uint8_t _col, uint8_t _value)
lixianyu 0:5ca227682ee7 173 {
lixianyu 0:5ca227682ee7 174 if (_col > 7)
lixianyu 0:5ca227682ee7 175 return;
lixianyu 0:5ca227682ee7 176
lixianyu 0:5ca227682ee7 177 uint8_t val;
lixianyu 0:5ca227682ee7 178 for (uint8_t _row = 0; _row < 8; _row++) {
lixianyu 0:5ca227682ee7 179 //val = _value >> (7 - _row);
lixianyu 0:5ca227682ee7 180 val = _value >> (_row);
lixianyu 0:5ca227682ee7 181 val = val & 0x01;
lixianyu 0:5ca227682ee7 182 this->setLed(_row, _col, val);
lixianyu 0:5ca227682ee7 183 }
lixianyu 0:5ca227682ee7 184 }
lixianyu 0:5ca227682ee7 185
lixianyu 0:5ca227682ee7 186 void LedControl::writeString(int _time, char * _displayString)
lixianyu 0:5ca227682ee7 187 {
lixianyu 0:5ca227682ee7 188 int16_t _leng = 0;
lixianyu 0:5ca227682ee7 189 int16_t _wight = 0;
lixianyu 0:5ca227682ee7 190 while (_displayString[_leng] != NULL) {
lixianyu 0:5ca227682ee7 191 _wight += 1 + pgm_read_byte((uint8_t*)alphabetBitmap[CharToInt(_displayString[_leng++])] + FONE_SIZE_X);
lixianyu 0:5ca227682ee7 192 }
lixianyu 0:5ca227682ee7 193 // Serial.println(_wight);
lixianyu 0:5ca227682ee7 194
lixianyu 0:5ca227682ee7 195 for (int16_t a = 8; a > -_wight; a--) {
lixianyu 0:5ca227682ee7 196 setCursor(a, 0);
lixianyu 0:5ca227682ee7 197 print(_displayString);
lixianyu 0:5ca227682ee7 198 //delay(_time);
lixianyu 0:5ca227682ee7 199 wait_ms(_time);
lixianyu 0:5ca227682ee7 200 }
lixianyu 0:5ca227682ee7 201 }
lixianyu 0:5ca227682ee7 202
lixianyu 0:5ca227682ee7 203 void LedControl::setCursor(int16_t _x, int16_t _y)
lixianyu 0:5ca227682ee7 204 {
lixianyu 0:5ca227682ee7 205 this->cursor_x = _x;
lixianyu 0:5ca227682ee7 206 this->cursor_y = _y;
lixianyu 0:5ca227682ee7 207 }
lixianyu 0:5ca227682ee7 208
lixianyu 0:5ca227682ee7 209 size_t LedControl::write(uint8_t c)
lixianyu 0:5ca227682ee7 210 {
lixianyu 0:5ca227682ee7 211 if (CharToInt(c) > 94 || CharToInt(c) < 0)
lixianyu 0:5ca227682ee7 212 return 0;
lixianyu 0:5ca227682ee7 213
lixianyu 0:5ca227682ee7 214 this->displayChar((this->cursor_x), (this->cursor_y), c);
lixianyu 0:5ca227682ee7 215 if (this->Font_mode)
lixianyu 0:5ca227682ee7 216 this->cursor_x += 1 + pgm_read_byte((uint8_t*)(alphabetBitmap[CharToInt(c)] + FONE_SIZE_X));
lixianyu 0:5ca227682ee7 217 else
lixianyu 0:5ca227682ee7 218 this->cursor_y += 1 + FONE_SIZE_Y;
lixianyu 0:5ca227682ee7 219 return 1;
lixianyu 0:5ca227682ee7 220 }
lixianyu 0:5ca227682ee7 221
lixianyu 0:5ca227682ee7 222
lixianyu 0:5ca227682ee7 223 void LedControl::displayChar(int8_t row, int8_t col, char _charIndex)
lixianyu 0:5ca227682ee7 224 {
lixianyu 0:5ca227682ee7 225 if (row < 0 - 8 || row > 7 + 8 || col < 0 - 8 || col > 7 + 8 || CharToInt(_charIndex) > 94 || CharToInt(_charIndex) < 0)
lixianyu 0:5ca227682ee7 226 return;
lixianyu 0:5ca227682ee7 227
lixianyu 0:5ca227682ee7 228 uint8_t n = CharToInt(_charIndex);
lixianyu 0:5ca227682ee7 229 uint8_t m = (this->Font_mode ? FONE_SIZE_X - pgm_read_byte((uint8_t*)alphabetBitmap[n] + FONE_SIZE_X) : 0);
lixianyu 0:5ca227682ee7 230
lixianyu 0:5ca227682ee7 231 uint8_t val;
lixianyu 0:5ca227682ee7 232 for (int8_t i = m; i < FONE_SIZE_X + 1; i++) {
lixianyu 0:5ca227682ee7 233 for (int8_t _col = col; col < 0 ? _col < 8 + col : _col < 8; _col++) {
lixianyu 0:5ca227682ee7 234 if (i - m + row < 0 || i - m + row > 7)
lixianyu 0:5ca227682ee7 235 break;
lixianyu 0:5ca227682ee7 236 if (i != FONE_SIZE_X)
lixianyu 0:5ca227682ee7 237 val = pgm_read_byte((uint8_t*)alphabetBitmap[n] + i) >> (_col - col);
lixianyu 0:5ca227682ee7 238 else
lixianyu 0:5ca227682ee7 239 val = 0x00 >> (_col - col);
lixianyu 0:5ca227682ee7 240 val = val & 0x01;
lixianyu 0:5ca227682ee7 241 this->setLed(i - m + row, _col, val);
lixianyu 0:5ca227682ee7 242 }
lixianyu 0:5ca227682ee7 243 }
lixianyu 0:5ca227682ee7 244 }