PT6311 VFD driver lib. Initial version, supports VFDEM2 DVD player display.
PT6311.cpp@0:43499fc489c6, 2016-01-20 (annotated)
- Committer:
- wim
- Date:
- Wed Jan 20 19:05:43 2016 +0000
- Revision:
- 0:43499fc489c6
Initial version, VFDEM2.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:43499fc489c6 | 1 | /* mbed PT6311 Library, for Princeton PT6311 VFD controller |
wim | 0:43499fc489c6 | 2 | * Copyright (c) 2016, v01: WH, Initial version, for VFDEM2 code |
wim | 0:43499fc489c6 | 3 | * |
wim | 0:43499fc489c6 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:43499fc489c6 | 5 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:43499fc489c6 | 6 | * in the Software without restriction, including without limitation the rights |
wim | 0:43499fc489c6 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:43499fc489c6 | 8 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:43499fc489c6 | 9 | * furnished to do so, subject to the following conditions: |
wim | 0:43499fc489c6 | 10 | * |
wim | 0:43499fc489c6 | 11 | * The above copyright notice and this permission notice shall be included in |
wim | 0:43499fc489c6 | 12 | * all copies or substantial portions of the Software. |
wim | 0:43499fc489c6 | 13 | * |
wim | 0:43499fc489c6 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:43499fc489c6 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:43499fc489c6 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:43499fc489c6 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:43499fc489c6 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:43499fc489c6 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:43499fc489c6 | 20 | * THE SOFTWARE. |
wim | 0:43499fc489c6 | 21 | */ |
wim | 0:43499fc489c6 | 22 | #include "mbed.h" |
wim | 0:43499fc489c6 | 23 | #include "PT6311.h" |
wim | 0:43499fc489c6 | 24 | |
wim | 0:43499fc489c6 | 25 | /** Constructor for class for driving Princeton PT6311 VFD controller |
wim | 0:43499fc489c6 | 26 | * |
wim | 0:43499fc489c6 | 27 | * @brief Supports 8 Grids of 20 Segments upto 16 Grids of 12 Segments. Also supports a scanned keyboard of upto 48 keys, 4 switches and 5 LEDs. |
wim | 0:43499fc489c6 | 28 | * SPI bus interface device. |
wim | 0:43499fc489c6 | 29 | * |
wim | 0:43499fc489c6 | 30 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:43499fc489c6 | 31 | * @param Mode selects either number of Digits and Segments |
wim | 0:43499fc489c6 | 32 | */ |
wim | 0:43499fc489c6 | 33 | PT6311::PT6311(PinName mosi, PinName miso, PinName sclk, PinName cs, Mode mode) : _spi(mosi,miso,sclk), _cs(cs), _mode(mode) { |
wim | 0:43499fc489c6 | 34 | |
wim | 0:43499fc489c6 | 35 | _init(); |
wim | 0:43499fc489c6 | 36 | } |
wim | 0:43499fc489c6 | 37 | |
wim | 0:43499fc489c6 | 38 | /** Init the SPI interface and the controller |
wim | 0:43499fc489c6 | 39 | * @param none |
wim | 0:43499fc489c6 | 40 | * @return none |
wim | 0:43499fc489c6 | 41 | */ |
wim | 0:43499fc489c6 | 42 | void PT6311::_init(){ |
wim | 0:43499fc489c6 | 43 | |
wim | 0:43499fc489c6 | 44 | //init SPI |
wim | 0:43499fc489c6 | 45 | _cs=1; |
wim | 0:43499fc489c6 | 46 | _spi.format(8,3); //PT6311 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge) |
wim | 0:43499fc489c6 | 47 | // _spi.frequency(100000); |
wim | 0:43499fc489c6 | 48 | _spi.frequency(500000); |
wim | 0:43499fc489c6 | 49 | |
wim | 0:43499fc489c6 | 50 | //init controller |
wim | 0:43499fc489c6 | 51 | _writeCmd(PT6311_MODE_SET_CMD, _mode); // Mode set command |
wim | 0:43499fc489c6 | 52 | |
wim | 0:43499fc489c6 | 53 | _display = PT6311_DSP_ON; |
wim | 0:43499fc489c6 | 54 | _bright = PT6311_BRT_DEF; |
wim | 0:43499fc489c6 | 55 | _writeCmd(PT6311_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:43499fc489c6 | 56 | |
wim | 0:43499fc489c6 | 57 | _writeCmd(PT6311_DATA_SET_CMD, PT6311_DATA_WR | PT6311_ADDR_INC | PT6311_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:43499fc489c6 | 58 | } |
wim | 0:43499fc489c6 | 59 | |
wim | 0:43499fc489c6 | 60 | |
wim | 0:43499fc489c6 | 61 | /** Clear the screen and locate to 0 |
wim | 0:43499fc489c6 | 62 | */ |
wim | 0:43499fc489c6 | 63 | void PT6311::cls() { |
wim | 0:43499fc489c6 | 64 | |
wim | 0:43499fc489c6 | 65 | _cs=0; |
wim | 0:43499fc489c6 | 66 | wait_us(1); |
wim | 0:43499fc489c6 | 67 | _spi.write(_flip(PT6311_ADDR_SET_CMD | 0x00)); // Address set cmd, 0 |
wim | 0:43499fc489c6 | 68 | |
wim | 0:43499fc489c6 | 69 | for (int cnt=0; cnt<PT6311_DISPLAY_MEM; cnt++) { |
wim | 0:43499fc489c6 | 70 | _spi.write(0x00); // data |
wim | 0:43499fc489c6 | 71 | } |
wim | 0:43499fc489c6 | 72 | |
wim | 0:43499fc489c6 | 73 | wait_us(1); |
wim | 0:43499fc489c6 | 74 | _cs=1; |
wim | 0:43499fc489c6 | 75 | |
wim | 0:43499fc489c6 | 76 | } |
wim | 0:43499fc489c6 | 77 | |
wim | 0:43499fc489c6 | 78 | /** Set Brightness |
wim | 0:43499fc489c6 | 79 | * |
wim | 0:43499fc489c6 | 80 | * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) |
wim | 0:43499fc489c6 | 81 | * @return none |
wim | 0:43499fc489c6 | 82 | */ |
wim | 0:43499fc489c6 | 83 | void PT6311::setBrightness(char brightness){ |
wim | 0:43499fc489c6 | 84 | |
wim | 0:43499fc489c6 | 85 | _bright = brightness & PT6311_BRT_MSK; // mask invalid bits |
wim | 0:43499fc489c6 | 86 | |
wim | 0:43499fc489c6 | 87 | _writeCmd(PT6311_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:43499fc489c6 | 88 | |
wim | 0:43499fc489c6 | 89 | } |
wim | 0:43499fc489c6 | 90 | |
wim | 0:43499fc489c6 | 91 | /** Set the Display mode On/off |
wim | 0:43499fc489c6 | 92 | * |
wim | 0:43499fc489c6 | 93 | * @param bool display mode |
wim | 0:43499fc489c6 | 94 | */ |
wim | 0:43499fc489c6 | 95 | void PT6311::setDisplay(bool on) { |
wim | 0:43499fc489c6 | 96 | |
wim | 0:43499fc489c6 | 97 | if (on) { |
wim | 0:43499fc489c6 | 98 | _display = PT6311_DSP_ON; |
wim | 0:43499fc489c6 | 99 | } |
wim | 0:43499fc489c6 | 100 | else { |
wim | 0:43499fc489c6 | 101 | _display = PT6311_DSP_OFF; |
wim | 0:43499fc489c6 | 102 | } |
wim | 0:43499fc489c6 | 103 | |
wim | 0:43499fc489c6 | 104 | _writeCmd(PT6311_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness |
wim | 0:43499fc489c6 | 105 | } |
wim | 0:43499fc489c6 | 106 | |
wim | 0:43499fc489c6 | 107 | /** Write databyte to PT6311 |
wim | 0:43499fc489c6 | 108 | * @param int address display memory location to write byte |
wim | 0:43499fc489c6 | 109 | * @param char data byte written at given address |
wim | 0:43499fc489c6 | 110 | * @return none |
wim | 0:43499fc489c6 | 111 | */ |
wim | 0:43499fc489c6 | 112 | void PT6311::writeData(int address, char data) { |
wim | 0:43499fc489c6 | 113 | _cs=0; |
wim | 0:43499fc489c6 | 114 | wait_us(1); |
wim | 0:43499fc489c6 | 115 | _spi.write(_flip(PT6311_ADDR_SET_CMD | (address & PT6311_ADDR_MSK))); // Set Address cmd |
wim | 0:43499fc489c6 | 116 | |
wim | 0:43499fc489c6 | 117 | _spi.write(_flip(data)); // data |
wim | 0:43499fc489c6 | 118 | |
wim | 0:43499fc489c6 | 119 | wait_us(1); |
wim | 0:43499fc489c6 | 120 | _cs=1; |
wim | 0:43499fc489c6 | 121 | |
wim | 0:43499fc489c6 | 122 | } |
wim | 0:43499fc489c6 | 123 | |
wim | 0:43499fc489c6 | 124 | /** Write Display datablock to PT6311 |
wim | 0:43499fc489c6 | 125 | * @param DisplayData_t data Array of PT6311_DISPLAY_MEM (=48) bytes for displaydata (starting at address 0) |
wim | 0:43499fc489c6 | 126 | * @param length number bytes to write (valid range 0..PT6311_DISPLAY_MEM (=48), starting at address 0) |
wim | 0:43499fc489c6 | 127 | * @return none |
wim | 0:43499fc489c6 | 128 | */ |
wim | 0:43499fc489c6 | 129 | void PT6311::writeData(DisplayData_t data, int length) { |
wim | 0:43499fc489c6 | 130 | _cs=0; |
wim | 0:43499fc489c6 | 131 | wait_us(1); |
wim | 0:43499fc489c6 | 132 | _spi.write(_flip(PT6311_ADDR_SET_CMD | 0x00)); // Set Address at 0 |
wim | 0:43499fc489c6 | 133 | |
wim | 0:43499fc489c6 | 134 | // sanity check |
wim | 0:43499fc489c6 | 135 | if (length < 0) {length = 0;} |
wim | 0:43499fc489c6 | 136 | if (length > PT6311_DISPLAY_MEM) {length = PT6311_DISPLAY_MEM;} |
wim | 0:43499fc489c6 | 137 | |
wim | 0:43499fc489c6 | 138 | // for (int idx=0; idx<PT6311_DISPLAY_MEM; idx++) { |
wim | 0:43499fc489c6 | 139 | for (int idx=0; idx<length; idx++) { |
wim | 0:43499fc489c6 | 140 | _spi.write(_flip(data[idx])); // data |
wim | 0:43499fc489c6 | 141 | } |
wim | 0:43499fc489c6 | 142 | |
wim | 0:43499fc489c6 | 143 | wait_us(1); |
wim | 0:43499fc489c6 | 144 | _cs=1; |
wim | 0:43499fc489c6 | 145 | |
wim | 0:43499fc489c6 | 146 | } |
wim | 0:43499fc489c6 | 147 | |
wim | 0:43499fc489c6 | 148 | /** Read keydata block from PT6311 |
wim | 0:43499fc489c6 | 149 | * @param *keydata Ptr to Array of PT6311_KEY_MEM (=6) bytes for keydata |
wim | 0:43499fc489c6 | 150 | * @return bool keypress True when at least one key was pressed |
wim | 0:43499fc489c6 | 151 | * |
wim | 0:43499fc489c6 | 152 | * Note: Due to the hardware configuration the PT6311 key matrix scanner will detect multiple keys pressed at same time, |
wim | 0:43499fc489c6 | 153 | * but this may also result in some spurious keys being set in keypress data array. |
wim | 0:43499fc489c6 | 154 | * It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting. |
wim | 0:43499fc489c6 | 155 | */ |
wim | 0:43499fc489c6 | 156 | bool PT6311::getKeys(KeyData_t *keydata) { |
wim | 0:43499fc489c6 | 157 | int keypress = 0; |
wim | 0:43499fc489c6 | 158 | char data; |
wim | 0:43499fc489c6 | 159 | |
wim | 0:43499fc489c6 | 160 | // Read keys |
wim | 0:43499fc489c6 | 161 | _cs=0; |
wim | 0:43499fc489c6 | 162 | wait_us(1); |
wim | 0:43499fc489c6 | 163 | |
wim | 0:43499fc489c6 | 164 | // Enable Key Read mode |
wim | 0:43499fc489c6 | 165 | _spi.write(_flip(PT6311_DATA_SET_CMD | PT6311_KEY_RD | PT6311_ADDR_INC | PT6311_MODE_NORM)); // Data set cmd, normal mode, auto incr, read data |
wim | 0:43499fc489c6 | 166 | |
wim | 0:43499fc489c6 | 167 | for (int idx=0; idx < PT6311_KEY_MEM; idx++) { |
wim | 0:43499fc489c6 | 168 | data = _flip(_spi.write(0xFF)); // read keys and correct bitorder |
wim | 0:43499fc489c6 | 169 | |
wim | 0:43499fc489c6 | 170 | data = data & PT6311_KEY_MSK; // Mask valid bits |
wim | 0:43499fc489c6 | 171 | if (data != 0) { // Check for any pressed key |
wim | 0:43499fc489c6 | 172 | for (int bit=0; bit < 8; bit++) { |
wim | 0:43499fc489c6 | 173 | if (data & (1 << bit)) {keypress++;} // Test all significant bits |
wim | 0:43499fc489c6 | 174 | } |
wim | 0:43499fc489c6 | 175 | } |
wim | 0:43499fc489c6 | 176 | |
wim | 0:43499fc489c6 | 177 | (*keydata)[idx] = data; // Store keydata after correcting bitorder |
wim | 0:43499fc489c6 | 178 | } |
wim | 0:43499fc489c6 | 179 | |
wim | 0:43499fc489c6 | 180 | wait_us(1); |
wim | 0:43499fc489c6 | 181 | _cs=1; |
wim | 0:43499fc489c6 | 182 | |
wim | 0:43499fc489c6 | 183 | // Restore Data Write mode |
wim | 0:43499fc489c6 | 184 | _writeCmd(PT6311_DATA_SET_CMD, PT6311_DATA_WR | PT6311_ADDR_INC | PT6311_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:43499fc489c6 | 185 | |
wim | 0:43499fc489c6 | 186 | #if(1) |
wim | 0:43499fc489c6 | 187 | // Dismiss multiple keypresses at same time |
wim | 0:43499fc489c6 | 188 | return (keypress == 1); |
wim | 0:43499fc489c6 | 189 | #else |
wim | 0:43499fc489c6 | 190 | // Allow multiple keypress and accept possible spurious keys |
wim | 0:43499fc489c6 | 191 | return (keypress > 0); |
wim | 0:43499fc489c6 | 192 | #endif |
wim | 0:43499fc489c6 | 193 | } |
wim | 0:43499fc489c6 | 194 | |
wim | 0:43499fc489c6 | 195 | |
wim | 0:43499fc489c6 | 196 | /** Read switches from PT6311 |
wim | 0:43499fc489c6 | 197 | * |
wim | 0:43499fc489c6 | 198 | * @param none |
wim | 0:43499fc489c6 | 199 | * @return char for switch data (4 least significant bits) |
wim | 0:43499fc489c6 | 200 | * |
wim | 0:43499fc489c6 | 201 | */ |
wim | 0:43499fc489c6 | 202 | char PT6311::getSwitches() { |
wim | 0:43499fc489c6 | 203 | char data; |
wim | 0:43499fc489c6 | 204 | |
wim | 0:43499fc489c6 | 205 | // Read switches |
wim | 0:43499fc489c6 | 206 | _cs=0; |
wim | 0:43499fc489c6 | 207 | wait_us(1); |
wim | 0:43499fc489c6 | 208 | |
wim | 0:43499fc489c6 | 209 | // Enable Switch Read mode |
wim | 0:43499fc489c6 | 210 | _spi.write(_flip(PT6311_DATA_SET_CMD | PT6311_SW_RD | PT6311_ADDR_INC | PT6311_MODE_NORM)); // Data set cmd, normal mode, auto incr, read data |
wim | 0:43499fc489c6 | 211 | |
wim | 0:43499fc489c6 | 212 | data = _flip(_spi.write(0xFF)) & PT6311_SW_MSK; // read switches and correct bitorder |
wim | 0:43499fc489c6 | 213 | |
wim | 0:43499fc489c6 | 214 | wait_us(1); |
wim | 0:43499fc489c6 | 215 | _cs=1; |
wim | 0:43499fc489c6 | 216 | |
wim | 0:43499fc489c6 | 217 | // Restore Data Write mode |
wim | 0:43499fc489c6 | 218 | _writeCmd(PT6311_DATA_SET_CMD, PT6311_DATA_WR | PT6311_ADDR_INC | PT6311_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:43499fc489c6 | 219 | |
wim | 0:43499fc489c6 | 220 | return data; |
wim | 0:43499fc489c6 | 221 | } |
wim | 0:43499fc489c6 | 222 | |
wim | 0:43499fc489c6 | 223 | |
wim | 0:43499fc489c6 | 224 | /** Set LEDs |
wim | 0:43499fc489c6 | 225 | * |
wim | 0:43499fc489c6 | 226 | * @param char leds (5 least significant bits) |
wim | 0:43499fc489c6 | 227 | * @return none |
wim | 0:43499fc489c6 | 228 | */ |
wim | 0:43499fc489c6 | 229 | void PT6311::setLED (char leds) { |
wim | 0:43499fc489c6 | 230 | |
wim | 0:43499fc489c6 | 231 | // Set LEDs |
wim | 0:43499fc489c6 | 232 | _cs=0; |
wim | 0:43499fc489c6 | 233 | wait_us(1); |
wim | 0:43499fc489c6 | 234 | |
wim | 0:43499fc489c6 | 235 | // Enable LED Write mode |
wim | 0:43499fc489c6 | 236 | _spi.write(_flip(PT6311_DATA_SET_CMD | PT6311_LED_WR | PT6311_ADDR_INC | PT6311_MODE_NORM)); // Data set cmd, normal mode, auto incr, write data |
wim | 0:43499fc489c6 | 237 | |
wim | 0:43499fc489c6 | 238 | _spi.write(_flip(leds & PT6311_LED_MSK)); // write LEDs in correct bitorder |
wim | 0:43499fc489c6 | 239 | |
wim | 0:43499fc489c6 | 240 | wait_us(1); |
wim | 0:43499fc489c6 | 241 | _cs=1; |
wim | 0:43499fc489c6 | 242 | |
wim | 0:43499fc489c6 | 243 | // Restore Data Write mode |
wim | 0:43499fc489c6 | 244 | _writeCmd(PT6311_DATA_SET_CMD, PT6311_DATA_WR | PT6311_ADDR_INC | PT6311_MODE_NORM); // Data set cmd, normal mode, auto incr, write data |
wim | 0:43499fc489c6 | 245 | } |
wim | 0:43499fc489c6 | 246 | |
wim | 0:43499fc489c6 | 247 | |
wim | 0:43499fc489c6 | 248 | |
wim | 0:43499fc489c6 | 249 | /** Helper to reverse all command or databits. The PT6311 expects LSB first, whereas SPI is MSB first |
wim | 0:43499fc489c6 | 250 | * @param char data |
wim | 0:43499fc489c6 | 251 | * @return bitreversed data |
wim | 0:43499fc489c6 | 252 | */ |
wim | 0:43499fc489c6 | 253 | char PT6311::_flip(char data) { |
wim | 0:43499fc489c6 | 254 | char value=0; |
wim | 0:43499fc489c6 | 255 | |
wim | 0:43499fc489c6 | 256 | if (data & 0x01) {value |= 0x80;} ; |
wim | 0:43499fc489c6 | 257 | if (data & 0x02) {value |= 0x40;} ; |
wim | 0:43499fc489c6 | 258 | if (data & 0x04) {value |= 0x20;} ; |
wim | 0:43499fc489c6 | 259 | if (data & 0x08) {value |= 0x10;} ; |
wim | 0:43499fc489c6 | 260 | if (data & 0x10) {value |= 0x08;} ; |
wim | 0:43499fc489c6 | 261 | if (data & 0x20) {value |= 0x04;} ; |
wim | 0:43499fc489c6 | 262 | if (data & 0x40) {value |= 0x02;} ; |
wim | 0:43499fc489c6 | 263 | if (data & 0x80) {value |= 0x01;} ; |
wim | 0:43499fc489c6 | 264 | return value; |
wim | 0:43499fc489c6 | 265 | } |
wim | 0:43499fc489c6 | 266 | |
wim | 0:43499fc489c6 | 267 | |
wim | 0:43499fc489c6 | 268 | /** Write command and parameter to PT6311 |
wim | 0:43499fc489c6 | 269 | * @param int cmd Command byte |
wim | 0:43499fc489c6 | 270 | * &Param int data Parameters for command |
wim | 0:43499fc489c6 | 271 | * @return none |
wim | 0:43499fc489c6 | 272 | */ |
wim | 0:43499fc489c6 | 273 | void PT6311::_writeCmd(int cmd, int data){ |
wim | 0:43499fc489c6 | 274 | |
wim | 0:43499fc489c6 | 275 | _cs=0; |
wim | 0:43499fc489c6 | 276 | wait_us(1); |
wim | 0:43499fc489c6 | 277 | |
wim | 0:43499fc489c6 | 278 | _spi.write(_flip( (cmd & PT6311_CMD_MSK) | (data & ~PT6311_CMD_MSK))); |
wim | 0:43499fc489c6 | 279 | |
wim | 0:43499fc489c6 | 280 | wait_us(1); |
wim | 0:43499fc489c6 | 281 | _cs=1; |
wim | 0:43499fc489c6 | 282 | |
wim | 0:43499fc489c6 | 283 | }; |
wim | 0:43499fc489c6 | 284 | |
wim | 0:43499fc489c6 | 285 | |
wim | 0:43499fc489c6 | 286 | |
wim | 0:43499fc489c6 | 287 | |
wim | 0:43499fc489c6 | 288 | #if (VFDEM2_TEST == 1) |
wim | 0:43499fc489c6 | 289 | /** Constructor for class for driving Princeton PT6311 VFD controller as used in VFDEM2 |
wim | 0:43499fc489c6 | 290 | * |
wim | 0:43499fc489c6 | 291 | * @brief Supports 12 Grids of 16 Segments and Icons (10 digits of 14 Segments plus some icons and another 2 Icon grids). |
wim | 0:43499fc489c6 | 292 | * Also supports a scanned keyboard of 7 keys and 1 LED. |
wim | 0:43499fc489c6 | 293 | * |
wim | 0:43499fc489c6 | 294 | * @param PinName mosi, miso, sclk, cs SPI bus pins |
wim | 0:43499fc489c6 | 295 | */ |
wim | 0:43499fc489c6 | 296 | PT6311_VFDEM2::PT6311_VFDEM2(PinName mosi, PinName miso, PinName sclk, PinName cs) : PT6311(mosi, miso, sclk, cs, Grid12_Seg16) { |
wim | 0:43499fc489c6 | 297 | _column = 0; |
wim | 0:43499fc489c6 | 298 | _columns = VFDEM2_NR_DIGITS; |
wim | 0:43499fc489c6 | 299 | } |
wim | 0:43499fc489c6 | 300 | |
wim | 0:43499fc489c6 | 301 | #if(0) |
wim | 0:43499fc489c6 | 302 | #if DOXYGEN_ONLY |
wim | 0:43499fc489c6 | 303 | /** Write a character to the LCD |
wim | 0:43499fc489c6 | 304 | * |
wim | 0:43499fc489c6 | 305 | * @param c The character to write to the display |
wim | 0:43499fc489c6 | 306 | */ |
wim | 0:43499fc489c6 | 307 | int putc(int c); |
wim | 0:43499fc489c6 | 308 | |
wim | 0:43499fc489c6 | 309 | /** Write a formatted string to the LCD |
wim | 0:43499fc489c6 | 310 | * |
wim | 0:43499fc489c6 | 311 | * @param format A printf-style format string, followed by the |
wim | 0:43499fc489c6 | 312 | * variables to use in formatting the string. |
wim | 0:43499fc489c6 | 313 | */ |
wim | 0:43499fc489c6 | 314 | int printf(const char* format, ...); |
wim | 0:43499fc489c6 | 315 | #endif |
wim | 0:43499fc489c6 | 316 | #endif |
wim | 0:43499fc489c6 | 317 | |
wim | 0:43499fc489c6 | 318 | /** Locate cursor to a screen column |
wim | 0:43499fc489c6 | 319 | * |
wim | 0:43499fc489c6 | 320 | * @param column The horizontal position from the left, indexed from 0 |
wim | 0:43499fc489c6 | 321 | */ |
wim | 0:43499fc489c6 | 322 | void PT6311_VFDEM2::locate(int column) { |
wim | 0:43499fc489c6 | 323 | //sanity check |
wim | 0:43499fc489c6 | 324 | if (column < 0) {column = 0;} |
wim | 0:43499fc489c6 | 325 | if (column > (_columns - 1)) {column = _columns - 1;} |
wim | 0:43499fc489c6 | 326 | |
wim | 0:43499fc489c6 | 327 | _column = column; |
wim | 0:43499fc489c6 | 328 | } |
wim | 0:43499fc489c6 | 329 | |
wim | 0:43499fc489c6 | 330 | |
wim | 0:43499fc489c6 | 331 | /** Number of screen columns |
wim | 0:43499fc489c6 | 332 | * |
wim | 0:43499fc489c6 | 333 | * @param none |
wim | 0:43499fc489c6 | 334 | * @return columns |
wim | 0:43499fc489c6 | 335 | */ |
wim | 0:43499fc489c6 | 336 | int PT6311_VFDEM2::columns() { |
wim | 0:43499fc489c6 | 337 | return _columns; |
wim | 0:43499fc489c6 | 338 | } |
wim | 0:43499fc489c6 | 339 | |
wim | 0:43499fc489c6 | 340 | |
wim | 0:43499fc489c6 | 341 | /** Clear the screen and locate to 0 |
wim | 0:43499fc489c6 | 342 | * @param bool clrAll Clear Icons also (default = false) |
wim | 0:43499fc489c6 | 343 | */ |
wim | 0:43499fc489c6 | 344 | void PT6311_VFDEM2::cls(bool clrAll) { |
wim | 0:43499fc489c6 | 345 | int idx; |
wim | 0:43499fc489c6 | 346 | |
wim | 0:43499fc489c6 | 347 | if (clrAll) { |
wim | 0:43499fc489c6 | 348 | //clear local buffer (including Icons) |
wim | 0:43499fc489c6 | 349 | for (idx=0; idx < (VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID); idx++) { |
wim | 0:43499fc489c6 | 350 | _displaybuffer[idx] = 0x00; |
wim | 0:43499fc489c6 | 351 | } |
wim | 0:43499fc489c6 | 352 | } |
wim | 0:43499fc489c6 | 353 | else { |
wim | 0:43499fc489c6 | 354 | //clear local buffer (preserving Icons) |
wim | 0:43499fc489c6 | 355 | for (int grd=0; grd < VFDEM2_NR_GRIDS; grd++) { |
wim | 0:43499fc489c6 | 356 | idx = grd * PT6311_BYTES_PER_GRID; // 3 bytes for every Grid |
wim | 0:43499fc489c6 | 357 | _displaybuffer[idx ] = _displaybuffer[idx ] & MASK_ICON_GRID[grd][0]; |
wim | 0:43499fc489c6 | 358 | _displaybuffer[idx + 1] = _displaybuffer[idx + 1] & MASK_ICON_GRID[grd][1]; |
wim | 0:43499fc489c6 | 359 | _displaybuffer[idx + 2] = _displaybuffer[idx + 2] & MASK_ICON_GRID[grd][2]; |
wim | 0:43499fc489c6 | 360 | } |
wim | 0:43499fc489c6 | 361 | } |
wim | 0:43499fc489c6 | 362 | |
wim | 0:43499fc489c6 | 363 | writeData(_displaybuffer, (VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID)); |
wim | 0:43499fc489c6 | 364 | |
wim | 0:43499fc489c6 | 365 | _column = 0; |
wim | 0:43499fc489c6 | 366 | } |
wim | 0:43499fc489c6 | 367 | |
wim | 0:43499fc489c6 | 368 | /** Set Icon |
wim | 0:43499fc489c6 | 369 | * |
wim | 0:43499fc489c6 | 370 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:43499fc489c6 | 371 | * @return none |
wim | 0:43499fc489c6 | 372 | */ |
wim | 0:43499fc489c6 | 373 | void PT6311_VFDEM2::setIcon(Icon icon) { |
wim | 0:43499fc489c6 | 374 | int addr, icn; |
wim | 0:43499fc489c6 | 375 | |
wim | 0:43499fc489c6 | 376 | icn = icon & 0xFFFF; |
wim | 0:43499fc489c6 | 377 | addr = (icon >> 24) & 0xFF; |
wim | 0:43499fc489c6 | 378 | addr = (addr - 1) * PT6311_BYTES_PER_GRID; // 3 Bytes for every Grid |
wim | 0:43499fc489c6 | 379 | |
wim | 0:43499fc489c6 | 380 | //Save char...and set bits for icon to write |
wim | 0:43499fc489c6 | 381 | _displaybuffer[addr ] = _displaybuffer[addr ] | LO(icn); |
wim | 0:43499fc489c6 | 382 | _displaybuffer[addr + 1] = _displaybuffer[addr + 1] | MD(icn); |
wim | 0:43499fc489c6 | 383 | _displaybuffer[addr + 2] = _displaybuffer[addr + 2] | HI(icn); |
wim | 0:43499fc489c6 | 384 | writeData(_displaybuffer, (VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID)); |
wim | 0:43499fc489c6 | 385 | } |
wim | 0:43499fc489c6 | 386 | |
wim | 0:43499fc489c6 | 387 | /** Clr Icon |
wim | 0:43499fc489c6 | 388 | * |
wim | 0:43499fc489c6 | 389 | * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs |
wim | 0:43499fc489c6 | 390 | * @return none |
wim | 0:43499fc489c6 | 391 | */ |
wim | 0:43499fc489c6 | 392 | void PT6311_VFDEM2::clrIcon(Icon icon) { |
wim | 0:43499fc489c6 | 393 | int addr, icn; |
wim | 0:43499fc489c6 | 394 | |
wim | 0:43499fc489c6 | 395 | icn = icon & 0xFFFF; |
wim | 0:43499fc489c6 | 396 | addr = (icon >> 24) & 0xFF; |
wim | 0:43499fc489c6 | 397 | addr = (addr -1 ) * PT6311_BYTES_PER_GRID; // 3 Bytes for every Grid |
wim | 0:43499fc489c6 | 398 | |
wim | 0:43499fc489c6 | 399 | //Save char...and clr bits for icon to write |
wim | 0:43499fc489c6 | 400 | _displaybuffer[addr ] = _displaybuffer[addr ] & ~LO(icn); |
wim | 0:43499fc489c6 | 401 | _displaybuffer[addr + 1] = _displaybuffer[addr + 1] & ~MD(icn); |
wim | 0:43499fc489c6 | 402 | _displaybuffer[addr + 2] = _displaybuffer[addr + 2] & ~HI(icn); |
wim | 0:43499fc489c6 | 403 | writeData(_displaybuffer, (VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID)); |
wim | 0:43499fc489c6 | 404 | } |
wim | 0:43499fc489c6 | 405 | |
wim | 0:43499fc489c6 | 406 | |
wim | 0:43499fc489c6 | 407 | /** Set User Defined Characters (UDC) |
wim | 0:43499fc489c6 | 408 | * |
wim | 0:43499fc489c6 | 409 | * @param unsigned char udc_idx The Index of the UDC (0..7) |
wim | 0:43499fc489c6 | 410 | * @param int udc_data The bitpattern for the UDC (16 bits) |
wim | 0:43499fc489c6 | 411 | */ |
wim | 0:43499fc489c6 | 412 | void PT6311_VFDEM2::setUDC(unsigned char udc_idx, int udc_data) { |
wim | 0:43499fc489c6 | 413 | |
wim | 0:43499fc489c6 | 414 | //Sanity check |
wim | 0:43499fc489c6 | 415 | if (udc_idx > (VFDEM2_NR_UDC-1)) { |
wim | 0:43499fc489c6 | 416 | return; |
wim | 0:43499fc489c6 | 417 | } |
wim | 0:43499fc489c6 | 418 | |
wim | 0:43499fc489c6 | 419 | // Mask out Icon bits? |
wim | 0:43499fc489c6 | 420 | _UDC_16S[udc_idx] = udc_data & 0xFFFF; |
wim | 0:43499fc489c6 | 421 | } |
wim | 0:43499fc489c6 | 422 | |
wim | 0:43499fc489c6 | 423 | /** Write a single character (Stream implementation) |
wim | 0:43499fc489c6 | 424 | */ |
wim | 0:43499fc489c6 | 425 | int PT6311_VFDEM2::_putc(int value) { |
wim | 0:43499fc489c6 | 426 | bool validChar = false; |
wim | 0:43499fc489c6 | 427 | short pattern = 0x0000; |
wim | 0:43499fc489c6 | 428 | int addr; |
wim | 0:43499fc489c6 | 429 | |
wim | 0:43499fc489c6 | 430 | if ((value == '\n') || (value == '\r')) { |
wim | 0:43499fc489c6 | 431 | //No character to write |
wim | 0:43499fc489c6 | 432 | validChar = false; |
wim | 0:43499fc489c6 | 433 | |
wim | 0:43499fc489c6 | 434 | //Update Cursor |
wim | 0:43499fc489c6 | 435 | _column = 0; |
wim | 0:43499fc489c6 | 436 | } |
wim | 0:43499fc489c6 | 437 | else if ((value >= 0) && (value < VFDEM2_NR_UDC)) { |
wim | 0:43499fc489c6 | 438 | //Character to write |
wim | 0:43499fc489c6 | 439 | validChar = true; |
wim | 0:43499fc489c6 | 440 | pattern = _UDC_16S[value]; |
wim | 0:43499fc489c6 | 441 | } |
wim | 0:43499fc489c6 | 442 | #if (SHOW_ASCII == 1) |
wim | 0:43499fc489c6 | 443 | //display all ASCII characters |
wim | 0:43499fc489c6 | 444 | else if ((value >= FONT_16S_START) && (value <= FONT_16S_END)) { |
wim | 0:43499fc489c6 | 445 | //Character to write |
wim | 0:43499fc489c6 | 446 | validChar = true; |
wim | 0:43499fc489c6 | 447 | pattern = FONT_16S[value - FONT_16S_START]; |
wim | 0:43499fc489c6 | 448 | } // else |
wim | 0:43499fc489c6 | 449 | #else |
wim | 0:43499fc489c6 | 450 | //display only digits and hex characters |
wim | 0:43499fc489c6 | 451 | else if (value == '-') { |
wim | 0:43499fc489c6 | 452 | //Character to write |
wim | 0:43499fc489c6 | 453 | validChar = true; |
wim | 0:43499fc489c6 | 454 | pattern = C16_MIN; |
wim | 0:43499fc489c6 | 455 | } |
wim | 0:43499fc489c6 | 456 | else if ((value >= (int)'0') && (value <= (int) '9')) { |
wim | 0:43499fc489c6 | 457 | //Character to write |
wim | 0:43499fc489c6 | 458 | validChar = true; |
wim | 0:43499fc489c6 | 459 | pattern = FONT_16S[value - (int) '0']; |
wim | 0:43499fc489c6 | 460 | } |
wim | 0:43499fc489c6 | 461 | else if ((value >= (int) 'A') && (value <= (int) 'F')) { |
wim | 0:43499fc489c6 | 462 | //Character to write |
wim | 0:43499fc489c6 | 463 | validChar = true; |
wim | 0:43499fc489c6 | 464 | pattern = FONT_16S[10 + value - (int) 'A']; |
wim | 0:43499fc489c6 | 465 | } |
wim | 0:43499fc489c6 | 466 | else if ((value >= (int) 'a') && (value <= (int) 'f')) { |
wim | 0:43499fc489c6 | 467 | //Character to write |
wim | 0:43499fc489c6 | 468 | validChar = true; |
wim | 0:43499fc489c6 | 469 | pattern = FONT_16S[10 + value - (int) 'a']; |
wim | 0:43499fc489c6 | 470 | } //else |
wim | 0:43499fc489c6 | 471 | #endif |
wim | 0:43499fc489c6 | 472 | if (validChar) { |
wim | 0:43499fc489c6 | 473 | //Character to write |
wim | 0:43499fc489c6 | 474 | |
wim | 0:43499fc489c6 | 475 | //Translate between _column and displaybuffer entries |
wim | 0:43499fc489c6 | 476 | //Note that the VFDEM2 has ten 14 Segment digits/grids. |
wim | 0:43499fc489c6 | 477 | //Some of these Grids also have icons that need to be preserved |
wim | 0:43499fc489c6 | 478 | //_column == 0 => Grid11 => addr = 30 |
wim | 0:43499fc489c6 | 479 | //_column == 1 => Grid10 => addr = 27 |
wim | 0:43499fc489c6 | 480 | // .... |
wim | 0:43499fc489c6 | 481 | //_column == 9 => Grid2 => addr = 3 |
wim | 0:43499fc489c6 | 482 | addr = (10 - _column) * PT6311_BYTES_PER_GRID; // 3 Bytes for every Grid; |
wim | 0:43499fc489c6 | 483 | |
wim | 0:43499fc489c6 | 484 | //Save icons...and set bits for character to write |
wim | 0:43499fc489c6 | 485 | _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[10 - _column][0]) | LO(pattern); |
wim | 0:43499fc489c6 | 486 | _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[10 - _column][1]) | MD(pattern); |
wim | 0:43499fc489c6 | 487 | // _displaybuffer[addr+2] = (_displaybuffer[addr+2] & MASK_ICON_GRID[10 - _column][2]) | HI(pattern); |
wim | 0:43499fc489c6 | 488 | |
wim | 0:43499fc489c6 | 489 | writeData(_displaybuffer, (VFDEM2_NR_GRIDS * PT6311_BYTES_PER_GRID)); |
wim | 0:43499fc489c6 | 490 | |
wim | 0:43499fc489c6 | 491 | //Update Cursor |
wim | 0:43499fc489c6 | 492 | _column++; |
wim | 0:43499fc489c6 | 493 | if (_column > (VFDEM2_NR_DIGITS - 1)) { |
wim | 0:43499fc489c6 | 494 | _column = 0; |
wim | 0:43499fc489c6 | 495 | } |
wim | 0:43499fc489c6 | 496 | |
wim | 0:43499fc489c6 | 497 | } // if validChar |
wim | 0:43499fc489c6 | 498 | |
wim | 0:43499fc489c6 | 499 | return value; |
wim | 0:43499fc489c6 | 500 | } |
wim | 0:43499fc489c6 | 501 | |
wim | 0:43499fc489c6 | 502 | // get a single character (Stream implementation) |
wim | 0:43499fc489c6 | 503 | int PT6311_VFDEM2::_getc() { |
wim | 0:43499fc489c6 | 504 | return -1; |
wim | 0:43499fc489c6 | 505 | } |
wim | 0:43499fc489c6 | 506 | #endif |