Test program for Princeton PT6961 LED controller library.

Dependencies:   PT6961 mbed

See here for more information.

Committer:
wim
Date:
Sun Aug 23 14:40:33 2015 +0000
Revision:
0:c77f1ad8d993
Child:
1:59c864633347
First Test Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:c77f1ad8d993 1 /* mbed PT6961 Test program, for Princeton PT6961 LED controller
wim 0:c77f1ad8d993 2 * Copyright (c) 2015, v01: WH, Initial version
wim 0:c77f1ad8d993 3 *
wim 0:c77f1ad8d993 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:c77f1ad8d993 5 * of this software and associated documentation files (the "Software"), to deal
wim 0:c77f1ad8d993 6 * in the Software without restriction, including without limitation the rights
wim 0:c77f1ad8d993 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:c77f1ad8d993 8 * copies of the Software, and to permit persons to whom the Software is
wim 0:c77f1ad8d993 9 * furnished to do so, subject to the following conditions:
wim 0:c77f1ad8d993 10 *
wim 0:c77f1ad8d993 11 * The above copyright notice and this permission notice shall be included in
wim 0:c77f1ad8d993 12 * all copies or substantial portions of the Software.
wim 0:c77f1ad8d993 13 *
wim 0:c77f1ad8d993 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:c77f1ad8d993 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:c77f1ad8d993 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:c77f1ad8d993 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:c77f1ad8d993 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:c77f1ad8d993 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:c77f1ad8d993 20 * THE SOFTWARE.
wim 0:c77f1ad8d993 21 */
wim 0:c77f1ad8d993 22 #include "mbed.h"
wim 0:c77f1ad8d993 23 #include "PT6961.h"
wim 0:c77f1ad8d993 24
wim 0:c77f1ad8d993 25 #if(1)
wim 0:c77f1ad8d993 26 Serial pc(USBTX, USBRX);
wim 0:c77f1ad8d993 27
wim 0:c77f1ad8d993 28 DigitalOut myled(LED1);
wim 0:c77f1ad8d993 29
wim 0:c77f1ad8d993 30 // DisplayData_t size is 12 bytes (6 digits max 12 segments) OR 14 bytes (7 digits at max 11 segments)
wim 0:c77f1ad8d993 31 PT6961::DisplayData_t mbed_str = {0xDA,0x00, 0x7C,0x00, 0x3C,0x01, 0xF6,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};
wim 0:c77f1ad8d993 32 PT6961::DisplayData_t all_str = {0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F, 0xFF,0x0F};
wim 0:c77f1ad8d993 33 PT6961::DisplayData_t bye_str = {0x7C,0x00, 0xEC,0x00, 0x3C,0x01, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};
wim 0:c77f1ad8d993 34 PT6961::DisplayData_t hello_str = {0xDC,0x00, 0x3C,0x01, 0x38,0x00, 0x38,0x00, 0xF8,0x01, 0x00,0x00, 0x00,0x00};
wim 0:c77f1ad8d993 35
wim 0:c77f1ad8d993 36 // KeyData_t size is 5 bytes
wim 0:c77f1ad8d993 37 PT6961::KeyData_t keydata;
wim 0:c77f1ad8d993 38
wim 0:c77f1ad8d993 39 // PT6191 declaration, Default setting 7 Digits, 11 Segments
wim 0:c77f1ad8d993 40 PT6961 pt6961(p5,p6,p7, p8);
wim 0:c77f1ad8d993 41
wim 0:c77f1ad8d993 42 int main() {
wim 0:c77f1ad8d993 43 // char c;
wim 0:c77f1ad8d993 44 // int data;
wim 0:c77f1ad8d993 45
wim 0:c77f1ad8d993 46 pc.printf("Hello World\r\n"); //
wim 0:c77f1ad8d993 47
wim 0:c77f1ad8d993 48 pt6961.cls();
wim 0:c77f1ad8d993 49 pt6961.writeData(all_str);
wim 0:c77f1ad8d993 50 wait(4);
wim 0:c77f1ad8d993 51 pt6961.setBrightness(PT6961_BRT3);
wim 0:c77f1ad8d993 52 pt6961.writeData(mbed_str);
wim 0:c77f1ad8d993 53 wait(1);
wim 0:c77f1ad8d993 54 pt6961.setBrightness(PT6961_BRT0);
wim 0:c77f1ad8d993 55 wait(1);
wim 0:c77f1ad8d993 56 pt6961.setBrightness(PT6961_BRT3);
wim 0:c77f1ad8d993 57
wim 0:c77f1ad8d993 58 while (1) {
wim 0:c77f1ad8d993 59
wim 0:c77f1ad8d993 60 // Check and read keydata
wim 0:c77f1ad8d993 61 if (pt6961.readKeys(&keydata)) {
wim 0:c77f1ad8d993 62 // pc.printf("Key\r\n"); //
wim 0:c77f1ad8d993 63 pc.printf("Keydata 0..4 = 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\r\n", keydata[0], keydata[1], keydata[2], keydata[3], keydata[4]);
wim 0:c77f1ad8d993 64
wim 0:c77f1ad8d993 65 if (keydata[0] == 0x10) { //sw2
wim 0:c77f1ad8d993 66 pt6961.cls();
wim 0:c77f1ad8d993 67 pt6961.writeData(all_str);
wim 0:c77f1ad8d993 68 }
wim 0:c77f1ad8d993 69
wim 0:c77f1ad8d993 70 if (keydata[0] == 0x02) { //sw8
wim 0:c77f1ad8d993 71 pt6961.cls();
wim 0:c77f1ad8d993 72 pt6961.writeData(hello_str);
wim 0:c77f1ad8d993 73 }
wim 0:c77f1ad8d993 74
wim 0:c77f1ad8d993 75 if (keydata[0] == 0x20) { //sw6
wim 0:c77f1ad8d993 76 pt6961.cls();
wim 0:c77f1ad8d993 77 pt6961.writeData(mbed_str);
wim 0:c77f1ad8d993 78 }
wim 0:c77f1ad8d993 79
wim 0:c77f1ad8d993 80 if (keydata[1] == 0x02) { //sw4
wim 0:c77f1ad8d993 81 pt6961.cls();
wim 0:c77f1ad8d993 82 pt6961.writeData(bye_str);
wim 0:c77f1ad8d993 83 }
wim 0:c77f1ad8d993 84
wim 0:c77f1ad8d993 85 } //if
wim 0:c77f1ad8d993 86
wim 0:c77f1ad8d993 87 myled = !myled;
wim 0:c77f1ad8d993 88 wait(0.3);
wim 0:c77f1ad8d993 89 } //while
wim 0:c77f1ad8d993 90 }
wim 0:c77f1ad8d993 91
wim 0:c77f1ad8d993 92 #else
wim 0:c77f1ad8d993 93
wim 0:c77f1ad8d993 94 Serial pc(USBTX, USBRX);
wim 0:c77f1ad8d993 95
wim 0:c77f1ad8d993 96 DigitalOut myled(LED1);
wim 0:c77f1ad8d993 97
wim 0:c77f1ad8d993 98 SPI spi(p5,p6,p7);
wim 0:c77f1ad8d993 99 DigitalOut cs(p8);
wim 0:c77f1ad8d993 100
wim 0:c77f1ad8d993 101 int flip(char data) {
wim 0:c77f1ad8d993 102 char value=0;
wim 0:c77f1ad8d993 103
wim 0:c77f1ad8d993 104 if ((data & 0x01) == 0x01) {value |= 0x80;} ;
wim 0:c77f1ad8d993 105 if ((data & 0x02) == 0x02) {value |= 0x40;} ;
wim 0:c77f1ad8d993 106 if ((data & 0x04) == 0x04) {value |= 0x20;} ;
wim 0:c77f1ad8d993 107 if ((data & 0x08) == 0x08) {value |= 0x10;} ;
wim 0:c77f1ad8d993 108 if ((data & 0x10) == 0x10) {value |= 0x08;} ;
wim 0:c77f1ad8d993 109 if ((data & 0x20) == 0x20) {value |= 0x04;} ;
wim 0:c77f1ad8d993 110 if ((data & 0x40) == 0x40) {value |= 0x02;} ;
wim 0:c77f1ad8d993 111 if ((data & 0x80) == 0x80) {value |= 0x01;} ;
wim 0:c77f1ad8d993 112 return value;
wim 0:c77f1ad8d993 113 }
wim 0:c77f1ad8d993 114
wim 0:c77f1ad8d993 115 void fill () {
wim 0:c77f1ad8d993 116 cs=0;
wim 0:c77f1ad8d993 117 wait_us(1);
wim 0:c77f1ad8d993 118 spi.write(flip(0xC0)); // address set cmd, 0
wim 0:c77f1ad8d993 119
wim 0:c77f1ad8d993 120 spi.write(flip(0xFF)); // data
wim 0:c77f1ad8d993 121 spi.write(flip(0x07)); // data
wim 0:c77f1ad8d993 122
wim 0:c77f1ad8d993 123 spi.write(flip(0xFF)); // data
wim 0:c77f1ad8d993 124 spi.write(flip(0x07)); // data
wim 0:c77f1ad8d993 125
wim 0:c77f1ad8d993 126 spi.write(flip(0xFF)); // data
wim 0:c77f1ad8d993 127 spi.write(flip(0x07)); // data
wim 0:c77f1ad8d993 128
wim 0:c77f1ad8d993 129 spi.write(flip(0xFF)); // data
wim 0:c77f1ad8d993 130 spi.write(flip(0x07)); // data
wim 0:c77f1ad8d993 131
wim 0:c77f1ad8d993 132 spi.write(flip(0xFF)); // data
wim 0:c77f1ad8d993 133 spi.write(flip(0x07)); // data
wim 0:c77f1ad8d993 134
wim 0:c77f1ad8d993 135 spi.write(flip(0xFF)); // data
wim 0:c77f1ad8d993 136 spi.write(flip(0x07)); // data
wim 0:c77f1ad8d993 137
wim 0:c77f1ad8d993 138 spi.write(flip(0xFF)); // data
wim 0:c77f1ad8d993 139 spi.write(flip(0x07)); // data
wim 0:c77f1ad8d993 140
wim 0:c77f1ad8d993 141 wait_us(1);
wim 0:c77f1ad8d993 142 cs=1;
wim 0:c77f1ad8d993 143 }
wim 0:c77f1ad8d993 144
wim 0:c77f1ad8d993 145 void clear () {
wim 0:c77f1ad8d993 146 cs=0;
wim 0:c77f1ad8d993 147 wait_us(1);
wim 0:c77f1ad8d993 148 spi.write(flip(0xC0)); // address set cmd, 0
wim 0:c77f1ad8d993 149
wim 0:c77f1ad8d993 150 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 151 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 152
wim 0:c77f1ad8d993 153 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 154 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 155
wim 0:c77f1ad8d993 156 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 157 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 158
wim 0:c77f1ad8d993 159 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 160 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 161
wim 0:c77f1ad8d993 162 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 163 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 164
wim 0:c77f1ad8d993 165 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 166 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 167
wim 0:c77f1ad8d993 168 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 169 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 170
wim 0:c77f1ad8d993 171 wait_us(1);
wim 0:c77f1ad8d993 172 cs=1;
wim 0:c77f1ad8d993 173 }
wim 0:c77f1ad8d993 174
wim 0:c77f1ad8d993 175 int main() {
wim 0:c77f1ad8d993 176 char c;
wim 0:c77f1ad8d993 177 int data;
wim 0:c77f1ad8d993 178
wim 0:c77f1ad8d993 179 pc.printf("Hello World\r\n"); //
wim 0:c77f1ad8d993 180
wim 0:c77f1ad8d993 181 cs=1;
wim 0:c77f1ad8d993 182 spi.format(8,3);
wim 0:c77f1ad8d993 183 spi.frequency(100000);
wim 0:c77f1ad8d993 184
wim 0:c77f1ad8d993 185 cs=0;
wim 0:c77f1ad8d993 186 wait_us(1);
wim 0:c77f1ad8d993 187 spi.write(flip(0x03)); // display mode cmd, 7 digits, 11 segments
wim 0:c77f1ad8d993 188 wait_us(1);
wim 0:c77f1ad8d993 189 cs=1;
wim 0:c77f1ad8d993 190
wim 0:c77f1ad8d993 191 wait_us(50);
wim 0:c77f1ad8d993 192
wim 0:c77f1ad8d993 193 cs=0;
wim 0:c77f1ad8d993 194 wait_us(1);
wim 0:c77f1ad8d993 195 spi.write(flip(0x40)); // data set cmd, normal mode, auto incr, write data
wim 0:c77f1ad8d993 196 wait_us(1);
wim 0:c77f1ad8d993 197 cs=1;
wim 0:c77f1ad8d993 198
wim 0:c77f1ad8d993 199 wait_us(50);
wim 0:c77f1ad8d993 200
wim 0:c77f1ad8d993 201 cs=0;
wim 0:c77f1ad8d993 202 wait_us(1);
wim 0:c77f1ad8d993 203 spi.write(flip(0x8B)); // display control cmd, display On, 10/16 pwm
wim 0:c77f1ad8d993 204 wait_us(1);
wim 0:c77f1ad8d993 205 cs=1;
wim 0:c77f1ad8d993 206
wim 0:c77f1ad8d993 207 wait_us(50);
wim 0:c77f1ad8d993 208
wim 0:c77f1ad8d993 209 fill();
wim 0:c77f1ad8d993 210
wim 0:c77f1ad8d993 211 pc.printf("Press key\r\n"); //
wim 0:c77f1ad8d993 212 c=pc.getc();
wim 0:c77f1ad8d993 213
wim 0:c77f1ad8d993 214 clear();
wim 0:c77f1ad8d993 215
wim 0:c77f1ad8d993 216 #if(0)
wim 0:c77f1ad8d993 217 for (int digit=0; digit<7; digit++) {
wim 0:c77f1ad8d993 218 for (int seg=0; seg<12; seg++) {
wim 0:c77f1ad8d993 219 data = 1 << seg;
wim 0:c77f1ad8d993 220
wim 0:c77f1ad8d993 221 cs=0;
wim 0:c77f1ad8d993 222 wait_us(1);
wim 0:c77f1ad8d993 223 spi.write(flip(0xC0 + (digit << 1))); // address set cmd, 0
wim 0:c77f1ad8d993 224 spi.write(flip( data & 0xFF)); // data
wim 0:c77f1ad8d993 225 spi.write(flip((data >> 8) & 0x07)); // data
wim 0:c77f1ad8d993 226 wait_us(1);
wim 0:c77f1ad8d993 227 cs=1;
wim 0:c77f1ad8d993 228
wim 0:c77f1ad8d993 229 //wait(0.5);
wim 0:c77f1ad8d993 230 pc.printf("Dig=%d, Seg=%d, Press key\r\n", digit, seg); //
wim 0:c77f1ad8d993 231 c=pc.getc();
wim 0:c77f1ad8d993 232
wim 0:c77f1ad8d993 233 } //seg
wim 0:c77f1ad8d993 234
wim 0:c77f1ad8d993 235 cs=0;
wim 0:c77f1ad8d993 236 wait_us(1);
wim 0:c77f1ad8d993 237 spi.write(flip(0xC0 + digit)); // address set cmd, 0
wim 0:c77f1ad8d993 238 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 239 spi.write(flip(0x00)); // data
wim 0:c77f1ad8d993 240 wait_us(1);
wim 0:c77f1ad8d993 241 cs=1;
wim 0:c77f1ad8d993 242
wim 0:c77f1ad8d993 243 } //dig
wim 0:c77f1ad8d993 244
wim 0:c77f1ad8d993 245 #endif
wim 0:c77f1ad8d993 246 const char mbed[] = {0xDA,0x00, 0x7C,0x00, 0x3C,0x01, 0xF6,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00};
wim 0:c77f1ad8d993 247 cs=0;
wim 0:c77f1ad8d993 248 wait_us(1);
wim 0:c77f1ad8d993 249 spi.write(flip(0xC0)); // address set cmd, 0
wim 0:c77f1ad8d993 250 for (int dig=0; dig<7; dig++) {
wim 0:c77f1ad8d993 251 spi.write(flip(mbed[(dig<<1)])); // data
wim 0:c77f1ad8d993 252 spi.write(flip(mbed[(dig<<1) + 1])); // data
wim 0:c77f1ad8d993 253 }
wim 0:c77f1ad8d993 254 wait_us(1);
wim 0:c77f1ad8d993 255 cs=1;
wim 0:c77f1ad8d993 256
wim 0:c77f1ad8d993 257 while(1) {
wim 0:c77f1ad8d993 258 myled = 1;
wim 0:c77f1ad8d993 259 wait(0.5);
wim 0:c77f1ad8d993 260 myled = 0;
wim 0:c77f1ad8d993 261 wait(0.5);
wim 0:c77f1ad8d993 262
wim 0:c77f1ad8d993 263 cs=0;
wim 0:c77f1ad8d993 264 wait_us(1);
wim 0:c77f1ad8d993 265 spi.write(flip(0x42)); // data set cmd, normal mode, auto incr, read data
wim 0:c77f1ad8d993 266 data = spi.write(0xFF); // read keys 0
wim 0:c77f1ad8d993 267 pc.printf("data=0x%02x ", flip(data)); //
wim 0:c77f1ad8d993 268 data = spi.write(0xFF); // read keys 1
wim 0:c77f1ad8d993 269 pc.printf("data=0x%02x ", flip(data)); //
wim 0:c77f1ad8d993 270 data = spi.write(0xFF); // read keys 2
wim 0:c77f1ad8d993 271 pc.printf("data=0x%02x ", flip(data)); //
wim 0:c77f1ad8d993 272 data = spi.write(0xFF); // read keys 3
wim 0:c77f1ad8d993 273 pc.printf("data=0x%02x ", flip(data)); //
wim 0:c77f1ad8d993 274 data = spi.write(0xFF); // read keys 4
wim 0:c77f1ad8d993 275 pc.printf("data=0x%02x\r\n", flip(data)); //
wim 0:c77f1ad8d993 276 wait_us(1);
wim 0:c77f1ad8d993 277 cs=1;
wim 0:c77f1ad8d993 278 }
wim 0:c77f1ad8d993 279 }
wim 0:c77f1ad8d993 280
wim 0:c77f1ad8d993 281 #endif