Library for Princeton PT6961 LED driver. Supports 6 digits @ 12 segments or 7 digits @ 11 segments. Also supports keyboard scanning of upto 30 keys. SPI interface.

Dependents:   mbed_PT6961

This LED driver is found in frontpanel controllers of consumer electronics such as DVD players. The added features such as the matrix keyboard scanning are useful in these applications.

Additional information is available on the component page here

Committer:
wim
Date:
Thu Jan 07 20:57:00 2016 +0000
Revision:
1:eb4758bba68a
Parent:
0:feebe8eca523
Child:
2:c6883ede8d8b
Added support for HR734 and V56S display print, Added Stream support for decimal and hex digits.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:feebe8eca523 1 /* mbed PT6961 Library, for Princeton PT6961 LED controller
wim 1:eb4758bba68a 2 * Copyright (c) 2015, v01: WH, Initial version (HR734)
wim 1:eb4758bba68a 3 * 2016, v02: WH, Added V56S, Added Stream support
wim 0:feebe8eca523 4 *
wim 0:feebe8eca523 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:feebe8eca523 6 * of this software and associated documentation files (the "Software"), to deal
wim 0:feebe8eca523 7 * in the Software without restriction, including without limitation the rights
wim 0:feebe8eca523 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:feebe8eca523 9 * copies of the Software, and to permit persons to whom the Software is
wim 0:feebe8eca523 10 * furnished to do so, subject to the following conditions:
wim 0:feebe8eca523 11 *
wim 0:feebe8eca523 12 * The above copyright notice and this permission notice shall be included in
wim 0:feebe8eca523 13 * all copies or substantial portions of the Software.
wim 0:feebe8eca523 14 *
wim 0:feebe8eca523 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:feebe8eca523 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:feebe8eca523 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:feebe8eca523 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:feebe8eca523 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:feebe8eca523 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:feebe8eca523 21 * THE SOFTWARE.
wim 0:feebe8eca523 22 */
wim 0:feebe8eca523 23 #include "mbed.h"
wim 0:feebe8eca523 24 #include "PT6961.h"
wim 0:feebe8eca523 25
wim 0:feebe8eca523 26 /** Constructor for class for driving Princeton PT6961 LED controller with SPI bus interface device.
wim 1:eb4758bba68a 27 * @brief Supports 6 Grids @ 12 Segments or 7 Grids @ 11 Segments. Also supports a scanned keyboard of upto 30 keys.
wim 0:feebe8eca523 28 *
wim 0:feebe8eca523 29 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 1:eb4758bba68a 30 * @param Mode selects either 6 Grids @ 12 Segments or 7 Grids @ 11 Segments (default)
wim 0:feebe8eca523 31 */
wim 0:feebe8eca523 32 PT6961::PT6961(PinName mosi, PinName miso, PinName sclk, PinName cs, Mode mode) : _spi(mosi,miso,sclk), _cs(cs), _mode(mode) {
wim 0:feebe8eca523 33
wim 0:feebe8eca523 34 _init();
wim 0:feebe8eca523 35 }
wim 0:feebe8eca523 36
wim 0:feebe8eca523 37 /** Init the SPI interface and the controller
wim 0:feebe8eca523 38 * @param none
wim 0:feebe8eca523 39 * @return none
wim 0:feebe8eca523 40 */
wim 0:feebe8eca523 41 void PT6961::_init(){
wim 0:feebe8eca523 42
wim 0:feebe8eca523 43 //init SPI
wim 0:feebe8eca523 44 _cs=1;
wim 0:feebe8eca523 45 _spi.format(8,3); //PT6961 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge)
wim 0:feebe8eca523 46 _spi.frequency(500000);
wim 0:feebe8eca523 47
wim 0:feebe8eca523 48 //init controller
wim 0:feebe8eca523 49 _writeCmd(PT6961_MODE_SET_CMD, _mode); // Mode set command
wim 0:feebe8eca523 50
wim 0:feebe8eca523 51 _display = PT6961_DSP_ON;
wim 0:feebe8eca523 52 _bright = PT6961_BRT_DEF;
wim 0:feebe8eca523 53 _writeCmd(PT6961_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:feebe8eca523 54
wim 0:feebe8eca523 55 _writeCmd(PT6961_DATA_SET_CMD, PT6961_DATA_WR | PT6961_ADDR_INC | PT6961_MODE_NORM); // Data set cmd, normal mode, auto incr, write data
wim 0:feebe8eca523 56 }
wim 0:feebe8eca523 57
wim 0:feebe8eca523 58
wim 0:feebe8eca523 59 /** Clear the screen and locate to 0
wim 0:feebe8eca523 60 */
wim 0:feebe8eca523 61 void PT6961::cls() {
wim 0:feebe8eca523 62
wim 0:feebe8eca523 63 _cs=0;
wim 0:feebe8eca523 64 wait_us(1);
wim 0:feebe8eca523 65 _spi.write(_flip(PT6961_ADDR_SET_CMD | 0x00)); // Address set cmd, 0
wim 0:feebe8eca523 66
wim 0:feebe8eca523 67 for (int cnt=0; cnt<PT6961_DISPLAY_MEM; cnt++) {
wim 0:feebe8eca523 68 _spi.write(0x00); // data
wim 0:feebe8eca523 69 }
wim 0:feebe8eca523 70
wim 0:feebe8eca523 71 wait_us(1);
wim 0:feebe8eca523 72 _cs=1;
wim 0:feebe8eca523 73 }
wim 0:feebe8eca523 74
wim 0:feebe8eca523 75 /** Set Brightness
wim 0:feebe8eca523 76 *
wim 0:feebe8eca523 77 * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle)
wim 0:feebe8eca523 78 * @return none
wim 0:feebe8eca523 79 */
wim 0:feebe8eca523 80 void PT6961::setBrightness(char brightness){
wim 0:feebe8eca523 81
wim 0:feebe8eca523 82 _bright = brightness & PT6961_BRT_MSK; // mask invalid bits
wim 0:feebe8eca523 83
wim 0:feebe8eca523 84 _writeCmd(PT6961_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:feebe8eca523 85 }
wim 0:feebe8eca523 86
wim 0:feebe8eca523 87 /** Set the Display mode On/off
wim 0:feebe8eca523 88 *
wim 0:feebe8eca523 89 * @param bool display mode
wim 0:feebe8eca523 90 */
wim 0:feebe8eca523 91 void PT6961::setDisplay(bool on) {
wim 0:feebe8eca523 92
wim 0:feebe8eca523 93 if (on) {
wim 0:feebe8eca523 94 _display = PT6961_DSP_ON;
wim 0:feebe8eca523 95 }
wim 0:feebe8eca523 96 else {
wim 0:feebe8eca523 97 _display = PT6961_DSP_OFF;
wim 0:feebe8eca523 98 }
wim 0:feebe8eca523 99
wim 0:feebe8eca523 100 _writeCmd(PT6961_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness
wim 0:feebe8eca523 101 }
wim 0:feebe8eca523 102
wim 0:feebe8eca523 103 /** Write databyte to PT6961
wim 0:feebe8eca523 104 * @param int address display memory location to write byte
wim 0:feebe8eca523 105 * @param char data byte written at given address
wim 0:feebe8eca523 106 * @return none
wim 0:feebe8eca523 107 */
wim 0:feebe8eca523 108 void PT6961::writeData(int address, char data) {
wim 0:feebe8eca523 109 _cs=0;
wim 0:feebe8eca523 110 wait_us(1);
wim 0:feebe8eca523 111 _spi.write(_flip(PT6961_ADDR_SET_CMD | (address & PT6961_ADDR_MSK))); // Set Address cmd
wim 0:feebe8eca523 112
wim 0:feebe8eca523 113 _spi.write(_flip(data)); // data
wim 0:feebe8eca523 114
wim 0:feebe8eca523 115 wait_us(1);
wim 0:feebe8eca523 116 _cs=1;
wim 0:feebe8eca523 117 }
wim 0:feebe8eca523 118
wim 0:feebe8eca523 119 /** Write Display datablock to PT6961
wim 0:feebe8eca523 120 * @param DisplayData_t data Array of PT6961_DISPLAY_MEM (=14) bytes for displaydata (starting at address 0)
wim 0:feebe8eca523 121 * @param length number bytes to write (valide range 0..PT6961_DISPLAY_MEM (=14), starting at address 0)
wim 0:feebe8eca523 122 * @return none
wim 0:feebe8eca523 123 */
wim 0:feebe8eca523 124 void PT6961::writeData(DisplayData_t data, int length) {
wim 0:feebe8eca523 125 _cs=0;
wim 0:feebe8eca523 126 wait_us(1);
wim 0:feebe8eca523 127 _spi.write(_flip(PT6961_ADDR_SET_CMD | 0x00)); // Set Address at 0
wim 0:feebe8eca523 128
wim 0:feebe8eca523 129 // sanity check
wim 0:feebe8eca523 130 if (length < 0) {length = 0;}
wim 0:feebe8eca523 131 if (length > PT6961_DISPLAY_MEM) {length = PT6961_DISPLAY_MEM;}
wim 0:feebe8eca523 132
wim 0:feebe8eca523 133 // for (int idx=0; idx<PT6961_DISPLAY_MEM; idx++) {
wim 0:feebe8eca523 134 for (int idx=0; idx<length; idx++) {
wim 0:feebe8eca523 135 _spi.write(_flip(data[idx])); // data
wim 0:feebe8eca523 136 }
wim 0:feebe8eca523 137
wim 0:feebe8eca523 138 wait_us(1);
wim 0:feebe8eca523 139 _cs=1;
wim 0:feebe8eca523 140 }
wim 0:feebe8eca523 141
wim 0:feebe8eca523 142
wim 0:feebe8eca523 143 /** Read keydata block from PT6961
wim 0:feebe8eca523 144 * @param *keydata Ptr to Array of PT6961_KEY_MEM (=5) bytes for keydata
wim 0:feebe8eca523 145 * @return bool keypress True when at least one key was pressed
wim 0:feebe8eca523 146 *
wim 0:feebe8eca523 147 * Note: Due to the hardware configuration the PT6961 key matrix scanner will detect multiple keys pressed at same time,
wim 0:feebe8eca523 148 * but this may also result in some spurious keys being set in keypress data array.
wim 0:feebe8eca523 149 * It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting.
wim 0:feebe8eca523 150 */
wim 0:feebe8eca523 151 bool PT6961::getKeys(KeyData_t *keydata) {
wim 0:feebe8eca523 152 int keypress = 0;
wim 0:feebe8eca523 153 char data;
wim 0:feebe8eca523 154
wim 0:feebe8eca523 155 // Read keys
wim 0:feebe8eca523 156 _cs=0;
wim 0:feebe8eca523 157 wait_us(1);
wim 0:feebe8eca523 158
wim 0:feebe8eca523 159 // Enable Key Read mode
wim 0:feebe8eca523 160 _spi.write(_flip(PT6961_DATA_SET_CMD | PT6961_KEY_RD | PT6961_ADDR_INC | PT6961_MODE_NORM)); // Data set cmd, normal mode, auto incr, read data
wim 0:feebe8eca523 161
wim 0:feebe8eca523 162 for (int idx=0; idx < PT6961_KEY_MEM; idx++) {
wim 0:feebe8eca523 163 data = _flip(_spi.write(0xFF)); // read keys and correct bitorder
wim 0:feebe8eca523 164
wim 0:feebe8eca523 165 if (data != 0) { // Check for any pressed key
wim 0:feebe8eca523 166 for (int bit=0; bit < PT6961_KEY_BITS; bit++) {
wim 0:feebe8eca523 167 if (data & (1 << bit)) {keypress++;} // Test all significant bits
wim 0:feebe8eca523 168 }
wim 0:feebe8eca523 169 }
wim 0:feebe8eca523 170
wim 0:feebe8eca523 171 (*keydata)[idx] = data; // Store keydata after correcting bitorder
wim 0:feebe8eca523 172 }
wim 0:feebe8eca523 173
wim 0:feebe8eca523 174 wait_us(1);
wim 0:feebe8eca523 175 _cs=1;
wim 0:feebe8eca523 176
wim 0:feebe8eca523 177 // Restore Data Write mode
wim 0:feebe8eca523 178 _writeCmd(PT6961_DATA_SET_CMD, PT6961_DATA_WR | PT6961_ADDR_INC | PT6961_MODE_NORM); // Data set cmd, normal mode, auto incr, write data
wim 0:feebe8eca523 179
wim 0:feebe8eca523 180 #if(1)
wim 0:feebe8eca523 181 // Dismiss multiple keypresses at same time
wim 0:feebe8eca523 182 return (keypress == 1);
wim 0:feebe8eca523 183 #else
wim 0:feebe8eca523 184 // Allow multiple keypress and accept possible spurious keys
wim 0:feebe8eca523 185 return (keypress > 0);
wim 0:feebe8eca523 186 #endif
wim 0:feebe8eca523 187 }
wim 0:feebe8eca523 188
wim 0:feebe8eca523 189
wim 0:feebe8eca523 190 /** Helper to reverse all command or databits. The PT6961 expects LSB first, whereas SPI is MSB first
wim 0:feebe8eca523 191 * @param char data
wim 0:feebe8eca523 192 * @return bitreversed data
wim 0:feebe8eca523 193 */
wim 0:feebe8eca523 194 char PT6961::_flip(char data) {
wim 0:feebe8eca523 195 char value=0;
wim 0:feebe8eca523 196
wim 0:feebe8eca523 197 if (data & 0x01) {value |= 0x80;} ;
wim 0:feebe8eca523 198 if (data & 0x02) {value |= 0x40;} ;
wim 0:feebe8eca523 199 if (data & 0x04) {value |= 0x20;} ;
wim 0:feebe8eca523 200 if (data & 0x08) {value |= 0x10;} ;
wim 0:feebe8eca523 201 if (data & 0x10) {value |= 0x08;} ;
wim 0:feebe8eca523 202 if (data & 0x20) {value |= 0x04;} ;
wim 0:feebe8eca523 203 if (data & 0x40) {value |= 0x02;} ;
wim 0:feebe8eca523 204 if (data & 0x80) {value |= 0x01;} ;
wim 0:feebe8eca523 205 return value;
wim 0:feebe8eca523 206 }
wim 0:feebe8eca523 207
wim 0:feebe8eca523 208
wim 0:feebe8eca523 209 /** Write command and parameter to PT6961
wim 0:feebe8eca523 210 * @param int cmd Command byte
wim 0:feebe8eca523 211 * &Param int data Parameters for command
wim 0:feebe8eca523 212 * @return none
wim 0:feebe8eca523 213 */
wim 0:feebe8eca523 214 void PT6961::_writeCmd(int cmd, int data){
wim 0:feebe8eca523 215
wim 0:feebe8eca523 216 _cs=0;
wim 0:feebe8eca523 217 wait_us(1);
wim 0:feebe8eca523 218 // _spi.write(_flip( (cmd & 0xF0) | (data & 0x0F)));
wim 0:feebe8eca523 219 _spi.write(_flip( (cmd & PT6961_CMD_MSK) | (data & ~PT6961_CMD_MSK)));
wim 0:feebe8eca523 220
wim 0:feebe8eca523 221 wait_us(1);
wim 0:feebe8eca523 222 _cs=1;
wim 0:feebe8eca523 223 }
wim 0:feebe8eca523 224
wim 0:feebe8eca523 225
wim 1:eb4758bba68a 226
wim 1:eb4758bba68a 227 #if(HR734_TEST == 1)
wim 1:eb4758bba68a 228 // Derived class for PT6961 used in HR734 display unit
wim 1:eb4758bba68a 229 //
wim 1:eb4758bba68a 230 #include "Font_7Seg.h"
wim 1:eb4758bba68a 231
wim 1:eb4758bba68a 232 /** Constructor for class for driving PT6961 LED controller as used in HR734
wim 1:eb4758bba68a 233 *
wim 1:eb4758bba68a 234 * @brief Supports 5 Digits of 7 Segments + some additional segments + Icons. Also supports a scanned keyboard of 10 keys.
wim 1:eb4758bba68a 235 *
wim 1:eb4758bba68a 236 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 1:eb4758bba68a 237 */
wim 1:eb4758bba68a 238 PT6961_HR734::PT6961_HR734(PinName mosi, PinName miso, PinName sclk, PinName cs) : PT6961(mosi, miso, sclk, cs) {
wim 1:eb4758bba68a 239 _column = 0;
wim 1:eb4758bba68a 240 _columns = HR734_NR_DIGITS;
wim 1:eb4758bba68a 241 }
wim 1:eb4758bba68a 242
wim 1:eb4758bba68a 243 #if(0)
wim 1:eb4758bba68a 244 #if DOXYGEN_ONLY
wim 1:eb4758bba68a 245 /** Write a character to the Display
wim 1:eb4758bba68a 246 *
wim 1:eb4758bba68a 247 * @param c The character to write to the display
wim 1:eb4758bba68a 248 */
wim 1:eb4758bba68a 249 int putc(int c);
wim 1:eb4758bba68a 250
wim 1:eb4758bba68a 251 /** Write a formatted string to the Display
wim 1:eb4758bba68a 252 *
wim 1:eb4758bba68a 253 * @param format A printf-style format string, followed by the
wim 1:eb4758bba68a 254 * variables to use in formatting the string.
wim 1:eb4758bba68a 255 */
wim 1:eb4758bba68a 256 int printf(const char* format, ...);
wim 1:eb4758bba68a 257 #endif
wim 1:eb4758bba68a 258 #endif
wim 1:eb4758bba68a 259
wim 1:eb4758bba68a 260 /** Locate cursor to a screen column
wim 1:eb4758bba68a 261 *
wim 1:eb4758bba68a 262 * @param column The horizontal position from the left, indexed from 0
wim 1:eb4758bba68a 263 */
wim 1:eb4758bba68a 264 void PT6961_HR734::locate(int column) {
wim 1:eb4758bba68a 265 //sanity check
wim 1:eb4758bba68a 266 if (column < 0) {column = 0;}
wim 1:eb4758bba68a 267 if (column > (_columns - 1)) {column = _columns - 1;}
wim 1:eb4758bba68a 268
wim 1:eb4758bba68a 269 _column = column;
wim 1:eb4758bba68a 270 }
wim 1:eb4758bba68a 271
wim 1:eb4758bba68a 272
wim 1:eb4758bba68a 273 /** Number of screen columns
wim 1:eb4758bba68a 274 *
wim 1:eb4758bba68a 275 * @param none
wim 1:eb4758bba68a 276 * @return columns
wim 1:eb4758bba68a 277 */
wim 1:eb4758bba68a 278 int PT6961_HR734::columns() {
wim 1:eb4758bba68a 279 return _columns;
wim 1:eb4758bba68a 280 }
wim 1:eb4758bba68a 281
wim 1:eb4758bba68a 282
wim 1:eb4758bba68a 283 /** Clear the screen and locate to 0
wim 1:eb4758bba68a 284 * @param bool clrAll Clear Icons also (default = false)
wim 1:eb4758bba68a 285 */
wim 1:eb4758bba68a 286 void PT6961_HR734::cls(bool clrAll) {
wim 1:eb4758bba68a 287
wim 1:eb4758bba68a 288 if (clrAll) {
wim 1:eb4758bba68a 289 //clear local buffer (including Icons)
wim 1:eb4758bba68a 290 for (int idx=0; idx < (HR734_NR_GRIDS << 1); idx++) {
wim 1:eb4758bba68a 291 _displaybuffer[idx] = 0x00;
wim 1:eb4758bba68a 292 }
wim 1:eb4758bba68a 293 }
wim 1:eb4758bba68a 294 else {
wim 1:eb4758bba68a 295 //clear local buffer (preserving Icons)
wim 1:eb4758bba68a 296 for (int idx=0; idx < HR734_NR_GRIDS; idx++) {
wim 1:eb4758bba68a 297 _displaybuffer[(idx<<1)] = _displaybuffer[(idx<<1)] & MASK_ICON_GRID[idx][0];
wim 1:eb4758bba68a 298 _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
wim 1:eb4758bba68a 299 }
wim 1:eb4758bba68a 300 }
wim 1:eb4758bba68a 301
wim 1:eb4758bba68a 302 writeData(_displaybuffer, (HR734_NR_GRIDS*2));
wim 1:eb4758bba68a 303
wim 1:eb4758bba68a 304 _column = 0;
wim 1:eb4758bba68a 305 }
wim 1:eb4758bba68a 306
wim 1:eb4758bba68a 307 /** Set Icon
wim 1:eb4758bba68a 308 *
wim 1:eb4758bba68a 309 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:eb4758bba68a 310 * @return none
wim 1:eb4758bba68a 311 */
wim 1:eb4758bba68a 312 void PT6961_HR734::setIcon(Icon icon) {
wim 1:eb4758bba68a 313 int addr, icn;
wim 1:eb4758bba68a 314
wim 1:eb4758bba68a 315 icn = icon & 0xFFFF;
wim 1:eb4758bba68a 316 addr = (icon >> 24) & 0xFF;
wim 1:eb4758bba68a 317 addr = (addr - 1) << 1;
wim 1:eb4758bba68a 318
wim 1:eb4758bba68a 319 //Save char...and set bits for icon to write
wim 1:eb4758bba68a 320 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 1:eb4758bba68a 321 _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);
wim 1:eb4758bba68a 322 writeData(_displaybuffer, (HR734_NR_GRIDS*2));
wim 1:eb4758bba68a 323 }
wim 1:eb4758bba68a 324
wim 1:eb4758bba68a 325 /** Clr Icon
wim 1:eb4758bba68a 326 *
wim 1:eb4758bba68a 327 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:eb4758bba68a 328 * @return none
wim 1:eb4758bba68a 329 */
wim 1:eb4758bba68a 330 void PT6961_HR734::clrIcon(Icon icon) {
wim 1:eb4758bba68a 331 int addr, icn;
wim 1:eb4758bba68a 332
wim 1:eb4758bba68a 333 icn = icon & 0xFFFF;
wim 1:eb4758bba68a 334 addr = (icon >> 24) & 0xFF;
wim 1:eb4758bba68a 335 addr = (addr - 1) << 1;
wim 1:eb4758bba68a 336
wim 1:eb4758bba68a 337 //Save char...and clr bits for icon to write
wim 1:eb4758bba68a 338 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 1:eb4758bba68a 339 _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);
wim 1:eb4758bba68a 340 writeData(_displaybuffer, (HR734_NR_GRIDS*2));
wim 1:eb4758bba68a 341 }
wim 1:eb4758bba68a 342
wim 1:eb4758bba68a 343
wim 1:eb4758bba68a 344 /** Set User Defined Characters (UDC)
wim 1:eb4758bba68a 345 *
wim 1:eb4758bba68a 346 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 1:eb4758bba68a 347 * @param int udc_data The bitpattern for the UDC (16 bits)
wim 1:eb4758bba68a 348 */
wim 1:eb4758bba68a 349 void PT6961_HR734::setUDC(unsigned char udc_idx, int udc_data) {
wim 1:eb4758bba68a 350
wim 1:eb4758bba68a 351 //Sanity check
wim 1:eb4758bba68a 352 if (udc_idx > (HR734_NR_UDC-1)) {
wim 1:eb4758bba68a 353 return;
wim 1:eb4758bba68a 354 }
wim 1:eb4758bba68a 355 // Mask out Icon bits?
wim 1:eb4758bba68a 356
wim 1:eb4758bba68a 357 _UDC_7S[udc_idx] = udc_data;
wim 1:eb4758bba68a 358 }
wim 1:eb4758bba68a 359
wim 1:eb4758bba68a 360
wim 1:eb4758bba68a 361 /** Write a single character (Stream implementation)
wim 1:eb4758bba68a 362 */
wim 1:eb4758bba68a 363 int PT6961_HR734::_putc(int value) {
wim 1:eb4758bba68a 364 int addr;
wim 1:eb4758bba68a 365 bool validChar = false;
wim 1:eb4758bba68a 366 short pattern = 0x0000;
wim 1:eb4758bba68a 367
wim 1:eb4758bba68a 368 if ((value == '\n') || (value == '\r')) {
wim 1:eb4758bba68a 369 //No character to write
wim 1:eb4758bba68a 370 validChar = false;
wim 1:eb4758bba68a 371
wim 1:eb4758bba68a 372 //Update Cursor
wim 1:eb4758bba68a 373 _column = 0;
wim 1:eb4758bba68a 374 }
wim 1:eb4758bba68a 375 else if (value == '-') {
wim 1:eb4758bba68a 376 //No character to write
wim 1:eb4758bba68a 377 validChar = true;
wim 1:eb4758bba68a 378 pattern = C7_MIN;
wim 1:eb4758bba68a 379 }
wim 1:eb4758bba68a 380 else if ((value >= 0) && (value < HR734_NR_UDC)) {
wim 1:eb4758bba68a 381 //Character to write
wim 1:eb4758bba68a 382 validChar = true;
wim 1:eb4758bba68a 383 pattern = _UDC_7S[value];
wim 1:eb4758bba68a 384 }
wim 1:eb4758bba68a 385 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 1:eb4758bba68a 386 //Character to write
wim 1:eb4758bba68a 387 validChar = true;
wim 1:eb4758bba68a 388 pattern = FONT_7S[value - (int) '0'];
wim 1:eb4758bba68a 389 }
wim 1:eb4758bba68a 390 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 1:eb4758bba68a 391 //Character to write
wim 1:eb4758bba68a 392 validChar = true;
wim 1:eb4758bba68a 393 pattern = FONT_7S[10 + value - (int) 'A'];
wim 1:eb4758bba68a 394 }
wim 1:eb4758bba68a 395 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 1:eb4758bba68a 396 //Character to write
wim 1:eb4758bba68a 397 validChar = true;
wim 1:eb4758bba68a 398 pattern = FONT_7S[10 + value - (int) 'a'];
wim 1:eb4758bba68a 399 } //else
wim 1:eb4758bba68a 400
wim 1:eb4758bba68a 401 if (validChar) {
wim 1:eb4758bba68a 402 //Character to write
wim 1:eb4758bba68a 403
wim 1:eb4758bba68a 404 //Translate between _column and displaybuffer entries
wim 1:eb4758bba68a 405 addr = _column << 1;
wim 1:eb4758bba68a 406
wim 1:eb4758bba68a 407 //Save icons...and set bits for character to write
wim 1:eb4758bba68a 408 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column][0]) | LO(pattern);
wim 1:eb4758bba68a 409 _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][1]) | HI(pattern);
wim 1:eb4758bba68a 410
wim 1:eb4758bba68a 411 writeData(_displaybuffer, (HR734_NR_GRIDS*2));
wim 1:eb4758bba68a 412
wim 1:eb4758bba68a 413 //Update Cursor
wim 1:eb4758bba68a 414 _column++;
wim 1:eb4758bba68a 415 if (_column > (HR734_NR_DIGITS - 1)) {
wim 1:eb4758bba68a 416 _column = 0;
wim 1:eb4758bba68a 417 }
wim 1:eb4758bba68a 418
wim 1:eb4758bba68a 419 } // if validChar
wim 1:eb4758bba68a 420
wim 1:eb4758bba68a 421 return value;
wim 1:eb4758bba68a 422 }
wim 1:eb4758bba68a 423
wim 1:eb4758bba68a 424
wim 1:eb4758bba68a 425 // get a single character (Stream implementation)
wim 1:eb4758bba68a 426 int PT6961_HR734::_getc() {
wim 1:eb4758bba68a 427 return -1;
wim 1:eb4758bba68a 428 }
wim 1:eb4758bba68a 429
wim 1:eb4758bba68a 430 #endif
wim 1:eb4758bba68a 431
wim 1:eb4758bba68a 432
wim 1:eb4758bba68a 433
wim 1:eb4758bba68a 434 #if(V56S_TEST == 1)
wim 1:eb4758bba68a 435 // Derived class for PT6961 used in V56S display unit
wim 1:eb4758bba68a 436 //
wim 1:eb4758bba68a 437 #include "Font_7Seg.h"
wim 1:eb4758bba68a 438
wim 1:eb4758bba68a 439 /** Constructor for class for driving PT6961 LED controller as used in V56S
wim 1:eb4758bba68a 440 *
wim 1:eb4758bba68a 441 * @brief Supports 5 Digits of 7 Segments + some additional segments + Icons. Also supports a scanned keyboard of 5 keys.
wim 1:eb4758bba68a 442 *
wim 1:eb4758bba68a 443 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 1:eb4758bba68a 444 */
wim 1:eb4758bba68a 445 PT6961_V56S::PT6961_V56S(PinName mosi, PinName miso, PinName sclk, PinName cs) : PT6961(mosi, miso, sclk, cs) {
wim 1:eb4758bba68a 446 _column = 0;
wim 1:eb4758bba68a 447 _columns = V56S_NR_DIGITS;
wim 1:eb4758bba68a 448 }
wim 1:eb4758bba68a 449
wim 1:eb4758bba68a 450 #if(0)
wim 1:eb4758bba68a 451 #if DOXYGEN_ONLY
wim 1:eb4758bba68a 452 /** Write a character to the Display
wim 1:eb4758bba68a 453 *
wim 1:eb4758bba68a 454 * @param c The character to write to the display
wim 1:eb4758bba68a 455 */
wim 1:eb4758bba68a 456 int putc(int c);
wim 1:eb4758bba68a 457
wim 1:eb4758bba68a 458 /** Write a formatted string to the Display
wim 1:eb4758bba68a 459 *
wim 1:eb4758bba68a 460 * @param format A printf-style format string, followed by the
wim 1:eb4758bba68a 461 * variables to use in formatting the string.
wim 1:eb4758bba68a 462 */
wim 1:eb4758bba68a 463 int printf(const char* format, ...);
wim 1:eb4758bba68a 464 #endif
wim 1:eb4758bba68a 465 #endif
wim 1:eb4758bba68a 466
wim 1:eb4758bba68a 467 /** Locate cursor to a screen column
wim 1:eb4758bba68a 468 *
wim 1:eb4758bba68a 469 * @param column The horizontal position from the left, indexed from 0
wim 1:eb4758bba68a 470 */
wim 1:eb4758bba68a 471 void PT6961_V56S::locate(int column) {
wim 1:eb4758bba68a 472 //sanity check
wim 1:eb4758bba68a 473 if (column < 0) {column = 0;}
wim 1:eb4758bba68a 474 if (column > (_columns - 1)) {column = _columns - 1;}
wim 1:eb4758bba68a 475
wim 1:eb4758bba68a 476 _column = column;
wim 1:eb4758bba68a 477 }
wim 1:eb4758bba68a 478
wim 1:eb4758bba68a 479
wim 1:eb4758bba68a 480 /** Number of screen columns
wim 1:eb4758bba68a 481 *
wim 1:eb4758bba68a 482 * @param none
wim 1:eb4758bba68a 483 * @return columns
wim 1:eb4758bba68a 484 */
wim 1:eb4758bba68a 485 int PT6961_V56S::columns() {
wim 1:eb4758bba68a 486 return _columns;
wim 1:eb4758bba68a 487 }
wim 1:eb4758bba68a 488
wim 1:eb4758bba68a 489
wim 1:eb4758bba68a 490 /** Clear the screen and locate to 0
wim 1:eb4758bba68a 491 * @param bool clrAll Clear Icons also (default = false)
wim 1:eb4758bba68a 492 */
wim 1:eb4758bba68a 493 void PT6961_V56S::cls(bool clrAll) {
wim 1:eb4758bba68a 494
wim 1:eb4758bba68a 495 if (clrAll) {
wim 1:eb4758bba68a 496 //clear local buffer (including Icons)
wim 1:eb4758bba68a 497 for (int idx=0; idx < (V56S_NR_GRIDS << 1); idx++) {
wim 1:eb4758bba68a 498 _displaybuffer[idx] = 0x00;
wim 1:eb4758bba68a 499 }
wim 1:eb4758bba68a 500 }
wim 1:eb4758bba68a 501 else {
wim 1:eb4758bba68a 502 //clear local buffer (preserving Icons)
wim 1:eb4758bba68a 503 for (int idx=0; idx < V56S_NR_GRIDS; idx++) {
wim 1:eb4758bba68a 504 _displaybuffer[(idx<<1)] = _displaybuffer[(idx<<1)] & MASK_ICON_GRID[idx][0];
wim 1:eb4758bba68a 505 _displaybuffer[(idx<<1) + 1] = _displaybuffer[(idx<<1) + 1] & MASK_ICON_GRID[idx][1];
wim 1:eb4758bba68a 506 }
wim 1:eb4758bba68a 507 }
wim 1:eb4758bba68a 508
wim 1:eb4758bba68a 509 writeData(_displaybuffer, (V56S_NR_GRIDS*2));
wim 1:eb4758bba68a 510
wim 1:eb4758bba68a 511 _column = 0;
wim 1:eb4758bba68a 512 }
wim 1:eb4758bba68a 513
wim 1:eb4758bba68a 514 /** Set Icon
wim 1:eb4758bba68a 515 *
wim 1:eb4758bba68a 516 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:eb4758bba68a 517 * @return none
wim 1:eb4758bba68a 518 */
wim 1:eb4758bba68a 519 void PT6961_V56S::setIcon(Icon icon) {
wim 1:eb4758bba68a 520 int addr, icn;
wim 1:eb4758bba68a 521
wim 1:eb4758bba68a 522 icn = icon & 0xFFFF;
wim 1:eb4758bba68a 523 addr = (icon >> 24) & 0xFF;
wim 1:eb4758bba68a 524 addr = (addr - 1) << 1;
wim 1:eb4758bba68a 525
wim 1:eb4758bba68a 526 //Save char...and set bits for icon to write
wim 1:eb4758bba68a 527 _displaybuffer[addr] = _displaybuffer[addr] | LO(icn);
wim 1:eb4758bba68a 528 _displaybuffer[addr+1] = _displaybuffer[addr+1] | HI(icn);
wim 1:eb4758bba68a 529 writeData(_displaybuffer, (V56S_NR_GRIDS*2));
wim 1:eb4758bba68a 530 }
wim 1:eb4758bba68a 531
wim 1:eb4758bba68a 532 /** Clr Icon
wim 1:eb4758bba68a 533 *
wim 1:eb4758bba68a 534 * @param Icon icon Enums Icon has Grid position encoded in 8 MSBs, Icon pattern encoded in 16 LSBs
wim 1:eb4758bba68a 535 * @return none
wim 1:eb4758bba68a 536 */
wim 1:eb4758bba68a 537 void PT6961_V56S::clrIcon(Icon icon) {
wim 1:eb4758bba68a 538 int addr, icn;
wim 1:eb4758bba68a 539
wim 1:eb4758bba68a 540 icn = icon & 0xFFFF;
wim 1:eb4758bba68a 541 addr = (icon >> 24) & 0xFF;
wim 1:eb4758bba68a 542 addr = (addr - 1) << 1;
wim 1:eb4758bba68a 543
wim 1:eb4758bba68a 544 //Save char...and clr bits for icon to write
wim 1:eb4758bba68a 545 _displaybuffer[addr] = _displaybuffer[addr] & ~LO(icn);
wim 1:eb4758bba68a 546 _displaybuffer[addr+1] = _displaybuffer[addr+1] & ~HI(icn);
wim 1:eb4758bba68a 547 writeData(_displaybuffer, (V56S_NR_GRIDS*2));
wim 1:eb4758bba68a 548 }
wim 1:eb4758bba68a 549
wim 1:eb4758bba68a 550
wim 1:eb4758bba68a 551 /** Set User Defined Characters (UDC)
wim 1:eb4758bba68a 552 *
wim 1:eb4758bba68a 553 * @param unsigned char udc_idx The Index of the UDC (0..7)
wim 1:eb4758bba68a 554 * @param int udc_data The bitpattern for the UDC (16 bits)
wim 1:eb4758bba68a 555 */
wim 1:eb4758bba68a 556 void PT6961_V56S::setUDC(unsigned char udc_idx, int udc_data) {
wim 1:eb4758bba68a 557
wim 1:eb4758bba68a 558 //Sanity check
wim 1:eb4758bba68a 559 if (udc_idx > (V56S_NR_UDC-1)) {
wim 1:eb4758bba68a 560 return;
wim 1:eb4758bba68a 561 }
wim 1:eb4758bba68a 562 // Mask out Icon bits?
wim 1:eb4758bba68a 563
wim 1:eb4758bba68a 564 _UDC_7S[udc_idx] = udc_data;
wim 1:eb4758bba68a 565 }
wim 1:eb4758bba68a 566
wim 1:eb4758bba68a 567
wim 1:eb4758bba68a 568 /** Write a single character (Stream implementation)
wim 1:eb4758bba68a 569 */
wim 1:eb4758bba68a 570 int PT6961_V56S::_putc(int value) {
wim 1:eb4758bba68a 571 int addr;
wim 1:eb4758bba68a 572 bool validChar = false;
wim 1:eb4758bba68a 573 short pattern = 0x0000;
wim 1:eb4758bba68a 574
wim 1:eb4758bba68a 575 if ((value == '\n') || (value == '\r')) {
wim 1:eb4758bba68a 576 //No character to write
wim 1:eb4758bba68a 577 validChar = false;
wim 1:eb4758bba68a 578
wim 1:eb4758bba68a 579 //Update Cursor
wim 1:eb4758bba68a 580 _column = 0;
wim 1:eb4758bba68a 581 }
wim 1:eb4758bba68a 582 else if (value == '-') {
wim 1:eb4758bba68a 583 //No character to write
wim 1:eb4758bba68a 584 validChar = true;
wim 1:eb4758bba68a 585 pattern = C7_MIN;
wim 1:eb4758bba68a 586 }
wim 1:eb4758bba68a 587 else if ((value >= 0) && (value < V56S_NR_UDC)) {
wim 1:eb4758bba68a 588 //Character to write
wim 1:eb4758bba68a 589 validChar = true;
wim 1:eb4758bba68a 590 pattern = _UDC_7S[value];
wim 1:eb4758bba68a 591 }
wim 1:eb4758bba68a 592 else if ((value >= (int)'0') && (value <= (int) '9')) {
wim 1:eb4758bba68a 593 //Character to write
wim 1:eb4758bba68a 594 validChar = true;
wim 1:eb4758bba68a 595 pattern = FONT_7S[value - (int) '0'];
wim 1:eb4758bba68a 596 }
wim 1:eb4758bba68a 597 else if ((value >= (int) 'A') && (value <= (int) 'F')) {
wim 1:eb4758bba68a 598 //Character to write
wim 1:eb4758bba68a 599 validChar = true;
wim 1:eb4758bba68a 600 pattern = FONT_7S[10 + value - (int) 'A'];
wim 1:eb4758bba68a 601 }
wim 1:eb4758bba68a 602 else if ((value >= (int) 'a') && (value <= (int) 'f')) {
wim 1:eb4758bba68a 603 //Character to write
wim 1:eb4758bba68a 604 validChar = true;
wim 1:eb4758bba68a 605 pattern = FONT_7S[10 + value - (int) 'a'];
wim 1:eb4758bba68a 606 } //else
wim 1:eb4758bba68a 607
wim 1:eb4758bba68a 608 if (validChar) {
wim 1:eb4758bba68a 609 //Character to write
wim 1:eb4758bba68a 610
wim 1:eb4758bba68a 611 //Translate between _column and displaybuffer entries
wim 1:eb4758bba68a 612 addr = _column << 1;
wim 1:eb4758bba68a 613
wim 1:eb4758bba68a 614 //Save icons...and set bits for character to write
wim 1:eb4758bba68a 615 _displaybuffer[addr] = (_displaybuffer[addr] & MASK_ICON_GRID[_column][0]) | LO(pattern);
wim 1:eb4758bba68a 616 _displaybuffer[addr+1] = (_displaybuffer[addr+1] & MASK_ICON_GRID[_column][1]) | HI(pattern);
wim 1:eb4758bba68a 617
wim 1:eb4758bba68a 618 writeData(_displaybuffer, (V56S_NR_GRIDS*2));
wim 1:eb4758bba68a 619
wim 1:eb4758bba68a 620 //Update Cursor
wim 1:eb4758bba68a 621 _column++;
wim 1:eb4758bba68a 622 if (_column > (V56S_NR_DIGITS - 1)) {
wim 1:eb4758bba68a 623 _column = 0;
wim 1:eb4758bba68a 624 }
wim 1:eb4758bba68a 625
wim 1:eb4758bba68a 626 } // if validChar
wim 1:eb4758bba68a 627
wim 1:eb4758bba68a 628 return value;
wim 1:eb4758bba68a 629 }
wim 1:eb4758bba68a 630
wim 1:eb4758bba68a 631
wim 1:eb4758bba68a 632 // get a single character (Stream implementation)
wim 1:eb4758bba68a 633 int PT6961_V56S::_getc() {
wim 1:eb4758bba68a 634 return -1;
wim 1:eb4758bba68a 635 }
wim 1:eb4758bba68a 636
wim 1:eb4758bba68a 637 #endif