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 14 20:03:13 2016 +0000
Revision:
2:c6883ede8d8b
Parent:
1:eb4758bba68a
Refactored display and keyboard defines

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