Library for Sure Electronics HT1632 based LED matrix displays. Supports multiple displays connected together.
Dependents: HT1632MsgScroller SMS_LEDMatrixPrinter
HT1632_LedMatrix.cpp@9:8a3c981babd9, 2012-11-28 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Wed Nov 28 14:03:35 2012 +0000
- Revision:
- 9:8a3c981babd9
- Parent:
- 8:61130f9b5b79
- Child:
- 10:af973a9c48b2
Added documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 6:80f554fd77a0 | 1 | /** Library for Holtek HT1632 LED driver chip, |
SomeRandomBloke | 6:80f554fd77a0 | 2 | * As implemented on the Sure Electronics DE-DP10X display board |
SomeRandomBloke | 6:80f554fd77a0 | 3 | * 8 x 32 dot matrix LED module.) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 4 | * |
SomeRandomBloke | 0:b3e0f5bb3b87 | 5 | * Original code by: |
SomeRandomBloke | 0:b3e0f5bb3b87 | 6 | * Nov, 2008 by Bill Westfield ("WestfW") |
SomeRandomBloke | 0:b3e0f5bb3b87 | 7 | * Copyrighted and distributed under the terms of the Berkely license |
SomeRandomBloke | 0:b3e0f5bb3b87 | 8 | * (copy freely, but include this notice of original author.) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 9 | * |
SomeRandomBloke | 0:b3e0f5bb3b87 | 10 | * Adapted for 8x32 display by FlorinC. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 11 | * |
SomeRandomBloke | 2:3e477c909f7a | 12 | * Arduino Library Created and updated by Andrew Lindsay October/November 2009 |
SomeRandomBloke | 2:3e477c909f7a | 13 | * |
SomeRandomBloke | 2:3e477c909f7a | 14 | * Ported to Mbed platform by Andrew Lindsay, November 2012 |
SomeRandomBloke | 6:80f554fd77a0 | 15 | * |
SomeRandomBloke | 6:80f554fd77a0 | 16 | * @author Andrew Lindsay |
SomeRandomBloke | 6:80f554fd77a0 | 17 | * |
SomeRandomBloke | 6:80f554fd77a0 | 18 | * @section LICENSE |
SomeRandomBloke | 6:80f554fd77a0 | 19 | * |
SomeRandomBloke | 6:80f554fd77a0 | 20 | * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk) |
SomeRandomBloke | 6:80f554fd77a0 | 21 | * |
SomeRandomBloke | 6:80f554fd77a0 | 22 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
SomeRandomBloke | 6:80f554fd77a0 | 23 | * of this software and associated documentation files (the "Software"), to deal |
SomeRandomBloke | 6:80f554fd77a0 | 24 | * in the Software without restriction, including without limitation the rights |
SomeRandomBloke | 6:80f554fd77a0 | 25 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
SomeRandomBloke | 6:80f554fd77a0 | 26 | * copies of the Software, and to permit persons to whom the Software is |
SomeRandomBloke | 6:80f554fd77a0 | 27 | * furnished to do so, subject to the following conditions: |
SomeRandomBloke | 6:80f554fd77a0 | 28 | |
SomeRandomBloke | 6:80f554fd77a0 | 29 | * The above copyright notice and this permission notice shall be included in |
SomeRandomBloke | 6:80f554fd77a0 | 30 | * all copies or substantial portions of the Software. |
SomeRandomBloke | 6:80f554fd77a0 | 31 | * |
SomeRandomBloke | 6:80f554fd77a0 | 32 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
SomeRandomBloke | 6:80f554fd77a0 | 33 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
SomeRandomBloke | 6:80f554fd77a0 | 34 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
SomeRandomBloke | 6:80f554fd77a0 | 35 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
SomeRandomBloke | 6:80f554fd77a0 | 36 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
SomeRandomBloke | 6:80f554fd77a0 | 37 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
SomeRandomBloke | 6:80f554fd77a0 | 38 | * THE SOFTWARE. |
SomeRandomBloke | 6:80f554fd77a0 | 39 | * |
SomeRandomBloke | 6:80f554fd77a0 | 40 | * @section DESCRIPTION |
SomeRandomBloke | 6:80f554fd77a0 | 41 | * Functions to drive displays |
SomeRandomBloke | 6:80f554fd77a0 | 42 | * |
SomeRandomBloke | 6:80f554fd77a0 | 43 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 44 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 45 | #include "mbed.h" |
SomeRandomBloke | 0:b3e0f5bb3b87 | 46 | #include "HT1632_LedMatrix.h" |
SomeRandomBloke | 0:b3e0f5bb3b87 | 47 | #include "font_5x7_p.h" |
SomeRandomBloke | 0:b3e0f5bb3b87 | 48 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 49 | #define HIGH 1 |
SomeRandomBloke | 0:b3e0f5bb3b87 | 50 | #define LOW 0 |
SomeRandomBloke | 0:b3e0f5bb3b87 | 51 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 52 | * Set these constants to the values of the pins connected to the SureElectronics Module |
SomeRandomBloke | 6:80f554fd77a0 | 53 | * For mbed, use WR=p7, DATA=p5, cs1=p17, cs2=p18, cs3=p19, cs4=p20 |
SomeRandomBloke | 0:b3e0f5bb3b87 | 54 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 55 | |
SomeRandomBloke | 6:80f554fd77a0 | 56 | DigitalOut ht1632_wrclk(p7); |
SomeRandomBloke | 6:80f554fd77a0 | 57 | DigitalOut ht1632_data(p5); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 58 | DigitalOut ht1632_cs1(p17); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 59 | DigitalOut ht1632_cs2(p18); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 60 | DigitalOut ht1632_cs3(p19); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 61 | DigitalOut ht1632_cs4(p20); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 62 | |
SomeRandomBloke | 2:3e477c909f7a | 63 | // CS pins 17, 18, 19, 20 used. |
SomeRandomBloke | 6:80f554fd77a0 | 64 | DigitalOut ht1632_cs[4] = {p17, p18, p19, p20}; // Chip Select (1, 2, 3, 4) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 65 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 66 | // helper macros |
SomeRandomBloke | 0:b3e0f5bb3b87 | 67 | #define chip_number(x,y) (x >> 5) + (y >> 3)*numYDevices |
SomeRandomBloke | 3:48f430fe186e | 68 | #define chip_nibble_address(x,y) ((x%32)<<1) + ((y%8)>>2); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 69 | #define chip_byte_address(x,y) ((x%32)<<1); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 70 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 71 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 72 | // Display size and configuration, defaul is for a single 8x32 display |
SomeRandomBloke | 0:b3e0f5bb3b87 | 73 | uint8_t numDevices = 1; // Total number of devices |
SomeRandomBloke | 0:b3e0f5bb3b87 | 74 | uint8_t numXDevices = 1; // Number of horizontal devices |
SomeRandomBloke | 0:b3e0f5bb3b87 | 75 | uint8_t numYDevices = 1; // Number of vertical devices |
SomeRandomBloke | 0:b3e0f5bb3b87 | 76 | uint8_t xMax = 32 * numXDevices-1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 77 | uint8_t yMax = 8 * numYDevices-1; |
SomeRandomBloke | 9:8a3c981babd9 | 78 | uint8_t displayWidth = 32; |
SomeRandomBloke | 9:8a3c981babd9 | 79 | uint8_t displayHeight = 8; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 80 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 81 | // Variables used to keep track of cursor position |
SomeRandomBloke | 0:b3e0f5bb3b87 | 82 | int cursorX = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 83 | int cursorY = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 84 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 85 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 86 | * we keep a copy of the display controller contents so that we can |
SomeRandomBloke | 0:b3e0f5bb3b87 | 87 | * know which bits are on without having to (slowly) read the device. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 88 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 89 | // 4 boards at 32 bytes per board + 1 byte means we don't need to check overwrite in putChar |
SomeRandomBloke | 0:b3e0f5bb3b87 | 90 | uint8_t shadowram[129]; // our copy of the display's RAM |
SomeRandomBloke | 0:b3e0f5bb3b87 | 91 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 92 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 93 | // Custom character buffers - 8 characters available |
SomeRandomBloke | 0:b3e0f5bb3b87 | 94 | // 6 cols * 8 rows - first byte of each char is the number of columns used |
SomeRandomBloke | 0:b3e0f5bb3b87 | 95 | // Bits are aranged in columns with LSB at top |
SomeRandomBloke | 3:48f430fe186e | 96 | uint8_t cgram [8][7] = { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 97 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 0:b3e0f5bb3b87 | 98 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 0:b3e0f5bb3b87 | 99 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 0:b3e0f5bb3b87 | 100 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 0:b3e0f5bb3b87 | 101 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 0:b3e0f5bb3b87 | 102 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 0:b3e0f5bb3b87 | 103 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 3:48f430fe186e | 104 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
SomeRandomBloke | 3:48f430fe186e | 105 | }; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 106 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 107 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 108 | // Default constructor |
SomeRandomBloke | 3:48f430fe186e | 109 | HT1632_LedMatrix::HT1632_LedMatrix( void ) |
SomeRandomBloke | 3:48f430fe186e | 110 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 111 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 112 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 113 | |
SomeRandomBloke | 9:8a3c981babd9 | 114 | void HT1632_LedMatrix::init( uint8_t xDevices, uint8_t yDevices, uint8_t displayType ) |
SomeRandomBloke | 3:48f430fe186e | 115 | { |
SomeRandomBloke | 3:48f430fe186e | 116 | // Set up the display size based on number of devices both horizontal and vertical |
SomeRandomBloke | 9:8a3c981babd9 | 117 | |
SomeRandomBloke | 3:48f430fe186e | 118 | numXDevices = xDevices; |
SomeRandomBloke | 3:48f430fe186e | 119 | numYDevices = yDevices; |
SomeRandomBloke | 3:48f430fe186e | 120 | numDevices = numXDevices * numYDevices; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 121 | |
SomeRandomBloke | 9:8a3c981babd9 | 122 | if( displayType == HT1632_24x16 ) { |
SomeRandomBloke | 9:8a3c981babd9 | 123 | displayWidth = 24; |
SomeRandomBloke | 9:8a3c981babd9 | 124 | displayHeight = 16; |
SomeRandomBloke | 9:8a3c981babd9 | 125 | } else { |
SomeRandomBloke | 9:8a3c981babd9 | 126 | displayWidth = 32; |
SomeRandomBloke | 9:8a3c981babd9 | 127 | displayHeight = 8; |
SomeRandomBloke | 9:8a3c981babd9 | 128 | } |
SomeRandomBloke | 9:8a3c981babd9 | 129 | xMax = displayWidth * numXDevices-1; |
SomeRandomBloke | 9:8a3c981babd9 | 130 | yMax = displayHeight * numYDevices-1; |
SomeRandomBloke | 9:8a3c981babd9 | 131 | |
SomeRandomBloke | 3:48f430fe186e | 132 | // Disable all display CS lines by taking high |
SomeRandomBloke | 3:48f430fe186e | 133 | for( uint8_t i = 0; i < 4; i++ ) |
SomeRandomBloke | 3:48f430fe186e | 134 | ht1632_cs[i] = HIGH; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 135 | |
SomeRandomBloke | 4:7513dd37efed | 136 | for (uint8_t chipno=0; chipno<4; chipno++) { |
SomeRandomBloke | 3:48f430fe186e | 137 | chipfree(chipno); // unselect it |
SomeRandomBloke | 8:61130f9b5b79 | 138 | |
SomeRandomBloke | 8:61130f9b5b79 | 139 | sendcmd(chipno, HT1632_CMD_SYSON); // System on |
SomeRandomBloke | 8:61130f9b5b79 | 140 | sendcmd(chipno, HT1632_CMD_LEDON); // LEDs on |
SomeRandomBloke | 8:61130f9b5b79 | 141 | sendcmd(chipno, HT1632_CMD_BLOFF); // Blink Off |
SomeRandomBloke | 8:61130f9b5b79 | 142 | sendcmd(chipno, HT1632_CMD_MSTMD); // Master Mode |
SomeRandomBloke | 9:8a3c981babd9 | 143 | sendcmd(chipno, HT1632_CMD_RCCLK); // Internal Oscillator |
SomeRandomBloke | 9:8a3c981babd9 | 144 | if( displayType == HT1632_24x16 ) { |
SomeRandomBloke | 9:8a3c981babd9 | 145 | // TODO - check |
SomeRandomBloke | 9:8a3c981babd9 | 146 | sendcmd(chipno, HT1632_CMD_COMS10); // 16*24, NMOS drivers |
SomeRandomBloke | 9:8a3c981babd9 | 147 | } else { |
SomeRandomBloke | 9:8a3c981babd9 | 148 | sendcmd(chipno, HT1632_CMD_COMS00); // 08*32, NMOS drivers |
SomeRandomBloke | 9:8a3c981babd9 | 149 | } |
SomeRandomBloke | 3:48f430fe186e | 150 | sendcmd(chipno, HT1632_CMD_PWM | 0x0c); // PWM Duty |
SomeRandomBloke | 8:61130f9b5b79 | 151 | |
SomeRandomBloke | 3:48f430fe186e | 152 | for (uint8_t i=0; i<96; i++) |
SomeRandomBloke | 3:48f430fe186e | 153 | senddata(chipno, i, 0); // clear the display |
SomeRandomBloke | 3:48f430fe186e | 154 | wait(0.1); |
SomeRandomBloke | 3:48f430fe186e | 155 | } |
SomeRandomBloke | 3:48f430fe186e | 156 | cursorX = 0; |
SomeRandomBloke | 3:48f430fe186e | 157 | cursorY = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 158 | } |
SomeRandomBloke | 3:48f430fe186e | 159 | |
SomeRandomBloke | 6:80f554fd77a0 | 160 | void HT1632_LedMatrix::displayOff( void ) |
SomeRandomBloke | 6:80f554fd77a0 | 161 | { |
SomeRandomBloke | 5:33b2bfce06b7 | 162 | for (uint8_t chipno=0; chipno<4; chipno++) { |
SomeRandomBloke | 5:33b2bfce06b7 | 163 | chipfree(chipno); // unselect it |
SomeRandomBloke | 5:33b2bfce06b7 | 164 | sendcmd(chipno, HT1632_CMD_LEDOFF); // LEDs on |
SomeRandomBloke | 5:33b2bfce06b7 | 165 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 166 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 167 | |
SomeRandomBloke | 6:80f554fd77a0 | 168 | void HT1632_LedMatrix::displayOn( void ) |
SomeRandomBloke | 6:80f554fd77a0 | 169 | { |
SomeRandomBloke | 5:33b2bfce06b7 | 170 | for (uint8_t chipno=0; chipno<4; chipno++) { |
SomeRandomBloke | 5:33b2bfce06b7 | 171 | chipfree(chipno); // unselect it |
SomeRandomBloke | 5:33b2bfce06b7 | 172 | sendcmd(chipno, HT1632_CMD_LEDON); // LEDs on |
SomeRandomBloke | 5:33b2bfce06b7 | 173 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 174 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 175 | |
SomeRandomBloke | 5:33b2bfce06b7 | 176 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 177 | /*********************************************************************** |
SomeRandomBloke | 0:b3e0f5bb3b87 | 178 | * chipselect / chipfree |
SomeRandomBloke | 0:b3e0f5bb3b87 | 179 | * Select or de-select a particular ht1632 chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 180 | * De-selecting a chip ends the commands being sent to a chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 181 | * CD pins are active-low; writing 0 to the pin selects the chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 182 | ***********************************************************************/ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 183 | void HT1632_LedMatrix::chipselect(uint8_t chipno) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 184 | { |
SomeRandomBloke | 8:61130f9b5b79 | 185 | // for(int i=0; i<4; i++) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 186 | ht1632_cs[chipno] = LOW; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 187 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 188 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 189 | void HT1632_LedMatrix::chipfree(uint8_t chipno) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 190 | { |
SomeRandomBloke | 8:61130f9b5b79 | 191 | // for(int i=0; i<4; i++) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 192 | ht1632_cs[chipno] = HIGH; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 193 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 194 | |
SomeRandomBloke | 9:8a3c981babd9 | 195 | /** |
SomeRandomBloke | 0:b3e0f5bb3b87 | 196 | * writebits |
SomeRandomBloke | 9:8a3c981babd9 | 197 | * Write bits to HT1632 on pins HT1632_DATA, HT1632_WRCLK |
SomeRandomBloke | 0:b3e0f5bb3b87 | 198 | * Chip is assumed to already be chip-selected |
SomeRandomBloke | 0:b3e0f5bb3b87 | 199 | * Bits are shifted out from MSB to LSB, with the first bit sent |
SomeRandomBloke | 0:b3e0f5bb3b87 | 200 | * being (bits & firstbit), shifted till firsbit is zero. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 201 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 202 | void HT1632_LedMatrix::writebits (uint8_t bits, uint8_t firstbit) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 203 | { |
SomeRandomBloke | 3:48f430fe186e | 204 | while (firstbit) { |
SomeRandomBloke | 3:48f430fe186e | 205 | ht1632_wrclk = LOW; |
SomeRandomBloke | 3:48f430fe186e | 206 | if (bits & firstbit) { |
SomeRandomBloke | 3:48f430fe186e | 207 | ht1632_data = HIGH; |
SomeRandomBloke | 3:48f430fe186e | 208 | } else { |
SomeRandomBloke | 3:48f430fe186e | 209 | ht1632_data = LOW; |
SomeRandomBloke | 3:48f430fe186e | 210 | } |
SomeRandomBloke | 3:48f430fe186e | 211 | ht1632_wrclk = HIGH; |
SomeRandomBloke | 3:48f430fe186e | 212 | firstbit >>= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 213 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 214 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 215 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 216 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 217 | * writedatabits |
SomeRandomBloke | 9:8a3c981babd9 | 218 | * Write databits to HT1632 on pins HT1632_DATA, HT1632_WRCLK |
SomeRandomBloke | 0:b3e0f5bb3b87 | 219 | * Chip is assumed to already be chip-selected |
SomeRandomBloke | 0:b3e0f5bb3b87 | 220 | * Bits are shifted out from LSB to MSB |
SomeRandomBloke | 0:b3e0f5bb3b87 | 221 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 222 | void HT1632_LedMatrix::writedatabits (uint8_t bits, uint8_t count) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 223 | { |
SomeRandomBloke | 3:48f430fe186e | 224 | while (count) { |
SomeRandomBloke | 3:48f430fe186e | 225 | ht1632_wrclk = LOW; |
SomeRandomBloke | 3:48f430fe186e | 226 | ht1632_data = bits & 1; |
SomeRandomBloke | 3:48f430fe186e | 227 | ht1632_wrclk = HIGH; |
SomeRandomBloke | 3:48f430fe186e | 228 | count--; |
SomeRandomBloke | 3:48f430fe186e | 229 | bits >>= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 230 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 231 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 232 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 233 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 234 | * sendcmd |
SomeRandomBloke | 0:b3e0f5bb3b87 | 235 | * Send a command to the ht1632 chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 236 | * A command consists of a 3-bit "CMD" ID, an 8bit command, and |
SomeRandomBloke | 0:b3e0f5bb3b87 | 237 | * one "don't care bit". |
SomeRandomBloke | 0:b3e0f5bb3b87 | 238 | * Select 1 0 0 c7 c6 c5 c4 c3 c2 c1 c0 xx Free |
SomeRandomBloke | 0:b3e0f5bb3b87 | 239 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 240 | void HT1632_LedMatrix::sendcmd (uint8_t chipno, uint8_t command) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 241 | { |
SomeRandomBloke | 3:48f430fe186e | 242 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 243 | writebits(HT1632_ID_CMD, 0x04); // send 3 bits of id: COMMMAND |
SomeRandomBloke | 3:48f430fe186e | 244 | writebits(command, 0x80); // send the actual command |
SomeRandomBloke | 3:48f430fe186e | 245 | writebits(0, 1); // one extra dont-care bit in commands. |
SomeRandomBloke | 3:48f430fe186e | 246 | chipfree(chipno); //done |
SomeRandomBloke | 0:b3e0f5bb3b87 | 247 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 248 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 249 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 250 | * clear |
SomeRandomBloke | 0:b3e0f5bb3b87 | 251 | * clear the display, and the shadow memory, and the snapshot |
SomeRandomBloke | 0:b3e0f5bb3b87 | 252 | * memory. This uses the "write multiple words" capability of |
SomeRandomBloke | 0:b3e0f5bb3b87 | 253 | * the chipset by writing all 96 words of memory without raising |
SomeRandomBloke | 0:b3e0f5bb3b87 | 254 | * the chipselect signal. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 255 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 256 | void HT1632_LedMatrix::clear() |
SomeRandomBloke | 0:b3e0f5bb3b87 | 257 | { |
SomeRandomBloke | 3:48f430fe186e | 258 | char i; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 259 | |
SomeRandomBloke | 3:48f430fe186e | 260 | for (uint8_t chipno=0; chipno<numDevices; chipno++) { |
SomeRandomBloke | 3:48f430fe186e | 261 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 262 | writebits(HT1632_ID_WR, 0x04); // send ID: WRITE to RAM |
SomeRandomBloke | 3:48f430fe186e | 263 | writebits(0, 0x40); // Send address |
SomeRandomBloke | 9:8a3c981babd9 | 264 | for (i = 0; i < displayWidth; i++) // Clear entire display |
SomeRandomBloke | 3:48f430fe186e | 265 | writedatabits(0, 8); // send 8 bits of data |
SomeRandomBloke | 3:48f430fe186e | 266 | chipfree(chipno); // done |
SomeRandomBloke | 3:48f430fe186e | 267 | for (i=0; i < 64; i++) |
SomeRandomBloke | 3:48f430fe186e | 268 | shadowram[i+64*chipno] = 0; |
SomeRandomBloke | 3:48f430fe186e | 269 | } |
SomeRandomBloke | 3:48f430fe186e | 270 | cursorX = 0; |
SomeRandomBloke | 3:48f430fe186e | 271 | cursorY = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 272 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 273 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 274 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 275 | // Brighness is from 0 to 15 |
SomeRandomBloke | 3:48f430fe186e | 276 | void HT1632_LedMatrix::setBrightness( unsigned char brightness ) |
SomeRandomBloke | 3:48f430fe186e | 277 | { |
SomeRandomBloke | 3:48f430fe186e | 278 | for (uint8_t chipno=0; chipno<numDevices; chipno++) { |
SomeRandomBloke | 3:48f430fe186e | 279 | sendcmd(chipno, HT1632_CMD_PWM | (brightness & 0x0F )); |
SomeRandomBloke | 3:48f430fe186e | 280 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 281 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 282 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 283 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 284 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 285 | * senddata |
SomeRandomBloke | 0:b3e0f5bb3b87 | 286 | * send a nibble (4 bits) of data to a particular memory location of the |
SomeRandomBloke | 0:b3e0f5bb3b87 | 287 | * ht1632. The command has 3 bit ID, 7 bits of address, and 4 bits of data. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 288 | * Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 Free |
SomeRandomBloke | 0:b3e0f5bb3b87 | 289 | * Note that the address is sent MSB first, while the data is sent LSB first! |
SomeRandomBloke | 0:b3e0f5bb3b87 | 290 | * This means that somewhere a bit reversal will have to be done to get |
SomeRandomBloke | 0:b3e0f5bb3b87 | 291 | * zero-based addressing of words and dots within words. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 292 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 293 | void HT1632_LedMatrix::senddata (uint8_t chipno, uint8_t address, uint8_t data) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 294 | { |
SomeRandomBloke | 3:48f430fe186e | 295 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 296 | writebits(HT1632_ID_WR, 0x04); // send ID: WRITE to RAM |
SomeRandomBloke | 3:48f430fe186e | 297 | writebits(address, 0x40); // Send address |
SomeRandomBloke | 3:48f430fe186e | 298 | writedatabits(data, 4); // send 4 bits of data |
SomeRandomBloke | 3:48f430fe186e | 299 | chipfree(chipno); // done |
SomeRandomBloke | 0:b3e0f5bb3b87 | 300 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 301 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 302 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 303 | * sendcol |
SomeRandomBloke | 0:b3e0f5bb3b87 | 304 | * send a byte of data to a particular memory location of the |
SomeRandomBloke | 0:b3e0f5bb3b87 | 305 | * ht1632. The command has 3 bit ID, 7 bits of address, and 8 bits of data. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 306 | * Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 D4 D5 D6 D7 D8 Free |
SomeRandomBloke | 0:b3e0f5bb3b87 | 307 | * Note that the address is sent MSB first, while the data is sent LSB first! |
SomeRandomBloke | 0:b3e0f5bb3b87 | 308 | * This means that somewhere a bit reversal will have to be done to get |
SomeRandomBloke | 0:b3e0f5bb3b87 | 309 | * zero-based addressing of words and dots within words. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 310 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 311 | void HT1632_LedMatrix::sendcol (uint8_t chipno, uint8_t address, uint8_t data) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 312 | { |
SomeRandomBloke | 3:48f430fe186e | 313 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 314 | writebits(HT1632_ID_WR, 0x04); // send ID: WRITE to RAM |
SomeRandomBloke | 3:48f430fe186e | 315 | writebits(address, 0x40); // Send address |
SomeRandomBloke | 3:48f430fe186e | 316 | writedatabits(data, 8); // send 8 bits of data |
SomeRandomBloke | 3:48f430fe186e | 317 | chipfree(chipno); // done |
SomeRandomBloke | 0:b3e0f5bb3b87 | 318 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 319 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 320 | // Write a string at the position specified |
SomeRandomBloke | 4:7513dd37efed | 321 | // x and y start from 0 and count number of pixels, 2nd row on a 2 row display is y=8 |
SomeRandomBloke | 3:48f430fe186e | 322 | void HT1632_LedMatrix::putString(int x, int y, char *str) |
SomeRandomBloke | 3:48f430fe186e | 323 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 324 | cursorX = x; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 325 | cursorY = y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 326 | while( *str ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 327 | putChar( cursorX, y, *str++ ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 328 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 329 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 330 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 331 | /* |
SomeRandomBloke | 9:8a3c981babd9 | 332 | * Copy a character glyph from the font data structure to |
SomeRandomBloke | 0:b3e0f5bb3b87 | 333 | * display memory, with its upper left at the given coordinate |
SomeRandomBloke | 0:b3e0f5bb3b87 | 334 | * This is unoptimized and simply uses plot() to draw each dot. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 335 | */ |
SomeRandomBloke | 3:48f430fe186e | 336 | void HT1632_LedMatrix::write( uint8_t c) |
SomeRandomBloke | 3:48f430fe186e | 337 | { |
SomeRandomBloke | 3:48f430fe186e | 338 | putChar( cursorX, cursorY, (char)c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 339 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 340 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 341 | /* |
SomeRandomBloke | 9:8a3c981babd9 | 342 | * Copy a character glyph from the font data structure to |
SomeRandomBloke | 0:b3e0f5bb3b87 | 343 | * display memory, with its upper left at the given coordinate |
SomeRandomBloke | 0:b3e0f5bb3b87 | 344 | * This is unoptimized and simply uses plot() to draw each dot. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 345 | * returns number of columns that didn't fit |
SomeRandomBloke | 0:b3e0f5bb3b87 | 346 | */ |
SomeRandomBloke | 3:48f430fe186e | 347 | uint8_t HT1632_LedMatrix::putChar(int x, int y, char c) |
SomeRandomBloke | 3:48f430fe186e | 348 | { |
SomeRandomBloke | 3:48f430fe186e | 349 | // fonts defined for ascii 32 and beyond (index 0 in font array is ascii 32); |
SomeRandomBloke | 3:48f430fe186e | 350 | // CGRAM characters are in range 0 to 15 with 8-15 being repeat of 0-7 |
SomeRandomBloke | 3:48f430fe186e | 351 | // note we force y to be modulo 8 - we do not support writing character to partial y values. |
SomeRandomBloke | 3:48f430fe186e | 352 | |
SomeRandomBloke | 3:48f430fe186e | 353 | uint8_t charIndex; |
SomeRandomBloke | 3:48f430fe186e | 354 | uint8_t colData; |
SomeRandomBloke | 3:48f430fe186e | 355 | uint8_t numCols; |
SomeRandomBloke | 3:48f430fe186e | 356 | uint8_t chipno; |
SomeRandomBloke | 3:48f430fe186e | 357 | uint8_t addr; |
SomeRandomBloke | 3:48f430fe186e | 358 | uint8_t colsLeft = 0; // cols that didn't fit |
SomeRandomBloke | 3:48f430fe186e | 359 | |
SomeRandomBloke | 3:48f430fe186e | 360 | if( c > 15 ) { |
SomeRandomBloke | 3:48f430fe186e | 361 | // Regular characters |
SomeRandomBloke | 3:48f430fe186e | 362 | // replace undisplayable characters with blank; |
SomeRandomBloke | 3:48f430fe186e | 363 | if (c < 32 || c > 126) { |
SomeRandomBloke | 3:48f430fe186e | 364 | charIndex = 0; |
SomeRandomBloke | 3:48f430fe186e | 365 | } else { |
SomeRandomBloke | 3:48f430fe186e | 366 | charIndex = c - 32; |
SomeRandomBloke | 3:48f430fe186e | 367 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 368 | |
SomeRandomBloke | 3:48f430fe186e | 369 | // move character definition, pixel by pixel, onto the display; |
SomeRandomBloke | 3:48f430fe186e | 370 | // fonts are defined as one byte per col; |
SomeRandomBloke | 3:48f430fe186e | 371 | numCols=smallFont[charIndex][6]; // get the number of columns this character uses |
SomeRandomBloke | 3:48f430fe186e | 372 | for (uint8_t col=0; col<numCols; col++) { |
SomeRandomBloke | 3:48f430fe186e | 373 | colData = smallFont[charIndex][col]; |
SomeRandomBloke | 3:48f430fe186e | 374 | chipno = chip_number(x,y); |
SomeRandomBloke | 3:48f430fe186e | 375 | addr = chip_byte_address(x,y); // compute which memory byte this is in |
SomeRandomBloke | 3:48f430fe186e | 376 | if (x <= xMax && y <= yMax) { |
SomeRandomBloke | 9:8a3c981babd9 | 377 | shadowram[(addr>>1) + displayWidth * chipno] = colData; |
SomeRandomBloke | 3:48f430fe186e | 378 | sendcol(chipno,addr,colData); |
SomeRandomBloke | 3:48f430fe186e | 379 | x++; |
SomeRandomBloke | 3:48f430fe186e | 380 | } else { |
SomeRandomBloke | 3:48f430fe186e | 381 | colsLeft++; |
SomeRandomBloke | 3:48f430fe186e | 382 | } |
SomeRandomBloke | 3:48f430fe186e | 383 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 384 | } else { |
SomeRandomBloke | 3:48f430fe186e | 385 | // CGRAM Characters |
SomeRandomBloke | 3:48f430fe186e | 386 | charIndex = c & 0x07; // Only low 3 bits count |
SomeRandomBloke | 3:48f430fe186e | 387 | numCols=cgram[charIndex][0]; // get the number of columns this character uses |
SomeRandomBloke | 3:48f430fe186e | 388 | // fonts are defined as one byte per col; |
SomeRandomBloke | 3:48f430fe186e | 389 | for (uint8_t col=1; col<=numCols; col++) { |
SomeRandomBloke | 3:48f430fe186e | 390 | colData = cgram[charIndex][col]; |
SomeRandomBloke | 3:48f430fe186e | 391 | chipno = chip_number(x,y); |
SomeRandomBloke | 3:48f430fe186e | 392 | addr = chip_byte_address(x,y); // compute which memory byte this is in |
SomeRandomBloke | 3:48f430fe186e | 393 | if (x <= xMax && y <= yMax) { |
SomeRandomBloke | 9:8a3c981babd9 | 394 | shadowram[(addr>>1) + displayWidth * chipno] = colData; |
SomeRandomBloke | 3:48f430fe186e | 395 | sendcol(chipno,addr,colData); |
SomeRandomBloke | 3:48f430fe186e | 396 | x++; |
SomeRandomBloke | 3:48f430fe186e | 397 | } else { |
SomeRandomBloke | 3:48f430fe186e | 398 | colsLeft++; |
SomeRandomBloke | 3:48f430fe186e | 399 | } |
SomeRandomBloke | 3:48f430fe186e | 400 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 401 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 402 | |
SomeRandomBloke | 3:48f430fe186e | 403 | cursorX = x; |
SomeRandomBloke | 3:48f430fe186e | 404 | cursorY = y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 405 | |
SomeRandomBloke | 3:48f430fe186e | 406 | return colsLeft; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 407 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 408 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 409 | // Set position of cursor for writing |
SomeRandomBloke | 3:48f430fe186e | 410 | void HT1632_LedMatrix::gotoXY(int x, int y) |
SomeRandomBloke | 3:48f430fe186e | 411 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 412 | cursorX = x; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 413 | cursorY = y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 414 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 415 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 416 | void HT1632_LedMatrix::getXY(int* x, int* y) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 417 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 418 | *x = cursorX; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 419 | *y = cursorY; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 420 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 421 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 422 | void HT1632_LedMatrix::getXYMax(int* x, int* y) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 423 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 424 | *x = xMax; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 425 | *y = yMax; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 426 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 427 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 428 | // Shift cursor X position a number of positions either left or right. |
SomeRandomBloke | 3:48f430fe186e | 429 | void HT1632_LedMatrix::shiftCursorX(int xinc) |
SomeRandomBloke | 3:48f430fe186e | 430 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 431 | cursorX += xinc; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 432 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 433 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 434 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 435 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 436 | * plot a point on the display, with the upper left hand corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 437 | * being (0,0), and the lower right hand corner being (xMax-1, yMax-1). |
SomeRandomBloke | 0:b3e0f5bb3b87 | 438 | * Note that Y increases going "downward" in contrast with most |
SomeRandomBloke | 0:b3e0f5bb3b87 | 439 | * mathematical coordiate systems, but in common with many displays |
SomeRandomBloke | 0:b3e0f5bb3b87 | 440 | * basic bounds checking used. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 441 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 442 | void HT1632_LedMatrix::plot (int x, int y, char val) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 443 | { |
SomeRandomBloke | 3:48f430fe186e | 444 | if (x<0 || x>xMax || y<0 || y>yMax) |
SomeRandomBloke | 3:48f430fe186e | 445 | return; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 446 | |
SomeRandomBloke | 3:48f430fe186e | 447 | uint8_t chipno = chip_number(x,y); |
SomeRandomBloke | 3:48f430fe186e | 448 | char addr = chip_byte_address(x,y); // compute which memory word this is in |
SomeRandomBloke | 3:48f430fe186e | 449 | char shadowAddress = addr >>1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 450 | |
SomeRandomBloke | 3:48f430fe186e | 451 | char bitval = 1<<(y&7); // compute which bit will need set |
SomeRandomBloke | 0:b3e0f5bb3b87 | 452 | |
SomeRandomBloke | 3:48f430fe186e | 453 | if (val) { // Modify the shadow memory |
SomeRandomBloke | 9:8a3c981babd9 | 454 | shadowram[shadowAddress + displayWidth * chipno] |= bitval; |
SomeRandomBloke | 3:48f430fe186e | 455 | } else { |
SomeRandomBloke | 9:8a3c981babd9 | 456 | shadowram[shadowAddress + displayWidth * chipno] &= ~bitval; |
SomeRandomBloke | 3:48f430fe186e | 457 | } |
SomeRandomBloke | 3:48f430fe186e | 458 | // Now copy the new memory value to the display |
SomeRandomBloke | 9:8a3c981babd9 | 459 | sendcol(chipno, addr, shadowram[shadowAddress + displayWidth * chipno]); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 460 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 461 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 462 | |
SomeRandomBloke | 3:48f430fe186e | 463 | void HT1632_LedMatrix::setCustomChar( int charNum, unsigned char cgchar[] ) |
SomeRandomBloke | 3:48f430fe186e | 464 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 465 | for(int i=1; i<7; i++ ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 466 | cgram[charNum][i] = (uint8_t)cgchar[i]; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 467 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 468 | cgram[charNum][6] = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 469 | cgram[charNum][0] = 6; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 470 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 471 | |
SomeRandomBloke | 3:48f430fe186e | 472 | void HT1632_LedMatrix::setCustomChar( int charNum, unsigned char cgchar[], uint8_t numCols ) |
SomeRandomBloke | 3:48f430fe186e | 473 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 474 | numCols = max(numCols, 6 ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 475 | for(int i=1; i<=numCols; i++ ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 476 | cgram[charNum][i] = (uint8_t)cgchar[i]; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 477 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 478 | cgram[charNum][0] = numCols; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 479 | cgram[charNum][numCols] = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 480 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 481 | |
SomeRandomBloke | 4:7513dd37efed | 482 | void HT1632_LedMatrix::scrollLeft(uint8_t numberCols, uint8_t lineNum ) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 483 | { |
SomeRandomBloke | 4:7513dd37efed | 484 | |
SomeRandomBloke | 4:7513dd37efed | 485 | for (int i=0; i<xMax-numberCols; i++) { |
SomeRandomBloke | 4:7513dd37efed | 486 | shadowram[i]=shadowram[i+numberCols]; |
SomeRandomBloke | 4:7513dd37efed | 487 | } |
SomeRandomBloke | 4:7513dd37efed | 488 | for (int i=xMax-numberCols; i<xMax; i++) { |
SomeRandomBloke | 4:7513dd37efed | 489 | shadowram[i]=0; |
SomeRandomBloke | 4:7513dd37efed | 490 | } |
SomeRandomBloke | 6:80f554fd77a0 | 491 | /* |
SomeRandomBloke | 6:80f554fd77a0 | 492 | for (int i=0; i<128-numberCols-1; i++) { |
SomeRandomBloke | 6:80f554fd77a0 | 493 | shadowram[i]=shadowram[i+numberCols]; |
SomeRandomBloke | 6:80f554fd77a0 | 494 | } |
SomeRandomBloke | 6:80f554fd77a0 | 495 | for (int i=128-numberCols; i<128; i++) { |
SomeRandomBloke | 6:80f554fd77a0 | 496 | shadowram[i]=0; |
SomeRandomBloke | 6:80f554fd77a0 | 497 | } |
SomeRandomBloke | 6:80f554fd77a0 | 498 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 499 | cursorX -= numberCols; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 500 | if (cursorX < 0 ) cursorX = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 501 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 502 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 503 | void HT1632_LedMatrix::putShadowRam() |
SomeRandomBloke | 0:b3e0f5bb3b87 | 504 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 505 | for (int chipno=0; chipno<numDevices; chipno++) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 506 | putShadowRam(chipno); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 507 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 508 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 509 | void HT1632_LedMatrix::putShadowRam(uint8_t chipno) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 510 | { |
SomeRandomBloke | 3:48f430fe186e | 511 | for (int i=0; i<64; i+=2) { |
SomeRandomBloke | 9:8a3c981babd9 | 512 | sendcol(chipno,i,shadowram[(i>>1)+ displayWidth * chipno]); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 513 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 514 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 515 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 516 | #ifdef USE_GRAPHIC |
SomeRandomBloke | 0:b3e0f5bb3b87 | 517 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 518 | * Name : drawLine |
SomeRandomBloke | 0:b3e0f5bb3b87 | 519 | * Description : Draws a line between two points on the display. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 520 | * Argument(s) : x1, y1 - Absolute pixel coordinates for line origin. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 521 | * x2, y2 - Absolute pixel coordinates for line end. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 522 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 0:b3e0f5bb3b87 | 523 | * Return value : none |
SomeRandomBloke | 0:b3e0f5bb3b87 | 524 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 525 | void HT1632_LedMatrix::drawLine(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 3:48f430fe186e | 526 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 527 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 528 | int dx, dy, stepx, stepy, fraction; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 529 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 530 | /* Calculate differential form */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 531 | /* dy y2 - y1 */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 532 | /* -- = ------- */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 533 | /* dx x2 - x1 */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 534 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 535 | /* Take differences */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 536 | dy = y2 - y1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 537 | dx = x2 - x1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 538 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 539 | /* dy is negative */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 540 | if ( dy < 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 541 | dy = -dy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 542 | stepy = -1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 543 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 544 | stepy = 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 545 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 546 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 547 | /* dx is negative */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 548 | if ( dx < 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 549 | dx = -dx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 550 | stepx = -1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 551 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 552 | stepx = 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 553 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 554 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 555 | dx <<= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 556 | dy <<= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 557 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 558 | /* Draw initial position */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 559 | plot( x1, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 560 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 561 | /* Draw next positions until end */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 562 | if ( dx > dy ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 563 | /* Take fraction */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 564 | fraction = dy - ( dx >> 1); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 565 | while ( x1 != x2 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 566 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 567 | y1 += stepy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 568 | fraction -= dx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 569 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 570 | x1 += stepx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 571 | fraction += dy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 572 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 573 | /* Draw calculated point */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 574 | plot( x1, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 575 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 576 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 577 | /* Take fraction */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 578 | fraction = dx - ( dy >> 1); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 579 | while ( y1 != y2 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 580 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 581 | x1 += stepx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 582 | fraction -= dy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 583 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 584 | y1 += stepy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 585 | fraction += dx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 586 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 587 | /* Draw calculated point */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 588 | plot( x1, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 589 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 590 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 591 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 592 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 593 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 594 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 595 | * Name : drawRectangle |
SomeRandomBloke | 0:b3e0f5bb3b87 | 596 | * Description : Draw a rectangle given to top left and bottom right points |
SomeRandomBloke | 0:b3e0f5bb3b87 | 597 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 598 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 3:48f430fe186e | 599 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 0:b3e0f5bb3b87 | 600 | * Return value : none |
SomeRandomBloke | 0:b3e0f5bb3b87 | 601 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 602 | void HT1632_LedMatrix::drawRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 3:48f430fe186e | 603 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 604 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 605 | drawLine( x1, y1, x2, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 606 | drawLine( x1, y1, x1, y2, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 607 | drawLine( x1, y2, x2, y2, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 608 | drawLine( x2, y1, x2, y2, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 609 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 610 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 611 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 612 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 613 | * Name : drawFilledRectangle |
SomeRandomBloke | 0:b3e0f5bb3b87 | 614 | * Description : Draw a filled rectangle given to top left and bottom right points |
SomeRandomBloke | 0:b3e0f5bb3b87 | 615 | * just simply draws horizontal lines where the rectangle would be |
SomeRandomBloke | 0:b3e0f5bb3b87 | 616 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 617 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 618 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 0:b3e0f5bb3b87 | 619 | * Return value : none |
SomeRandomBloke | 0:b3e0f5bb3b87 | 620 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 621 | void HT1632_LedMatrix::drawFilledRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 3:48f430fe186e | 622 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 623 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 624 | for(int i=y1; i <= y2; i++ ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 625 | drawLine( x1, i, x2, i, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 626 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 627 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 628 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 629 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 630 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 631 | * Name : drawCircle |
SomeRandomBloke | 3:48f430fe186e | 632 | * Description : Draw a circle using Bresenham's algorithm. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 633 | * Some small circles will look like squares!! |
SomeRandomBloke | 0:b3e0f5bb3b87 | 634 | * Argument(s) : xc, yc - Centre of circle |
SomeRandomBloke | 0:b3e0f5bb3b87 | 635 | * r - Radius |
SomeRandomBloke | 0:b3e0f5bb3b87 | 636 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 3:48f430fe186e | 637 | * Return value : None |
SomeRandomBloke | 0:b3e0f5bb3b87 | 638 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 639 | void HT1632_LedMatrix::drawCircle(unsigned char xc, unsigned char yc, |
SomeRandomBloke | 3:48f430fe186e | 640 | unsigned char r, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 641 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 642 | int x=0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 643 | int y=r; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 644 | int p=3-(2*r); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 645 | |
SomeRandomBloke | 3:48f430fe186e | 646 | plot( xc+x,yc-y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 647 | |
SomeRandomBloke | 3:48f430fe186e | 648 | for(x=0; x<=y; x++) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 649 | if (p<0) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 650 | y=y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 651 | p=(p+(4*x)+6); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 652 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 653 | y=y-1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 654 | p=p+((4*(x-y)+10)); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 655 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 656 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 657 | plot(xc+x,yc-y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 658 | plot(xc-x,yc-y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 659 | plot(xc+x,yc+y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 660 | plot(xc-x,yc+y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 661 | plot(xc+y,yc-x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 662 | plot(xc-y,yc-x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 663 | plot(xc+y,yc+x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 664 | plot(xc-y,yc+x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 665 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 666 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 667 | #endif |
SomeRandomBloke | 0:b3e0f5bb3b87 | 668 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 669 | // The end! |