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.
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
PT6961.cpp
- Committer:
- wim
- Date:
- 2015-09-20
- Revision:
- 0:feebe8eca523
- Child:
- 1:eb4758bba68a
File content as of revision 0:feebe8eca523:
/* mbed PT6961 Library, for Princeton PT6961 LED controller * Copyright (c) 2015, v01: WH, Initial version * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "mbed.h" #include "PT6961.h" /** Constructor for class for driving Princeton PT6961 LED controller with SPI bus interface device. * @brief Supports 6 Digits or 12 Segments or 7 Digits of 11 Segments. Also supports a scanned keyboard of upto 30 keys. * * @param PinName mosi, miso, sclk, cs SPI bus pins * @param Mode selects either 6 Digits of 12 Segments or 7 Digits of 11 Segments (default) */ PT6961::PT6961(PinName mosi, PinName miso, PinName sclk, PinName cs, Mode mode) : _spi(mosi,miso,sclk), _cs(cs), _mode(mode) { _init(); } /** Init the SPI interface and the controller * @param none * @return none */ void PT6961::_init(){ //init SPI _cs=1; _spi.format(8,3); //PT6961 uses mode 3 (Clock High on Idle, Data latched on second (=rising) edge) _spi.frequency(500000); //init controller _writeCmd(PT6961_MODE_SET_CMD, _mode); // Mode set command _display = PT6961_DSP_ON; _bright = PT6961_BRT_DEF; _writeCmd(PT6961_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness _writeCmd(PT6961_DATA_SET_CMD, PT6961_DATA_WR | PT6961_ADDR_INC | PT6961_MODE_NORM); // Data set cmd, normal mode, auto incr, write data } /** Clear the screen and locate to 0 */ void PT6961::cls() { _cs=0; wait_us(1); _spi.write(_flip(PT6961_ADDR_SET_CMD | 0x00)); // Address set cmd, 0 for (int cnt=0; cnt<PT6961_DISPLAY_MEM; cnt++) { _spi.write(0x00); // data } wait_us(1); _cs=1; } /** Set Brightness * * @param char brightness (3 significant bits, valid range 0..7 (1/16 .. 14/14 dutycycle) * @return none */ void PT6961::setBrightness(char brightness){ _bright = brightness & PT6961_BRT_MSK; // mask invalid bits _writeCmd(PT6961_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness } /** Set the Display mode On/off * * @param bool display mode */ void PT6961::setDisplay(bool on) { if (on) { _display = PT6961_DSP_ON; } else { _display = PT6961_DSP_OFF; } _writeCmd(PT6961_DSP_CTRL_CMD, _display | _bright ); // Display control cmd, display on/off, brightness } /** Write databyte to PT6961 * @param int address display memory location to write byte * @param char data byte written at given address * @return none */ void PT6961::writeData(int address, char data) { _cs=0; wait_us(1); _spi.write(_flip(PT6961_ADDR_SET_CMD | (address & PT6961_ADDR_MSK))); // Set Address cmd _spi.write(_flip(data)); // data wait_us(1); _cs=1; } /** Write Display datablock to PT6961 * @param DisplayData_t data Array of PT6961_DISPLAY_MEM (=14) bytes for displaydata (starting at address 0) * @param length number bytes to write (valide range 0..PT6961_DISPLAY_MEM (=14), starting at address 0) * @return none */ void PT6961::writeData(DisplayData_t data, int length) { _cs=0; wait_us(1); _spi.write(_flip(PT6961_ADDR_SET_CMD | 0x00)); // Set Address at 0 // sanity check if (length < 0) {length = 0;} if (length > PT6961_DISPLAY_MEM) {length = PT6961_DISPLAY_MEM;} // for (int idx=0; idx<PT6961_DISPLAY_MEM; idx++) { for (int idx=0; idx<length; idx++) { _spi.write(_flip(data[idx])); // data } wait_us(1); _cs=1; } /** Read keydata block from PT6961 * @param *keydata Ptr to Array of PT6961_KEY_MEM (=5) bytes for keydata * @return bool keypress True when at least one key was pressed * * Note: Due to the hardware configuration the PT6961 key matrix scanner will detect multiple keys pressed at same time, * but this may also result in some spurious keys being set in keypress data array. * It may be best to ignore all keys in those situations. That option is implemented in this method depending on #define setting. */ bool PT6961::getKeys(KeyData_t *keydata) { int keypress = 0; char data; // Read keys _cs=0; wait_us(1); // Enable Key Read mode _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 for (int idx=0; idx < PT6961_KEY_MEM; idx++) { data = _flip(_spi.write(0xFF)); // read keys and correct bitorder if (data != 0) { // Check for any pressed key for (int bit=0; bit < PT6961_KEY_BITS; bit++) { if (data & (1 << bit)) {keypress++;} // Test all significant bits } } (*keydata)[idx] = data; // Store keydata after correcting bitorder } wait_us(1); _cs=1; // Restore Data Write mode _writeCmd(PT6961_DATA_SET_CMD, PT6961_DATA_WR | PT6961_ADDR_INC | PT6961_MODE_NORM); // Data set cmd, normal mode, auto incr, write data #if(1) // Dismiss multiple keypresses at same time return (keypress == 1); #else // Allow multiple keypress and accept possible spurious keys return (keypress > 0); #endif } /** Helper to reverse all command or databits. The PT6961 expects LSB first, whereas SPI is MSB first * @param char data * @return bitreversed data */ char PT6961::_flip(char data) { char value=0; if (data & 0x01) {value |= 0x80;} ; if (data & 0x02) {value |= 0x40;} ; if (data & 0x04) {value |= 0x20;} ; if (data & 0x08) {value |= 0x10;} ; if (data & 0x10) {value |= 0x08;} ; if (data & 0x20) {value |= 0x04;} ; if (data & 0x40) {value |= 0x02;} ; if (data & 0x80) {value |= 0x01;} ; return value; } /** Write command and parameter to PT6961 * @param int cmd Command byte * &Param int data Parameters for command * @return none */ void PT6961::_writeCmd(int cmd, int data){ _cs=0; wait_us(1); // _spi.write(_flip( (cmd & 0xF0) | (data & 0x0F))); _spi.write(_flip( (cmd & PT6961_CMD_MSK) | (data & ~PT6961_CMD_MSK))); wait_us(1); _cs=1; }