Wim Huiskamp / PT6301
Committer:
wim
Date:
Sun Jun 13 13:14:12 2021 +0000
Revision:
1:aa0195b0f83c
Parent:
PT6302.cpp@0:ecc29c13a997
First release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 1:aa0195b0f83c 1 /* mbed PT6301 Library, for Princeton LC7571X VFD controller
wim 1:aa0195b0f83c 2 * The controller is used by Futaba 'Chip In Glass' (CIG) VFD tubes.
wim 0:ecc29c13a997 3 *
wim 1:aa0195b0f83c 4 * Copyright (c) 2021, v01: WH, Initial version
wim 0:ecc29c13a997 5 *
wim 0:ecc29c13a997 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:ecc29c13a997 7 * of this software and associated documentation files (the "Software"), to deal
wim 0:ecc29c13a997 8 * in the Software without restriction, including without limitation the rights
wim 0:ecc29c13a997 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:ecc29c13a997 10 * copies of the Software, and to permit persons to whom the Software is
wim 0:ecc29c13a997 11 * furnished to do so, subject to the following conditions:
wim 0:ecc29c13a997 12 *
wim 0:ecc29c13a997 13 * The above copyright notice and this permission notice shall be included in
wim 0:ecc29c13a997 14 * all copies or substantial portions of the Software.
wim 0:ecc29c13a997 15 *
wim 0:ecc29c13a997 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:ecc29c13a997 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:ecc29c13a997 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:ecc29c13a997 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:ecc29c13a997 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:ecc29c13a997 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:ecc29c13a997 22 * THE SOFTWARE.
wim 0:ecc29c13a997 23 */
wim 0:ecc29c13a997 24
wim 0:ecc29c13a997 25 #include "mbed.h"
wim 1:aa0195b0f83c 26 #include "PT6301.h"
wim 1:aa0195b0f83c 27 #include "PT6301_UDC.inc"
wim 0:ecc29c13a997 28
wim 0:ecc29c13a997 29
wim 1:aa0195b0f83c 30 /** Constructor for class for driving Princeton PT6301 VFD controller
wim 0:ecc29c13a997 31 *
wim 1:aa0195b0f83c 32 * @brief Supports upto 20 Grids of 5x7 matrix segments for 2 rows of characters (row A and B).
wim 1:aa0195b0f83c 33 * also supports 2 additional segments for 2 rows of characters (row A and B).
wim 0:ecc29c13a997 34 * SPI bus interface device.
wim 0:ecc29c13a997 35 * @param PinName mosi, sclk, cs SPI bus pins
wim 1:aa0195b0f83c 36 * @param Mode selects number of Grids and Rows (default 20 Grids, 2 rows)
wim 1:aa0195b0f83c 37 * @param bool inverted_rows selects mapping of Data onto Display layout (default false)
wim 1:aa0195b0f83c 38 * @param Columns selects number of characters per row (default 20, same as Mode Grids)
wim 1:aa0195b0f83c 39 * @param Rows selects number of rows (default 2, same as Mode Rows)
wim 0:ecc29c13a997 40 */
wim 1:aa0195b0f83c 41 PT6301::PT6301(PinName mosi, PinName sclk, PinName cs, PinName rst, Mode mode, bool inverted_rows, int columns, int rows) : _spi(mosi,NC,sclk), _cs(cs), _rst(rst), _mode(mode), _inverted_rows(inverted_rows), _columns(columns), _rows(rows) {
wim 1:aa0195b0f83c 42
wim 0:ecc29c13a997 43 _init();
wim 0:ecc29c13a997 44 }
wim 0:ecc29c13a997 45
wim 1:aa0195b0f83c 46
wim 1:aa0195b0f83c 47 /** Init the PT6301 interface and the controller
wim 0:ecc29c13a997 48 *
wim 0:ecc29c13a997 49 * @param none
wim 0:ecc29c13a997 50 * @return none
wim 0:ecc29c13a997 51 */
wim 1:aa0195b0f83c 52 void PT6301::_init(){
wim 0:ecc29c13a997 53
wim 0:ecc29c13a997 54 //init SPI
wim 0:ecc29c13a997 55 _cs=1;
wim 1:aa0195b0f83c 56 _spi.format(8,3); //PT6301 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge)
wim 0:ecc29c13a997 57 _spi.frequency(100000);
wim 0:ecc29c13a997 58 // _spi.frequency(250000);
wim 0:ecc29c13a997 59
wim 0:ecc29c13a997 60 //init controller
wim 1:aa0195b0f83c 61 #if(0)
wim 1:aa0195b0f83c 62 // Reset (3V3 level too low? Add pull-up to 5V)
wim 1:aa0195b0f83c 63 _rst=1;
wim 1:aa0195b0f83c 64 wait_ms(PT6301_RST_DLY);
wim 1:aa0195b0f83c 65 _rst=0;
wim 1:aa0195b0f83c 66 wait_ms(PT6301_RST_DLY);
wim 1:aa0195b0f83c 67 _rst=1;
wim 1:aa0195b0f83c 68 wait_ms(PT6301_RST_DLY);
wim 1:aa0195b0f83c 69 #endif
wim 0:ecc29c13a997 70
wim 0:ecc29c13a997 71 // Set number of Grids
wim 1:aa0195b0f83c 72 _writeCmd((PT6301_GRID_REG | (_mode & PT6301_GRID_MSK))); // Command register & value
wim 1:aa0195b0f83c 73
wim 1:aa0195b0f83c 74 setBrightness(PT6301_BRT_DEF); // Default Brightness
wim 0:ecc29c13a997 75
wim 1:aa0195b0f83c 76 // Clear the DCRAM and ADRAM (undefined at Reset) and reset (_row, _column)
wim 1:aa0195b0f83c 77 cls(true);
wim 1:aa0195b0f83c 78
wim 0:ecc29c13a997 79 // Clear the UDC RAM (undefined at Reset)
wim 0:ecc29c13a997 80 const char udc_none[] = {0x00,0x00,0x00,0x00,0x00};
wim 1:aa0195b0f83c 81 for (int idx=0; idx < PT6301_NR_UDC; idx++) {
wim 0:ecc29c13a997 82 setUDC(idx, (char *)udc_none);
wim 0:ecc29c13a997 83 }
wim 1:aa0195b0f83c 84
wim 1:aa0195b0f83c 85 // Update the display
wim 1:aa0195b0f83c 86 refresh();
wim 1:aa0195b0f83c 87
wim 1:aa0195b0f83c 88 setDisplay(true); // Display On
wim 0:ecc29c13a997 89 }
wim 0:ecc29c13a997 90
wim 0:ecc29c13a997 91
wim 1:aa0195b0f83c 92 /** Clear the screen and locate to (0,0)
wim 0:ecc29c13a997 93 *
wim 1:aa0195b0f83c 94 * @param bool clrAll Clear Icons also (default = false)
wim 0:ecc29c13a997 95 * @return none
wim 0:ecc29c13a997 96 */
wim 1:aa0195b0f83c 97 void PT6301::cls(bool clrAll) {
wim 0:ecc29c13a997 98
wim 1:aa0195b0f83c 99 for (_row = 0; _row < _rows; _row++) {
wim 1:aa0195b0f83c 100 for (_column = 0; _column < _columns; _column++) {
wim 1:aa0195b0f83c 101 _displaybuffer[_row][_column] = ' '; // data
wim 1:aa0195b0f83c 102
wim 1:aa0195b0f83c 103 if (clrAll) {
wim 1:aa0195b0f83c 104 _addbuffer[_row][_column] = 0; // icons
wim 1:aa0195b0f83c 105 }
wim 1:aa0195b0f83c 106 }
wim 0:ecc29c13a997 107 }
wim 0:ecc29c13a997 108
wim 1:aa0195b0f83c 109 _row = 0;
wim 1:aa0195b0f83c 110 _column = 0;
wim 0:ecc29c13a997 111 }
wim 0:ecc29c13a997 112
wim 0:ecc29c13a997 113
wim 1:aa0195b0f83c 114 /** Locate cursor to a screen row, column
wim 1:aa0195b0f83c 115 *
wim 1:aa0195b0f83c 116 * @param row The vertical position from the top, indexed from 0
wim 1:aa0195b0f83c 117 * @param column The horizontal position from the left, indexed from 0
wim 1:aa0195b0f83c 118 * @return none
wim 1:aa0195b0f83c 119 */
wim 1:aa0195b0f83c 120 void PT6301::locate(int row, int column) {
wim 1:aa0195b0f83c 121 //sanity check
wim 1:aa0195b0f83c 122 if (row < 0) {row = 0;}
wim 1:aa0195b0f83c 123 if (row > (_rows - 1)) {row = _rows - 1;}
wim 1:aa0195b0f83c 124
wim 1:aa0195b0f83c 125 if (column < 0) {column = 0;}
wim 1:aa0195b0f83c 126 if (column > (_columns - 1)) {column = _columns - 1;}
wim 1:aa0195b0f83c 127
wim 1:aa0195b0f83c 128 _row = row;
wim 1:aa0195b0f83c 129 _column = column;
wim 1:aa0195b0f83c 130 }
wim 1:aa0195b0f83c 131
wim 1:aa0195b0f83c 132 /** Number of screen columns
wim 1:aa0195b0f83c 133 *
wim 1:aa0195b0f83c 134 * @param none
wim 1:aa0195b0f83c 135 * @return columns
wim 1:aa0195b0f83c 136 */
wim 1:aa0195b0f83c 137 int PT6301::columns(){
wim 1:aa0195b0f83c 138 return _columns;
wim 1:aa0195b0f83c 139 }
wim 1:aa0195b0f83c 140
wim 1:aa0195b0f83c 141
wim 1:aa0195b0f83c 142 /** Number of screen rows
wim 1:aa0195b0f83c 143 *
wim 1:aa0195b0f83c 144 * @param none
wim 1:aa0195b0f83c 145 * @return rows
wim 1:aa0195b0f83c 146 */
wim 1:aa0195b0f83c 147 int PT6301::rows() {
wim 1:aa0195b0f83c 148 return _rows;
wim 1:aa0195b0f83c 149 }
wim 1:aa0195b0f83c 150
wim 1:aa0195b0f83c 151
wim 1:aa0195b0f83c 152 /** Refresh screen and show data in local mirrors on the display
wim 1:aa0195b0f83c 153 *
wim 1:aa0195b0f83c 154 * @param bool copyAll Copy Icons in Adat local mirror also (default = true)
wim 1:aa0195b0f83c 155 * @return none
wim 1:aa0195b0f83c 156 */
wim 1:aa0195b0f83c 157 void PT6301::refresh(bool copyAll) {
wim 1:aa0195b0f83c 158 int row_cnt, col_cnt;
wim 1:aa0195b0f83c 159
wim 1:aa0195b0f83c 160 //Copy character data mirror to display
wim 1:aa0195b0f83c 161 _cs=0; // Send Command for DATA_A_REG
wim 1:aa0195b0f83c 162 wait_us(1);
wim 1:aa0195b0f83c 163 _spi.write(_flip(PT6301_DATA_A_REG)); // Command register for DATA_A
wim 1:aa0195b0f83c 164 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 165
wim 1:aa0195b0f83c 166 row_cnt = _row_flip(0); // Reorder rows depending on VFD layout
wim 1:aa0195b0f83c 167 for (col_cnt = 0; col_cnt < _columns; col_cnt++) {
wim 1:aa0195b0f83c 168 _spi.write(_flip(_displaybuffer[row_cnt][col_cnt])); // DATA_A Row
wim 1:aa0195b0f83c 169 }
wim 1:aa0195b0f83c 170
wim 1:aa0195b0f83c 171 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 1:aa0195b0f83c 172 _cs=1; // Latch Command & Params
wim 1:aa0195b0f83c 173
wim 1:aa0195b0f83c 174 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 175
wim 1:aa0195b0f83c 176 _cs=0; // Send Command for DATA_B_REG
wim 1:aa0195b0f83c 177 wait_us(1);
wim 1:aa0195b0f83c 178 _spi.write(_flip(PT6301_DATA_B_REG)); // Command register for DATA_B
wim 1:aa0195b0f83c 179 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 180
wim 1:aa0195b0f83c 181 row_cnt = _row_flip(1); // Reorder rows depending on VFD layout
wim 1:aa0195b0f83c 182 for (col_cnt = 0; col_cnt < _columns; col_cnt++) {
wim 1:aa0195b0f83c 183 _spi.write(_flip(_displaybuffer[row_cnt][col_cnt])); // DATA_B Row
wim 1:aa0195b0f83c 184 }
wim 1:aa0195b0f83c 185
wim 1:aa0195b0f83c 186 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 1:aa0195b0f83c 187 _cs=1; // Latch Command & Params
wim 1:aa0195b0f83c 188
wim 1:aa0195b0f83c 189 //Copy icon data mirror to display
wim 1:aa0195b0f83c 190 if (copyAll) {
wim 1:aa0195b0f83c 191 _cs=0; // Send Command for ADAT_A_REG
wim 1:aa0195b0f83c 192 wait_us(1);
wim 1:aa0195b0f83c 193 _spi.write(_flip(PT6301_ADAT_A_REG)); // Command register for ADAT_A
wim 1:aa0195b0f83c 194 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 195
wim 1:aa0195b0f83c 196 row_cnt = _row_flip(0); // Reorder rows depending on VFD layout
wim 1:aa0195b0f83c 197 for (col_cnt = 0; col_cnt < _columns; col_cnt++) {
wim 1:aa0195b0f83c 198 _spi.write(_flip(_addbuffer[row_cnt][col_cnt])); // ADAT_A Row
wim 1:aa0195b0f83c 199 }
wim 1:aa0195b0f83c 200
wim 1:aa0195b0f83c 201 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 1:aa0195b0f83c 202 _cs=1; // Latch Command & Params
wim 1:aa0195b0f83c 203
wim 1:aa0195b0f83c 204 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 205
wim 1:aa0195b0f83c 206 _cs=0; // Send Command for ADAT_B_REG
wim 1:aa0195b0f83c 207 wait_us(1);
wim 1:aa0195b0f83c 208 _spi.write(_flip(PT6301_ADAT_B_REG)); // Command register for ADAT_B
wim 1:aa0195b0f83c 209 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 210
wim 1:aa0195b0f83c 211 row_cnt = _row_flip(1); // Reorder rows depending on VFD layout
wim 1:aa0195b0f83c 212 for (col_cnt = 0; col_cnt < _columns; col_cnt++) {
wim 1:aa0195b0f83c 213 _spi.write(_flip(_addbuffer[row_cnt][col_cnt])); // ADAT_B Row
wim 1:aa0195b0f83c 214 }
wim 1:aa0195b0f83c 215
wim 1:aa0195b0f83c 216 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 1:aa0195b0f83c 217 _cs=1; // Latch Command & Params
wim 1:aa0195b0f83c 218 }
wim 1:aa0195b0f83c 219 }
wim 1:aa0195b0f83c 220
wim 1:aa0195b0f83c 221
wim 0:ecc29c13a997 222 /** Set Brightness
wim 0:ecc29c13a997 223 *
wim 1:aa0195b0f83c 224 * @param char brightness (valid range 0..255)
wim 0:ecc29c13a997 225 * @return none
wim 0:ecc29c13a997 226 */
wim 1:aa0195b0f83c 227 void PT6301::setBrightness(char brightness){
wim 0:ecc29c13a997 228
wim 0:ecc29c13a997 229 //Sanity check
wim 1:aa0195b0f83c 230 //
wim 1:aa0195b0f83c 231
wim 1:aa0195b0f83c 232 _writeCmd(PT6301_BRT_REG, brightness); // Command register & value
wim 0:ecc29c13a997 233 }
wim 0:ecc29c13a997 234
wim 1:aa0195b0f83c 235
wim 0:ecc29c13a997 236 /** Set the Display mode On/off
wim 0:ecc29c13a997 237 *
wim 0:ecc29c13a997 238 * @param bool display mode
wim 0:ecc29c13a997 239 * @return none
wim 0:ecc29c13a997 240 */
wim 1:aa0195b0f83c 241 void PT6301::setDisplay(bool on) {
wim 0:ecc29c13a997 242 char display;
wim 0:ecc29c13a997 243
wim 0:ecc29c13a997 244 if (on) {
wim 1:aa0195b0f83c 245 display = PT6301_DSPL_NRM; // normal mode, show Display RAM content
wim 0:ecc29c13a997 246 }
wim 0:ecc29c13a997 247 else {
wim 1:aa0195b0f83c 248 display = PT6301_DSPL_OFF; // all segments off
wim 0:ecc29c13a997 249 }
wim 0:ecc29c13a997 250
wim 1:aa0195b0f83c 251 _writeCmd((PT6301_DSPL_REG | display)); // Command register & value
wim 0:ecc29c13a997 252 }
wim 0:ecc29c13a997 253
wim 0:ecc29c13a997 254
wim 1:aa0195b0f83c 255 /** Set the Display test mode On/off
wim 0:ecc29c13a997 256 *
wim 1:aa0195b0f83c 257 * @param bool display test mode
wim 1:aa0195b0f83c 258 * @return none
wim 1:aa0195b0f83c 259 */
wim 1:aa0195b0f83c 260 void PT6301::setDisplayTest(bool on) {
wim 1:aa0195b0f83c 261 char display;
wim 1:aa0195b0f83c 262
wim 1:aa0195b0f83c 263 if (on) {
wim 1:aa0195b0f83c 264 display = PT6301_DSPL_ON; // test mode, all segments on
wim 1:aa0195b0f83c 265 }
wim 1:aa0195b0f83c 266 else {
wim 1:aa0195b0f83c 267 display = PT6301_DSPL_NRM; // normal mode, show Display RAM content
wim 1:aa0195b0f83c 268 }
wim 1:aa0195b0f83c 269
wim 1:aa0195b0f83c 270 _writeCmd((PT6301_DSPL_REG | display)); // Command register & value
wim 1:aa0195b0f83c 271 }
wim 1:aa0195b0f83c 272
wim 1:aa0195b0f83c 273
wim 1:aa0195b0f83c 274 /** Set User Defined Characters (UDC) for A and B
wim 1:aa0195b0f83c 275 *
wim 1:aa0195b0f83c 276 * @param unsigned char udc_idx The Index of the UDC (0..15)
wim 0:ecc29c13a997 277 * @param UDCData_t udc_data The bitpattern for the UDC (5 bytes)
wim 0:ecc29c13a997 278 * @return none
wim 0:ecc29c13a997 279 */
wim 1:aa0195b0f83c 280 void PT6301::setUDC(unsigned char udc_idx, UDCData_t udc_data) {
wim 0:ecc29c13a997 281
wim 0:ecc29c13a997 282 //Sanity check
wim 1:aa0195b0f83c 283 udc_idx = udc_idx & PT6301_UADR_MSK; // mask invalid bits
wim 0:ecc29c13a997 284
wim 1:aa0195b0f83c 285 _cs=0; // Send Command & Params for UDC_A
wim 0:ecc29c13a997 286 wait_us(1);
wim 1:aa0195b0f83c 287 _spi.write(_flip(PT6301_UDC_A_REG | udc_idx)); // Command register & address for UDC_A
wim 1:aa0195b0f83c 288 wait_us(PT6301_CMD_DLY); // Command Delay
wim 0:ecc29c13a997 289
wim 1:aa0195b0f83c 290 _spi.write(_flip(udc_data[0] & PT6301_UDC_MSK)); // CD30 CD25 ...... CD0
wim 1:aa0195b0f83c 291 _spi.write(_flip(udc_data[1] & PT6301_UDC_MSK)); // CD31 CD26 ...... CD1
wim 1:aa0195b0f83c 292 _spi.write(_flip(udc_data[2] & PT6301_UDC_MSK)); // CD32 CD27 ...... CD2
wim 1:aa0195b0f83c 293 _spi.write(_flip(udc_data[3] & PT6301_UDC_MSK)); // CD33 CD28 ...... CD3
wim 1:aa0195b0f83c 294 _spi.write(_flip(udc_data[4] & PT6301_UDC_MSK)); // CD34 CD29 ...... CD4
wim 1:aa0195b0f83c 295
wim 1:aa0195b0f83c 296 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 1:aa0195b0f83c 297 _cs=1; // Latch Command & Params
wim 1:aa0195b0f83c 298
wim 1:aa0195b0f83c 299 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 300
wim 0:ecc29c13a997 301
wim 1:aa0195b0f83c 302 _cs=0; // Send Command & Params for UDC B
wim 1:aa0195b0f83c 303 wait_us(1);
wim 1:aa0195b0f83c 304 _spi.write(_flip(PT6301_UDC_B_REG | udc_idx)); // Command register & address for UDC_B
wim 1:aa0195b0f83c 305 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 306
wim 1:aa0195b0f83c 307 _spi.write(_flip(udc_data[0] & PT6301_UDC_MSK)); // CD30 CD25 ...... CD0
wim 1:aa0195b0f83c 308 _spi.write(_flip(udc_data[1] & PT6301_UDC_MSK)); // CD31 CD26 ...... CD1
wim 1:aa0195b0f83c 309 _spi.write(_flip(udc_data[2] & PT6301_UDC_MSK)); // CD32 CD27 ...... CD2
wim 1:aa0195b0f83c 310 _spi.write(_flip(udc_data[3] & PT6301_UDC_MSK)); // CD33 CD28 ...... CD3
wim 1:aa0195b0f83c 311 _spi.write(_flip(udc_data[4] & PT6301_UDC_MSK)); // CD34 CD29 ...... CD4
wim 1:aa0195b0f83c 312
wim 1:aa0195b0f83c 313 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 0:ecc29c13a997 314 _cs=1; // Latch Command & Params
wim 0:ecc29c13a997 315
wim 1:aa0195b0f83c 316 wait_us(PT6301_CMD_DLY); // Command Delay
wim 1:aa0195b0f83c 317
wim 0:ecc29c13a997 318 }
wim 0:ecc29c13a997 319
wim 1:aa0195b0f83c 320 /** Set Icon
wim 1:aa0195b0f83c 321 *
wim 1:aa0195b0f83c 322 * @param int row The row of the icon (0..(rows-1))
wim 1:aa0195b0f83c 323 * @param int column The column of the icon (0..(cols-1))
wim 1:aa0195b0f83c 324 * @return none
wim 1:aa0195b0f83c 325 */
wim 1:aa0195b0f83c 326 void PT6301::setIcon(int row, int column){
wim 1:aa0195b0f83c 327 //sanity check
wim 1:aa0195b0f83c 328 if (row < 0) {row = 0;}
wim 1:aa0195b0f83c 329 if (row > (_rows - 1)) {row = _rows - 1;}
wim 1:aa0195b0f83c 330
wim 1:aa0195b0f83c 331 if (column < 0) {column = 0;}
wim 1:aa0195b0f83c 332 if (column > (_columns - 1)) {column = _columns - 1;}
wim 1:aa0195b0f83c 333
wim 1:aa0195b0f83c 334 _addbuffer[row][column] = PT6301_ADAT_MSK;
wim 1:aa0195b0f83c 335 }
wim 1:aa0195b0f83c 336
wim 1:aa0195b0f83c 337 /** Clr Icon
wim 1:aa0195b0f83c 338 *
wim 1:aa0195b0f83c 339 * @param int row The row of the icon (0..(rows-1))
wim 1:aa0195b0f83c 340 * @param int column The column of the icon (0..(cols-1))
wim 1:aa0195b0f83c 341 * @return none
wim 1:aa0195b0f83c 342 */
wim 1:aa0195b0f83c 343 void PT6301::clrIcon(int row, int column){
wim 1:aa0195b0f83c 344 //sanity check
wim 1:aa0195b0f83c 345 if (row < 0) {row = 0;}
wim 1:aa0195b0f83c 346 if (row > (_rows - 1)) {row = _rows - 1;}
wim 1:aa0195b0f83c 347
wim 1:aa0195b0f83c 348 if (column < 0) {column = 0;}
wim 1:aa0195b0f83c 349 if (column > (_columns - 1)) {column = _columns - 1;}
wim 1:aa0195b0f83c 350
wim 1:aa0195b0f83c 351 _addbuffer[row][column] = 0x00;
wim 1:aa0195b0f83c 352 }
wim 1:aa0195b0f83c 353
wim 0:ecc29c13a997 354
wim 1:aa0195b0f83c 355 /** Write command to PT6301
wim 0:ecc29c13a997 356 *
wim 1:aa0195b0f83c 357 * @param char cmd Command byte
wim 0:ecc29c13a997 358 * @return none
wim 0:ecc29c13a997 359 */
wim 1:aa0195b0f83c 360 void PT6301::_writeCmd(char cmd){
wim 1:aa0195b0f83c 361
wim 1:aa0195b0f83c 362 _cs=0; // Prepare to send Command
wim 1:aa0195b0f83c 363 wait_us(1);
wim 0:ecc29c13a997 364
wim 1:aa0195b0f83c 365 _spi.write(_flip(cmd)); // Command register & value
wim 0:ecc29c13a997 366
wim 1:aa0195b0f83c 367 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 1:aa0195b0f83c 368 _cs=1; // Latch Command
wim 1:aa0195b0f83c 369
wim 1:aa0195b0f83c 370 wait_us(PT6301_CMD_DLY); // Command Delay
wim 0:ecc29c13a997 371 }
wim 0:ecc29c13a997 372
wim 0:ecc29c13a997 373
wim 1:aa0195b0f83c 374 /** Write command and data to PT6301
wim 0:ecc29c13a997 375 *
wim 0:ecc29c13a997 376 * @param char cmd Command byte
wim 0:ecc29c13a997 377 * @param char data Parameter for command
wim 0:ecc29c13a997 378 * @return none
wim 0:ecc29c13a997 379 */
wim 1:aa0195b0f83c 380 void PT6301::_writeCmd(char cmd, char data){
wim 0:ecc29c13a997 381
wim 0:ecc29c13a997 382 _cs=0; // Prepare to send Command and data
wim 0:ecc29c13a997 383 wait_us(1);
wim 0:ecc29c13a997 384
wim 0:ecc29c13a997 385 _spi.write(_flip(cmd)); // Command register & value
wim 0:ecc29c13a997 386
wim 1:aa0195b0f83c 387 wait_us(PT6301_CMD_DLY); // Command Delay
wim 0:ecc29c13a997 388
wim 0:ecc29c13a997 389 _spi.write(_flip(data)); // data
wim 0:ecc29c13a997 390
wim 1:aa0195b0f83c 391 wait_us(PT6301_CS_DLY); // CS Hold Delay
wim 0:ecc29c13a997 392 _cs=1; // Latch Command and data
wim 0:ecc29c13a997 393
wim 1:aa0195b0f83c 394 wait_us(PT6301_CMD_DLY); // Command Delay
wim 0:ecc29c13a997 395 }
wim 0:ecc29c13a997 396
wim 1:aa0195b0f83c 397 /** Write Data to local mirror
wim 0:ecc29c13a997 398 *
wim 1:aa0195b0f83c 399 * @param char data The databyte
wim 1:aa0195b0f83c 400 * @param row The vertical position from the top, indexed from 0
wim 1:aa0195b0f83c 401 * @param column The horizontal position from the left, indexed from 0
wim 1:aa0195b0f83c 402 * @return none
wim 1:aa0195b0f83c 403 */
wim 1:aa0195b0f83c 404 void PT6301::setData(char data, int row, int column){
wim 1:aa0195b0f83c 405
wim 1:aa0195b0f83c 406 //Sanity check, allow access to all of local mirror
wim 1:aa0195b0f83c 407 if (row < 0) {row = 0;}
wim 1:aa0195b0f83c 408 if (row > (PT6301_MAX_NR_ROWS - 1)) {row = PT6301_MAX_NR_ROWS - 1;}
wim 1:aa0195b0f83c 409
wim 1:aa0195b0f83c 410 if (column < 0) {column = 0;}
wim 1:aa0195b0f83c 411 if (column > (PT6301_MAX_NR_GRIDS - 1)) {column = PT6301_MAX_NR_GRIDS - 1;}
wim 1:aa0195b0f83c 412
wim 1:aa0195b0f83c 413 _displaybuffer[row][column] = data;
wim 1:aa0195b0f83c 414 }
wim 1:aa0195b0f83c 415
wim 1:aa0195b0f83c 416 /** Read Data from local mirror
wim 1:aa0195b0f83c 417 *
wim 1:aa0195b0f83c 418 * @param row The vertical position from the top, indexed from 0
wim 1:aa0195b0f83c 419 * @param column The horizontal position from the left, indexed from 0
wim 1:aa0195b0f83c 420 * @return char The databyte
wim 1:aa0195b0f83c 421 */
wim 1:aa0195b0f83c 422 char PT6301::getData(int row, int column){
wim 1:aa0195b0f83c 423
wim 1:aa0195b0f83c 424 //Sanity check, allow access to all of local mirror
wim 1:aa0195b0f83c 425 if (row < 0) {row = 0;}
wim 1:aa0195b0f83c 426 if (row > (PT6301_MAX_NR_ROWS - 1)) {row = PT6301_MAX_NR_ROWS - 1;}
wim 1:aa0195b0f83c 427
wim 1:aa0195b0f83c 428 if (column < 0) {column = 0;}
wim 1:aa0195b0f83c 429 if (column > (PT6301_MAX_NR_GRIDS - 1)) {column = PT6301_MAX_NR_GRIDS - 1;}
wim 1:aa0195b0f83c 430
wim 1:aa0195b0f83c 431 return _displaybuffer[row][column];
wim 1:aa0195b0f83c 432 }
wim 0:ecc29c13a997 433
wim 1:aa0195b0f83c 434 /** Write AData to local mirror
wim 1:aa0195b0f83c 435 *
wim 1:aa0195b0f83c 436 * @param char data The symbol databyte
wim 1:aa0195b0f83c 437 * @param row The vertical position from the top, indexed from 0
wim 1:aa0195b0f83c 438 * @param column The horizontal position from the left, indexed from 0
wim 1:aa0195b0f83c 439 * @return none
wim 1:aa0195b0f83c 440 */
wim 1:aa0195b0f83c 441 void PT6301::setAData(char data, int row, int column){
wim 1:aa0195b0f83c 442
wim 1:aa0195b0f83c 443 //Sanity check, allow access to all of local mirror
wim 1:aa0195b0f83c 444 if (row < 0) {row = 0;}
wim 1:aa0195b0f83c 445 if (row > (PT6301_MAX_NR_ROWS - 1)) {row = PT6301_MAX_NR_ROWS - 1;}
wim 0:ecc29c13a997 446
wim 1:aa0195b0f83c 447 if (column < 0) {column = 0;}
wim 1:aa0195b0f83c 448 if (column > (PT6301_MAX_NR_GRIDS - 1)) {column = PT6301_MAX_NR_GRIDS - 1;}
wim 1:aa0195b0f83c 449
wim 1:aa0195b0f83c 450 _addbuffer[row][column] = data & PT6301_ADAT_MSK;
wim 1:aa0195b0f83c 451 }
wim 0:ecc29c13a997 452
wim 1:aa0195b0f83c 453 /** Read AData from local mirror
wim 1:aa0195b0f83c 454 *
wim 1:aa0195b0f83c 455 * @param row The vertical position from the top, indexed from 0
wim 1:aa0195b0f83c 456 * @param column The horizontal position from the left, indexed from 0
wim 1:aa0195b0f83c 457 * @return char The symbol databyte
wim 1:aa0195b0f83c 458 */
wim 1:aa0195b0f83c 459 char PT6301::getAData(int row, int column){
wim 1:aa0195b0f83c 460
wim 1:aa0195b0f83c 461 //Sanity check, allow access to all of local mirror
wim 1:aa0195b0f83c 462 if (row < 0) {row = 0;}
wim 1:aa0195b0f83c 463 if (row > (PT6301_MAX_NR_ROWS - 1)) {row = PT6301_MAX_NR_ROWS - 1;}
wim 1:aa0195b0f83c 464
wim 1:aa0195b0f83c 465 if (column < 0) {column = 0;}
wim 1:aa0195b0f83c 466 if (column > (PT6301_MAX_NR_GRIDS - 1)) {column = PT6301_MAX_NR_GRIDS - 1;}
wim 1:aa0195b0f83c 467
wim 1:aa0195b0f83c 468 return _addbuffer[row][column];
wim 1:aa0195b0f83c 469 }
wim 0:ecc29c13a997 470
wim 0:ecc29c13a997 471
wim 0:ecc29c13a997 472
wim 1:aa0195b0f83c 473
wim 1:aa0195b0f83c 474 /** Helper to reverse all command or databits. The PT6301 expects LSB first, whereas SPI is MSB first
wim 0:ecc29c13a997 475 *
wim 0:ecc29c13a997 476 * @param char data
wim 0:ecc29c13a997 477 * @return bitreversed data
wim 0:ecc29c13a997 478 */
wim 1:aa0195b0f83c 479 char PT6301::_flip(char data) {
wim 0:ecc29c13a997 480 char value=0;
wim 0:ecc29c13a997 481
wim 0:ecc29c13a997 482 if (data & 0x01) {value |= 0x80;} ;
wim 0:ecc29c13a997 483 if (data & 0x02) {value |= 0x40;} ;
wim 0:ecc29c13a997 484 if (data & 0x04) {value |= 0x20;} ;
wim 0:ecc29c13a997 485 if (data & 0x08) {value |= 0x10;} ;
wim 0:ecc29c13a997 486 if (data & 0x10) {value |= 0x08;} ;
wim 0:ecc29c13a997 487 if (data & 0x20) {value |= 0x04;} ;
wim 0:ecc29c13a997 488 if (data & 0x40) {value |= 0x02;} ;
wim 0:ecc29c13a997 489 if (data & 0x80) {value |= 0x01;} ;
wim 1:aa0195b0f83c 490 return value;
wim 0:ecc29c13a997 491 }
wim 0:ecc29c13a997 492
wim 0:ecc29c13a997 493
wim 1:aa0195b0f83c 494 /** Helper to reverse row idx depending on VFD layout
wim 0:ecc29c13a997 495 *
wim 1:aa0195b0f83c 496 * @param int row_idx
wim 1:aa0195b0f83c 497 * @return adjusted row_idx
wim 1:aa0195b0f83c 498 */
wim 1:aa0195b0f83c 499 int PT6301::_row_flip(int row_idx) {
wim 1:aa0195b0f83c 500 if (_inverted_rows) {
wim 1:aa0195b0f83c 501 return (1 - row_idx); // Reorder row mapping to match VFD layout
wim 1:aa0195b0f83c 502 // Top line is DATA_B_REG, ADAT_B_REG
wim 1:aa0195b0f83c 503 // Bottom line is DATA_A_REG, ADAT_A_REG
wim 1:aa0195b0f83c 504 }
wim 1:aa0195b0f83c 505 else {
wim 1:aa0195b0f83c 506 return row_idx; // Maintain row mapping to match VFD layout
wim 1:aa0195b0f83c 507 // Top line is DATA_A_REG, ADAT_A_REG
wim 1:aa0195b0f83c 508 // Bottom line is DATA_B_REG, ADAT_B_REG
wim 1:aa0195b0f83c 509 }
wim 0:ecc29c13a997 510 }
wim 1:aa0195b0f83c 511
wim 0:ecc29c13a997 512
wim 0:ecc29c13a997 513 /** Write a single character (Stream implementation)
wim 0:ecc29c13a997 514 *
wim 0:ecc29c13a997 515 * @param value char to print
wim 0:ecc29c13a997 516 * @return value;
wim 0:ecc29c13a997 517 */
wim 1:aa0195b0f83c 518 int PT6301::_putc(int value) {
wim 1:aa0195b0f83c 519
wim 1:aa0195b0f83c 520 if (value == '\r') {
wim 0:ecc29c13a997 521 //No character to write
wim 0:ecc29c13a997 522
wim 0:ecc29c13a997 523 //Update Cursor
wim 0:ecc29c13a997 524 _column = 0;
wim 0:ecc29c13a997 525 }
wim 1:aa0195b0f83c 526 else if (value == '\n') {
wim 1:aa0195b0f83c 527 //No character to write
wim 1:aa0195b0f83c 528
wim 1:aa0195b0f83c 529 //Update Cursor
wim 1:aa0195b0f83c 530 _row++;
wim 1:aa0195b0f83c 531 if (_row > (_rows - 1)) {
wim 1:aa0195b0f83c 532 _row = 0;
wim 1:aa0195b0f83c 533 }
wim 1:aa0195b0f83c 534 }
wim 0:ecc29c13a997 535 else if ((value >= 0) && (value < 256)) {
wim 1:aa0195b0f83c 536 //Valid character to write
wim 0:ecc29c13a997 537
wim 1:aa0195b0f83c 538 //Write displaybuffer entry
wim 1:aa0195b0f83c 539 _displaybuffer[_row][_column] = value;
wim 0:ecc29c13a997 540
wim 0:ecc29c13a997 541 //Update Cursor
wim 0:ecc29c13a997 542 _column++;
wim 1:aa0195b0f83c 543 if (_column > (_columns - 1)) {
wim 0:ecc29c13a997 544 _column = 0;
wim 1:aa0195b0f83c 545 _row++;
wim 0:ecc29c13a997 546 }
wim 1:aa0195b0f83c 547 if (_row > (_rows - 1)) {
wim 1:aa0195b0f83c 548 _row = 0;
wim 1:aa0195b0f83c 549 }
wim 1:aa0195b0f83c 550 } // if validChar
wim 0:ecc29c13a997 551
wim 0:ecc29c13a997 552 return value;
wim 0:ecc29c13a997 553 }
wim 0:ecc29c13a997 554
wim 0:ecc29c13a997 555 /** Get a single character (Stream implementation)
wim 0:ecc29c13a997 556 *
wim 0:ecc29c13a997 557 * @param none
wim 0:ecc29c13a997 558 * @return -1
wim 0:ecc29c13a997 559 */
wim 1:aa0195b0f83c 560 int PT6301::_getc() {
wim 0:ecc29c13a997 561 return -1;
wim 1:aa0195b0f83c 562 }
wim 1:aa0195b0f83c 563
wim 1:aa0195b0f83c 564
wim 1:aa0195b0f83c 565
wim 1:aa0195b0f83c 566 #if (SMTG7400_TEST == 1)
wim 1:aa0195b0f83c 567
wim 1:aa0195b0f83c 568 /** Constructor for class for Princeton PT6301 VFD controller as used in SMTG7400
wim 1:aa0195b0f83c 569 *
wim 1:aa0195b0f83c 570 * @brief Supports 16 Grids of 5x7 Segments with 4 additional Segments in use.
wim 1:aa0195b0f83c 571 *
wim 1:aa0195b0f83c 572 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 1:aa0195b0f83c 573 * @param PinName rst Reset pin
wim 1:aa0195b0f83c 574 */
wim 1:aa0195b0f83c 575 PT6301_SMTG7400::PT6301_SMTG7400(PinName mosi, PinName sclk, PinName cs, PinName rst) : PT6301(mosi, sclk, cs, rst, Grid16, true, SMTG7400_NR_COLS, SMTG7400_NR_ROWS) {
wim 1:aa0195b0f83c 576
wim 0:ecc29c13a997 577 }
wim 1:aa0195b0f83c 578
wim 1:aa0195b0f83c 579 /** Set Icon
wim 1:aa0195b0f83c 580 *
wim 1:aa0195b0f83c 581 * @param int icon The icon ID
wim 1:aa0195b0f83c 582 * @return none
wim 1:aa0195b0f83c 583 */
wim 1:aa0195b0f83c 584 void PT6301_SMTG7400::setIcon(int icon) {
wim 1:aa0195b0f83c 585 PT6301::setIcon((icon >> SMTG7400_ICON_ROW_SHFT), (icon & SMTG7400_ICON_COL_MSK));
wim 1:aa0195b0f83c 586 }
wim 1:aa0195b0f83c 587
wim 1:aa0195b0f83c 588 /** Clr Icon
wim 1:aa0195b0f83c 589 *
wim 1:aa0195b0f83c 590 * @param int icon The icon ID
wim 1:aa0195b0f83c 591 * @return none
wim 1:aa0195b0f83c 592 */
wim 1:aa0195b0f83c 593 void PT6301_SMTG7400::clrIcon(int icon) {
wim 1:aa0195b0f83c 594 PT6301::clrIcon((icon >> SMTG7400_ICON_ROW_SHFT), (icon & SMTG7400_ICON_COL_MSK));
wim 1:aa0195b0f83c 595 }
wim 1:aa0195b0f83c 596
wim 0:ecc29c13a997 597 #endif
wim 1:aa0195b0f83c 598
wim 1:aa0195b0f83c 599
wim 1:aa0195b0f83c 600 #if (SMTC7140_TEST == 1)
wim 1:aa0195b0f83c 601
wim 1:aa0195b0f83c 602 /** Constructor for class for Princeton PT6301 VFD controller as used in SMTC7140
wim 1:aa0195b0f83c 603 *
wim 1:aa0195b0f83c 604 * @brief Supports 12 Grids of 5x7 Segments without additional Icon Segments, for 2 Rows.
wim 1:aa0195b0f83c 605 * Grid13 is used for icons displayed by a UDC symbol.
wim 1:aa0195b0f83c 606 *
wim 1:aa0195b0f83c 607 * @param PinName mosi, miso, sclk, cs SPI bus pins
wim 1:aa0195b0f83c 608 * @param PinName rst Reset pin
wim 1:aa0195b0f83c 609 */
wim 1:aa0195b0f83c 610 PT6301_SMTC7140::PT6301_SMTC7140(PinName mosi, PinName sclk, PinName cs, PinName rst) : PT6301(mosi, sclk, cs, rst, Grid13, true, SMTC7140_NR_COLS, SMTC7140_NR_ROWS) {
wim 1:aa0195b0f83c 611
wim 1:aa0195b0f83c 612 //Enable VGen for VFD Power Supply
wim 1:aa0195b0f83c 613 //Note this is wrong because we should send the init commands to the PT6301 before the 5V powersupply is enabled !
wim 1:aa0195b0f83c 614 // setVGen(true);
wim 1:aa0195b0f83c 615
wim 1:aa0195b0f83c 616 }
wim 1:aa0195b0f83c 617
wim 1:aa0195b0f83c 618 /** Set VFD VGen
wim 1:aa0195b0f83c 619 *
wim 1:aa0195b0f83c 620 * @param bool on
wim 1:aa0195b0f83c 621 * @return none
wim 1:aa0195b0f83c 622 */
wim 1:aa0195b0f83c 623 void PT6301_SMTC7140::setVGen (bool on) {
wim 1:aa0195b0f83c 624
wim 1:aa0195b0f83c 625 }
wim 1:aa0195b0f83c 626
wim 1:aa0195b0f83c 627
wim 1:aa0195b0f83c 628 /** Set IconGrid13
wim 1:aa0195b0f83c 629 * Icons are shown on Grid13 using the UDC at index=0. The UDC char=0 is stored in _displaybuffer[0][12] at reset.
wim 1:aa0195b0f83c 630 * This method will set the correct segment in the UDC for each icon.
wim 1:aa0195b0f83c 631 *
wim 1:aa0195b0f83c 632 * @param int icon The icon ID
wim 1:aa0195b0f83c 633 * @return none
wim 1:aa0195b0f83c 634 */
wim 1:aa0195b0f83c 635 void PT6301_SMTC7140::setIconGrid13(int icon) {
wim 1:aa0195b0f83c 636
wim 1:aa0195b0f83c 637 #if(0)
wim 1:aa0195b0f83c 638 //Test version to check all bits
wim 1:aa0195b0f83c 639 // clear icon
wim 1:aa0195b0f83c 640 for (int udc_col=0; udc_col<5; udc_col++) {
wim 1:aa0195b0f83c 641 _icon_data[udc_col] = 0x00;
wim 1:aa0195b0f83c 642 };
wim 1:aa0195b0f83c 643
wim 1:aa0195b0f83c 644 _icon_data[icon >> 8] = (icon & 0x7F);
wim 1:aa0195b0f83c 645 setUDC(0, (char *) _icon_data); // Store mirror for UDC_idx=0
wim 1:aa0195b0f83c 646
wim 1:aa0195b0f83c 647 #else
wim 1:aa0195b0f83c 648 //Normal version
wim 1:aa0195b0f83c 649 for (int udc_col=0; udc_col<5; udc_col++) {
wim 1:aa0195b0f83c 650 _icon_data[udc_col] = _icon_data[udc_col] | SMTC7140_ICONS[icon][udc_col]; // OR icon bitpattern with UDC mirror for UDC_idx=0
wim 1:aa0195b0f83c 651 }
wim 1:aa0195b0f83c 652
wim 1:aa0195b0f83c 653 setUDC(0, (char *) _icon_data); // Store mirror for UDC_idx=0
wim 1:aa0195b0f83c 654 #endif
wim 1:aa0195b0f83c 655
wim 1:aa0195b0f83c 656 }
wim 1:aa0195b0f83c 657
wim 1:aa0195b0f83c 658 /** Clr IconGrid13
wim 1:aa0195b0f83c 659 * Icons are shown on Grid13 using the UDC at index=0. The UDC char=0 is stored in _displaybuffer[0][12] at reset.
wim 1:aa0195b0f83c 660 * This method will clr the correct segment in the UDC for each icon.
wim 1:aa0195b0f83c 661 *
wim 1:aa0195b0f83c 662 * @param int icon The icon ID
wim 1:aa0195b0f83c 663 * @return none
wim 1:aa0195b0f83c 664 */
wim 1:aa0195b0f83c 665 void PT6301_SMTC7140::clrIconGrid13(int icon) {
wim 1:aa0195b0f83c 666
wim 1:aa0195b0f83c 667 for (int udc_col=0; udc_col<5; udc_col++) {
wim 1:aa0195b0f83c 668 _icon_data[udc_col] = _icon_data[udc_col] & ~(SMTC7140_ICONS[icon][udc_col]); // AND inverted icon bitpattern with UDC mirror for UDC_idx=0
wim 1:aa0195b0f83c 669 }
wim 1:aa0195b0f83c 670
wim 1:aa0195b0f83c 671 setUDC(0, (char *) _icon_data); // Store mirror for UDC_idx=0
wim 1:aa0195b0f83c 672 }
wim 1:aa0195b0f83c 673
wim 1:aa0195b0f83c 674 #endif