Library for Sure Electronics HT1632 based LED matrix displays. Supports multiple displays connected together.
Dependents: HT1632MsgScroller SMS_LEDMatrixPrinter
HT1632_LedMatrix.cpp@8:61130f9b5b79, 2012-11-24 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Sat Nov 24 21:58:03 2012 +0000
- Revision:
- 8:61130f9b5b79
- Parent:
- 6:80f554fd77a0
- Child:
- 9:8a3c981babd9
Updates for latest displays
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 | 0:b3e0f5bb3b87 | 78 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 79 | // Variables used to keep track of cursor position |
SomeRandomBloke | 0:b3e0f5bb3b87 | 80 | int cursorX = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 81 | int cursorY = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 82 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 83 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 84 | * we keep a copy of the display controller contents so that we can |
SomeRandomBloke | 0:b3e0f5bb3b87 | 85 | * know which bits are on without having to (slowly) read the device. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 86 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 87 | // 4 boards at 32 bytes per board + 1 byte means we don't need to check overwrite in putChar |
SomeRandomBloke | 0:b3e0f5bb3b87 | 88 | uint8_t shadowram[129]; // our copy of the display's RAM |
SomeRandomBloke | 0:b3e0f5bb3b87 | 89 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 90 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 91 | // Custom character buffers - 8 characters available |
SomeRandomBloke | 0:b3e0f5bb3b87 | 92 | // 6 cols * 8 rows - first byte of each char is the number of columns used |
SomeRandomBloke | 0:b3e0f5bb3b87 | 93 | // Bits are aranged in columns with LSB at top |
SomeRandomBloke | 3:48f430fe186e | 94 | uint8_t cgram [8][7] = { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 95 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
SomeRandomBloke | 0:b3e0f5bb3b87 | 96 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, |
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 | 3:48f430fe186e | 102 | { 0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } |
SomeRandomBloke | 3:48f430fe186e | 103 | }; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 104 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 105 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 106 | // Default constructor |
SomeRandomBloke | 3:48f430fe186e | 107 | HT1632_LedMatrix::HT1632_LedMatrix( void ) |
SomeRandomBloke | 3:48f430fe186e | 108 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 109 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 110 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 111 | |
SomeRandomBloke | 3:48f430fe186e | 112 | void HT1632_LedMatrix::init( uint8_t xDevices, uint8_t yDevices ) |
SomeRandomBloke | 3:48f430fe186e | 113 | { |
SomeRandomBloke | 3:48f430fe186e | 114 | // Set up the display size based on number of devices both horizontal and vertical |
SomeRandomBloke | 3:48f430fe186e | 115 | numXDevices = xDevices; |
SomeRandomBloke | 3:48f430fe186e | 116 | xMax = 32 * numXDevices-1; |
SomeRandomBloke | 3:48f430fe186e | 117 | numYDevices = yDevices; |
SomeRandomBloke | 3:48f430fe186e | 118 | yMax = 8 * numYDevices-1; |
SomeRandomBloke | 3:48f430fe186e | 119 | numDevices = numXDevices * numYDevices; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 120 | |
SomeRandomBloke | 3:48f430fe186e | 121 | // Disable all display CS lines by taking high |
SomeRandomBloke | 3:48f430fe186e | 122 | for( uint8_t i = 0; i < 4; i++ ) |
SomeRandomBloke | 3:48f430fe186e | 123 | ht1632_cs[i] = HIGH; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 124 | |
SomeRandomBloke | 4:7513dd37efed | 125 | for (uint8_t chipno=0; chipno<4; chipno++) { |
SomeRandomBloke | 3:48f430fe186e | 126 | chipfree(chipno); // unselect it |
SomeRandomBloke | 8:61130f9b5b79 | 127 | |
SomeRandomBloke | 8:61130f9b5b79 | 128 | sendcmd(chipno, HT1632_CMD_SYSON); // System on |
SomeRandomBloke | 8:61130f9b5b79 | 129 | sendcmd(chipno, HT1632_CMD_LEDON); // LEDs on |
SomeRandomBloke | 8:61130f9b5b79 | 130 | sendcmd(chipno, HT1632_CMD_BLOFF); // Blink Off |
SomeRandomBloke | 8:61130f9b5b79 | 131 | sendcmd(chipno, HT1632_CMD_MSTMD); // Master Mode |
SomeRandomBloke | 8:61130f9b5b79 | 132 | sendcmd(chipno, HT1632_CMD_RCCLK); // Internal Oscillator |
SomeRandomBloke | 8:61130f9b5b79 | 133 | sendcmd(chipno, HT1632_CMD_COMS00); // 08*32, NMOS drivers |
SomeRandomBloke | 3:48f430fe186e | 134 | sendcmd(chipno, HT1632_CMD_PWM | 0x0c); // PWM Duty |
SomeRandomBloke | 8:61130f9b5b79 | 135 | |
SomeRandomBloke | 3:48f430fe186e | 136 | for (uint8_t i=0; i<96; i++) |
SomeRandomBloke | 3:48f430fe186e | 137 | senddata(chipno, i, 0); // clear the display |
SomeRandomBloke | 3:48f430fe186e | 138 | wait(0.1); |
SomeRandomBloke | 3:48f430fe186e | 139 | } |
SomeRandomBloke | 3:48f430fe186e | 140 | cursorX = 0; |
SomeRandomBloke | 3:48f430fe186e | 141 | cursorY = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 142 | } |
SomeRandomBloke | 3:48f430fe186e | 143 | |
SomeRandomBloke | 6:80f554fd77a0 | 144 | void HT1632_LedMatrix::displayOff( void ) |
SomeRandomBloke | 6:80f554fd77a0 | 145 | { |
SomeRandomBloke | 5:33b2bfce06b7 | 146 | for (uint8_t chipno=0; chipno<4; chipno++) { |
SomeRandomBloke | 5:33b2bfce06b7 | 147 | chipfree(chipno); // unselect it |
SomeRandomBloke | 5:33b2bfce06b7 | 148 | sendcmd(chipno, HT1632_CMD_LEDOFF); // LEDs on |
SomeRandomBloke | 5:33b2bfce06b7 | 149 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 150 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 151 | |
SomeRandomBloke | 6:80f554fd77a0 | 152 | void HT1632_LedMatrix::displayOn( void ) |
SomeRandomBloke | 6:80f554fd77a0 | 153 | { |
SomeRandomBloke | 5:33b2bfce06b7 | 154 | for (uint8_t chipno=0; chipno<4; chipno++) { |
SomeRandomBloke | 5:33b2bfce06b7 | 155 | chipfree(chipno); // unselect it |
SomeRandomBloke | 5:33b2bfce06b7 | 156 | sendcmd(chipno, HT1632_CMD_LEDON); // LEDs on |
SomeRandomBloke | 5:33b2bfce06b7 | 157 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 158 | } |
SomeRandomBloke | 5:33b2bfce06b7 | 159 | |
SomeRandomBloke | 5:33b2bfce06b7 | 160 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 161 | /*********************************************************************** |
SomeRandomBloke | 0:b3e0f5bb3b87 | 162 | * chipselect / chipfree |
SomeRandomBloke | 0:b3e0f5bb3b87 | 163 | * Select or de-select a particular ht1632 chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 164 | * De-selecting a chip ends the commands being sent to a chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 165 | * CD pins are active-low; writing 0 to the pin selects the chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 166 | ***********************************************************************/ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 167 | void HT1632_LedMatrix::chipselect(uint8_t chipno) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 168 | { |
SomeRandomBloke | 8:61130f9b5b79 | 169 | // for(int i=0; i<4; i++) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 170 | ht1632_cs[chipno] = LOW; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 171 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 172 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 173 | void HT1632_LedMatrix::chipfree(uint8_t chipno) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 174 | { |
SomeRandomBloke | 8:61130f9b5b79 | 175 | // for(int i=0; i<4; i++) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 176 | ht1632_cs[chipno] = HIGH; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 177 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 178 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 179 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 180 | * writebits |
SomeRandomBloke | 0:b3e0f5bb3b87 | 181 | * Write bits to h1632 on pins HT1632_DATA, HT1632_WRCLK |
SomeRandomBloke | 0:b3e0f5bb3b87 | 182 | * Chip is assumed to already be chip-selected |
SomeRandomBloke | 0:b3e0f5bb3b87 | 183 | * Bits are shifted out from MSB to LSB, with the first bit sent |
SomeRandomBloke | 0:b3e0f5bb3b87 | 184 | * being (bits & firstbit), shifted till firsbit is zero. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 185 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 186 | void HT1632_LedMatrix::writebits (uint8_t bits, uint8_t firstbit) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 187 | { |
SomeRandomBloke | 3:48f430fe186e | 188 | while (firstbit) { |
SomeRandomBloke | 3:48f430fe186e | 189 | ht1632_wrclk = LOW; |
SomeRandomBloke | 3:48f430fe186e | 190 | if (bits & firstbit) { |
SomeRandomBloke | 3:48f430fe186e | 191 | ht1632_data = HIGH; |
SomeRandomBloke | 3:48f430fe186e | 192 | } else { |
SomeRandomBloke | 3:48f430fe186e | 193 | ht1632_data = LOW; |
SomeRandomBloke | 3:48f430fe186e | 194 | } |
SomeRandomBloke | 3:48f430fe186e | 195 | ht1632_wrclk = HIGH; |
SomeRandomBloke | 3:48f430fe186e | 196 | firstbit >>= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 197 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 198 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 199 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 200 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 201 | * writedatabits |
SomeRandomBloke | 0:b3e0f5bb3b87 | 202 | * Write databits to h1632 on pins HT1632_DATA, HT1632_WRCLK |
SomeRandomBloke | 0:b3e0f5bb3b87 | 203 | * Chip is assumed to already be chip-selected |
SomeRandomBloke | 0:b3e0f5bb3b87 | 204 | * Bits are shifted out from LSB to MSB |
SomeRandomBloke | 0:b3e0f5bb3b87 | 205 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 206 | void HT1632_LedMatrix::writedatabits (uint8_t bits, uint8_t count) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 207 | { |
SomeRandomBloke | 3:48f430fe186e | 208 | while (count) { |
SomeRandomBloke | 3:48f430fe186e | 209 | ht1632_wrclk = LOW; |
SomeRandomBloke | 3:48f430fe186e | 210 | ht1632_data = bits & 1; |
SomeRandomBloke | 3:48f430fe186e | 211 | ht1632_wrclk = HIGH; |
SomeRandomBloke | 3:48f430fe186e | 212 | count--; |
SomeRandomBloke | 3:48f430fe186e | 213 | bits >>= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 214 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 215 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 216 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 217 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 218 | * sendcmd |
SomeRandomBloke | 0:b3e0f5bb3b87 | 219 | * Send a command to the ht1632 chip. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 220 | * A command consists of a 3-bit "CMD" ID, an 8bit command, and |
SomeRandomBloke | 0:b3e0f5bb3b87 | 221 | * one "don't care bit". |
SomeRandomBloke | 0:b3e0f5bb3b87 | 222 | * Select 1 0 0 c7 c6 c5 c4 c3 c2 c1 c0 xx Free |
SomeRandomBloke | 0:b3e0f5bb3b87 | 223 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 224 | void HT1632_LedMatrix::sendcmd (uint8_t chipno, uint8_t command) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 225 | { |
SomeRandomBloke | 3:48f430fe186e | 226 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 227 | writebits(HT1632_ID_CMD, 0x04); // send 3 bits of id: COMMMAND |
SomeRandomBloke | 3:48f430fe186e | 228 | writebits(command, 0x80); // send the actual command |
SomeRandomBloke | 3:48f430fe186e | 229 | writebits(0, 1); // one extra dont-care bit in commands. |
SomeRandomBloke | 3:48f430fe186e | 230 | chipfree(chipno); //done |
SomeRandomBloke | 0:b3e0f5bb3b87 | 231 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 232 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 233 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 234 | * clear |
SomeRandomBloke | 0:b3e0f5bb3b87 | 235 | * clear the display, and the shadow memory, and the snapshot |
SomeRandomBloke | 0:b3e0f5bb3b87 | 236 | * memory. This uses the "write multiple words" capability of |
SomeRandomBloke | 0:b3e0f5bb3b87 | 237 | * the chipset by writing all 96 words of memory without raising |
SomeRandomBloke | 0:b3e0f5bb3b87 | 238 | * the chipselect signal. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 239 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 240 | void HT1632_LedMatrix::clear() |
SomeRandomBloke | 0:b3e0f5bb3b87 | 241 | { |
SomeRandomBloke | 3:48f430fe186e | 242 | char i; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 243 | |
SomeRandomBloke | 3:48f430fe186e | 244 | for (uint8_t chipno=0; chipno<numDevices; chipno++) { |
SomeRandomBloke | 3:48f430fe186e | 245 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 246 | writebits(HT1632_ID_WR, 0x04); // send ID: WRITE to RAM |
SomeRandomBloke | 3:48f430fe186e | 247 | writebits(0, 0x40); // Send address |
SomeRandomBloke | 3:48f430fe186e | 248 | for (i = 0; i < 32; i++) // Clear entire display |
SomeRandomBloke | 3:48f430fe186e | 249 | writedatabits(0, 8); // send 8 bits of data |
SomeRandomBloke | 3:48f430fe186e | 250 | chipfree(chipno); // done |
SomeRandomBloke | 3:48f430fe186e | 251 | for (i=0; i < 64; i++) |
SomeRandomBloke | 3:48f430fe186e | 252 | shadowram[i+64*chipno] = 0; |
SomeRandomBloke | 3:48f430fe186e | 253 | } |
SomeRandomBloke | 3:48f430fe186e | 254 | cursorX = 0; |
SomeRandomBloke | 3:48f430fe186e | 255 | cursorY = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 256 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 257 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 258 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 259 | // Brighness is from 0 to 15 |
SomeRandomBloke | 3:48f430fe186e | 260 | void HT1632_LedMatrix::setBrightness( unsigned char brightness ) |
SomeRandomBloke | 3:48f430fe186e | 261 | { |
SomeRandomBloke | 3:48f430fe186e | 262 | for (uint8_t chipno=0; chipno<numDevices; chipno++) { |
SomeRandomBloke | 3:48f430fe186e | 263 | sendcmd(chipno, HT1632_CMD_PWM | (brightness & 0x0F )); |
SomeRandomBloke | 3:48f430fe186e | 264 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 265 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 266 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 267 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 268 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 269 | * senddata |
SomeRandomBloke | 0:b3e0f5bb3b87 | 270 | * send a nibble (4 bits) of data to a particular memory location of the |
SomeRandomBloke | 0:b3e0f5bb3b87 | 271 | * ht1632. The command has 3 bit ID, 7 bits of address, and 4 bits of data. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 272 | * Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 Free |
SomeRandomBloke | 0:b3e0f5bb3b87 | 273 | * Note that the address is sent MSB first, while the data is sent LSB first! |
SomeRandomBloke | 0:b3e0f5bb3b87 | 274 | * This means that somewhere a bit reversal will have to be done to get |
SomeRandomBloke | 0:b3e0f5bb3b87 | 275 | * zero-based addressing of words and dots within words. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 276 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 277 | void HT1632_LedMatrix::senddata (uint8_t chipno, uint8_t address, uint8_t data) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 278 | { |
SomeRandomBloke | 3:48f430fe186e | 279 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 280 | writebits(HT1632_ID_WR, 0x04); // send ID: WRITE to RAM |
SomeRandomBloke | 3:48f430fe186e | 281 | writebits(address, 0x40); // Send address |
SomeRandomBloke | 3:48f430fe186e | 282 | writedatabits(data, 4); // send 4 bits of data |
SomeRandomBloke | 3:48f430fe186e | 283 | chipfree(chipno); // done |
SomeRandomBloke | 0:b3e0f5bb3b87 | 284 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 285 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 286 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 287 | * sendcol |
SomeRandomBloke | 0:b3e0f5bb3b87 | 288 | * send a byte of data to a particular memory location of the |
SomeRandomBloke | 0:b3e0f5bb3b87 | 289 | * ht1632. The command has 3 bit ID, 7 bits of address, and 8 bits of data. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 290 | * Select 1 0 1 A6 A5 A4 A3 A2 A1 A0 D0 D1 D2 D3 D4 D5 D6 D7 D8 Free |
SomeRandomBloke | 0:b3e0f5bb3b87 | 291 | * Note that the address is sent MSB first, while the data is sent LSB first! |
SomeRandomBloke | 0:b3e0f5bb3b87 | 292 | * This means that somewhere a bit reversal will have to be done to get |
SomeRandomBloke | 0:b3e0f5bb3b87 | 293 | * zero-based addressing of words and dots within words. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 294 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 295 | void HT1632_LedMatrix::sendcol (uint8_t chipno, uint8_t address, uint8_t data) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 296 | { |
SomeRandomBloke | 3:48f430fe186e | 297 | chipselect(chipno); // Select chip |
SomeRandomBloke | 3:48f430fe186e | 298 | writebits(HT1632_ID_WR, 0x04); // send ID: WRITE to RAM |
SomeRandomBloke | 3:48f430fe186e | 299 | writebits(address, 0x40); // Send address |
SomeRandomBloke | 3:48f430fe186e | 300 | writedatabits(data, 8); // send 8 bits of data |
SomeRandomBloke | 3:48f430fe186e | 301 | chipfree(chipno); // done |
SomeRandomBloke | 0:b3e0f5bb3b87 | 302 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 303 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 304 | // Write a string at the position specified |
SomeRandomBloke | 4:7513dd37efed | 305 | // x and y start from 0 and count number of pixels, 2nd row on a 2 row display is y=8 |
SomeRandomBloke | 3:48f430fe186e | 306 | void HT1632_LedMatrix::putString(int x, int y, char *str) |
SomeRandomBloke | 3:48f430fe186e | 307 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 308 | cursorX = x; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 309 | cursorY = y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 310 | while( *str ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 311 | putChar( cursorX, y, *str++ ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 312 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 313 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 314 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 315 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 316 | * Copy a character glyph from the smallFont data structure to |
SomeRandomBloke | 0:b3e0f5bb3b87 | 317 | * display memory, with its upper left at the given coordinate |
SomeRandomBloke | 0:b3e0f5bb3b87 | 318 | * This is unoptimized and simply uses plot() to draw each dot. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 319 | */ |
SomeRandomBloke | 3:48f430fe186e | 320 | void HT1632_LedMatrix::write( uint8_t c) |
SomeRandomBloke | 3:48f430fe186e | 321 | { |
SomeRandomBloke | 3:48f430fe186e | 322 | putChar( cursorX, cursorY, (char)c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 323 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 324 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 325 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 326 | * Copy a character glyph from the myfont data structure to |
SomeRandomBloke | 0:b3e0f5bb3b87 | 327 | * display memory, with its upper left at the given coordinate |
SomeRandomBloke | 0:b3e0f5bb3b87 | 328 | * This is unoptimized and simply uses plot() to draw each dot. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 329 | * returns number of columns that didn't fit |
SomeRandomBloke | 0:b3e0f5bb3b87 | 330 | */ |
SomeRandomBloke | 3:48f430fe186e | 331 | uint8_t HT1632_LedMatrix::putChar(int x, int y, char c) |
SomeRandomBloke | 3:48f430fe186e | 332 | { |
SomeRandomBloke | 3:48f430fe186e | 333 | // fonts defined for ascii 32 and beyond (index 0 in font array is ascii 32); |
SomeRandomBloke | 3:48f430fe186e | 334 | // CGRAM characters are in range 0 to 15 with 8-15 being repeat of 0-7 |
SomeRandomBloke | 3:48f430fe186e | 335 | // note we force y to be modulo 8 - we do not support writing character to partial y values. |
SomeRandomBloke | 3:48f430fe186e | 336 | |
SomeRandomBloke | 3:48f430fe186e | 337 | uint8_t charIndex; |
SomeRandomBloke | 3:48f430fe186e | 338 | uint8_t colData; |
SomeRandomBloke | 3:48f430fe186e | 339 | uint8_t numCols; |
SomeRandomBloke | 3:48f430fe186e | 340 | uint8_t chipno; |
SomeRandomBloke | 3:48f430fe186e | 341 | uint8_t addr; |
SomeRandomBloke | 3:48f430fe186e | 342 | uint8_t colsLeft = 0; // cols that didn't fit |
SomeRandomBloke | 3:48f430fe186e | 343 | |
SomeRandomBloke | 3:48f430fe186e | 344 | if( c > 15 ) { |
SomeRandomBloke | 3:48f430fe186e | 345 | // Regular characters |
SomeRandomBloke | 3:48f430fe186e | 346 | // replace undisplayable characters with blank; |
SomeRandomBloke | 3:48f430fe186e | 347 | if (c < 32 || c > 126) { |
SomeRandomBloke | 3:48f430fe186e | 348 | charIndex = 0; |
SomeRandomBloke | 3:48f430fe186e | 349 | } else { |
SomeRandomBloke | 3:48f430fe186e | 350 | charIndex = c - 32; |
SomeRandomBloke | 3:48f430fe186e | 351 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 352 | |
SomeRandomBloke | 3:48f430fe186e | 353 | // move character definition, pixel by pixel, onto the display; |
SomeRandomBloke | 3:48f430fe186e | 354 | // fonts are defined as one byte per col; |
SomeRandomBloke | 3:48f430fe186e | 355 | numCols=smallFont[charIndex][6]; // get the number of columns this character uses |
SomeRandomBloke | 3:48f430fe186e | 356 | for (uint8_t col=0; col<numCols; col++) { |
SomeRandomBloke | 3:48f430fe186e | 357 | colData = smallFont[charIndex][col]; |
SomeRandomBloke | 3:48f430fe186e | 358 | chipno = chip_number(x,y); |
SomeRandomBloke | 3:48f430fe186e | 359 | addr = chip_byte_address(x,y); // compute which memory byte this is in |
SomeRandomBloke | 3:48f430fe186e | 360 | if (x <= xMax && y <= yMax) { |
SomeRandomBloke | 3:48f430fe186e | 361 | shadowram[(addr>>1)+32*chipno] = colData; |
SomeRandomBloke | 3:48f430fe186e | 362 | sendcol(chipno,addr,colData); |
SomeRandomBloke | 3:48f430fe186e | 363 | x++; |
SomeRandomBloke | 3:48f430fe186e | 364 | } else { |
SomeRandomBloke | 3:48f430fe186e | 365 | colsLeft++; |
SomeRandomBloke | 3:48f430fe186e | 366 | } |
SomeRandomBloke | 3:48f430fe186e | 367 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 368 | } else { |
SomeRandomBloke | 3:48f430fe186e | 369 | // CGRAM Characters |
SomeRandomBloke | 3:48f430fe186e | 370 | charIndex = c & 0x07; // Only low 3 bits count |
SomeRandomBloke | 3:48f430fe186e | 371 | numCols=cgram[charIndex][0]; // get the number of columns this character uses |
SomeRandomBloke | 3:48f430fe186e | 372 | // fonts are defined as one byte per col; |
SomeRandomBloke | 3:48f430fe186e | 373 | for (uint8_t col=1; col<=numCols; col++) { |
SomeRandomBloke | 3:48f430fe186e | 374 | colData = cgram[charIndex][col]; |
SomeRandomBloke | 3:48f430fe186e | 375 | chipno = chip_number(x,y); |
SomeRandomBloke | 3:48f430fe186e | 376 | addr = chip_byte_address(x,y); // compute which memory byte this is in |
SomeRandomBloke | 3:48f430fe186e | 377 | if (x <= xMax && y <= yMax) { |
SomeRandomBloke | 3:48f430fe186e | 378 | shadowram[(addr>>1)+32*chipno] = colData; |
SomeRandomBloke | 3:48f430fe186e | 379 | sendcol(chipno,addr,colData); |
SomeRandomBloke | 3:48f430fe186e | 380 | x++; |
SomeRandomBloke | 3:48f430fe186e | 381 | } else { |
SomeRandomBloke | 3:48f430fe186e | 382 | colsLeft++; |
SomeRandomBloke | 3:48f430fe186e | 383 | } |
SomeRandomBloke | 3:48f430fe186e | 384 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 385 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 386 | |
SomeRandomBloke | 3:48f430fe186e | 387 | cursorX = x; |
SomeRandomBloke | 3:48f430fe186e | 388 | cursorY = y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 389 | |
SomeRandomBloke | 3:48f430fe186e | 390 | return colsLeft; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 391 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 392 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 393 | // Set position of cursor for writing |
SomeRandomBloke | 3:48f430fe186e | 394 | void HT1632_LedMatrix::gotoXY(int x, int y) |
SomeRandomBloke | 3:48f430fe186e | 395 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 396 | cursorX = x; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 397 | cursorY = y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 398 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 399 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 400 | void HT1632_LedMatrix::getXY(int* x, int* y) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 401 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 402 | *x = cursorX; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 403 | *y = cursorY; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 404 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 405 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 406 | void HT1632_LedMatrix::getXYMax(int* x, int* y) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 407 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 408 | *x = xMax; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 409 | *y = yMax; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 410 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 411 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 412 | // Shift cursor X position a number of positions either left or right. |
SomeRandomBloke | 3:48f430fe186e | 413 | void HT1632_LedMatrix::shiftCursorX(int xinc) |
SomeRandomBloke | 3:48f430fe186e | 414 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 415 | cursorX += xinc; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 416 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 417 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 418 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 419 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 420 | * plot a point on the display, with the upper left hand corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 421 | * being (0,0), and the lower right hand corner being (xMax-1, yMax-1). |
SomeRandomBloke | 0:b3e0f5bb3b87 | 422 | * Note that Y increases going "downward" in contrast with most |
SomeRandomBloke | 0:b3e0f5bb3b87 | 423 | * mathematical coordiate systems, but in common with many displays |
SomeRandomBloke | 0:b3e0f5bb3b87 | 424 | * basic bounds checking used. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 425 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 426 | void HT1632_LedMatrix::plot (int x, int y, char val) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 427 | { |
SomeRandomBloke | 3:48f430fe186e | 428 | if (x<0 || x>xMax || y<0 || y>yMax) |
SomeRandomBloke | 3:48f430fe186e | 429 | return; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 430 | |
SomeRandomBloke | 3:48f430fe186e | 431 | uint8_t chipno = chip_number(x,y); |
SomeRandomBloke | 3:48f430fe186e | 432 | char addr = chip_byte_address(x,y); // compute which memory word this is in |
SomeRandomBloke | 3:48f430fe186e | 433 | char shadowAddress = addr >>1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 434 | |
SomeRandomBloke | 3:48f430fe186e | 435 | char bitval = 1<<(y&7); // compute which bit will need set |
SomeRandomBloke | 0:b3e0f5bb3b87 | 436 | |
SomeRandomBloke | 3:48f430fe186e | 437 | if (val) { // Modify the shadow memory |
SomeRandomBloke | 3:48f430fe186e | 438 | shadowram[shadowAddress +32*chipno] |= bitval; |
SomeRandomBloke | 3:48f430fe186e | 439 | } else { |
SomeRandomBloke | 3:48f430fe186e | 440 | shadowram[shadowAddress +32*chipno] &= ~bitval; |
SomeRandomBloke | 3:48f430fe186e | 441 | } |
SomeRandomBloke | 3:48f430fe186e | 442 | // Now copy the new memory value to the display |
SomeRandomBloke | 3:48f430fe186e | 443 | sendcol(chipno, addr, shadowram[shadowAddress +32*chipno]); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 444 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 445 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 446 | |
SomeRandomBloke | 3:48f430fe186e | 447 | void HT1632_LedMatrix::setCustomChar( int charNum, unsigned char cgchar[] ) |
SomeRandomBloke | 3:48f430fe186e | 448 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 449 | for(int i=1; i<7; i++ ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 450 | cgram[charNum][i] = (uint8_t)cgchar[i]; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 451 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 452 | cgram[charNum][6] = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 453 | cgram[charNum][0] = 6; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 454 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 455 | |
SomeRandomBloke | 3:48f430fe186e | 456 | void HT1632_LedMatrix::setCustomChar( int charNum, unsigned char cgchar[], uint8_t numCols ) |
SomeRandomBloke | 3:48f430fe186e | 457 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 458 | numCols = max(numCols, 6 ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 459 | for(int i=1; i<=numCols; i++ ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 460 | cgram[charNum][i] = (uint8_t)cgchar[i]; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 461 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 462 | cgram[charNum][0] = numCols; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 463 | cgram[charNum][numCols] = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 464 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 465 | |
SomeRandomBloke | 4:7513dd37efed | 466 | void HT1632_LedMatrix::scrollLeft(uint8_t numberCols, uint8_t lineNum ) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 467 | { |
SomeRandomBloke | 4:7513dd37efed | 468 | |
SomeRandomBloke | 4:7513dd37efed | 469 | for (int i=0; i<xMax-numberCols; i++) { |
SomeRandomBloke | 4:7513dd37efed | 470 | shadowram[i]=shadowram[i+numberCols]; |
SomeRandomBloke | 4:7513dd37efed | 471 | } |
SomeRandomBloke | 4:7513dd37efed | 472 | for (int i=xMax-numberCols; i<xMax; i++) { |
SomeRandomBloke | 4:7513dd37efed | 473 | shadowram[i]=0; |
SomeRandomBloke | 4:7513dd37efed | 474 | } |
SomeRandomBloke | 6:80f554fd77a0 | 475 | /* |
SomeRandomBloke | 6:80f554fd77a0 | 476 | for (int i=0; i<128-numberCols-1; i++) { |
SomeRandomBloke | 6:80f554fd77a0 | 477 | shadowram[i]=shadowram[i+numberCols]; |
SomeRandomBloke | 6:80f554fd77a0 | 478 | } |
SomeRandomBloke | 6:80f554fd77a0 | 479 | for (int i=128-numberCols; i<128; i++) { |
SomeRandomBloke | 6:80f554fd77a0 | 480 | shadowram[i]=0; |
SomeRandomBloke | 6:80f554fd77a0 | 481 | } |
SomeRandomBloke | 6:80f554fd77a0 | 482 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 483 | cursorX -= numberCols; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 484 | if (cursorX < 0 ) cursorX = 0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 485 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 486 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 487 | void HT1632_LedMatrix::putShadowRam() |
SomeRandomBloke | 0:b3e0f5bb3b87 | 488 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 489 | for (int chipno=0; chipno<numDevices; chipno++) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 490 | putShadowRam(chipno); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 491 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 492 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 493 | void HT1632_LedMatrix::putShadowRam(uint8_t chipno) |
SomeRandomBloke | 0:b3e0f5bb3b87 | 494 | { |
SomeRandomBloke | 3:48f430fe186e | 495 | for (int i=0; i<64; i+=2) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 496 | sendcol(chipno,i,shadowram[(i>>1)+32*chipno]); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 497 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 498 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 499 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 500 | #ifdef USE_GRAPHIC |
SomeRandomBloke | 0:b3e0f5bb3b87 | 501 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 502 | * Name : drawLine |
SomeRandomBloke | 0:b3e0f5bb3b87 | 503 | * Description : Draws a line between two points on the display. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 504 | * Argument(s) : x1, y1 - Absolute pixel coordinates for line origin. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 505 | * x2, y2 - Absolute pixel coordinates for line end. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 506 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 0:b3e0f5bb3b87 | 507 | * Return value : none |
SomeRandomBloke | 0:b3e0f5bb3b87 | 508 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 509 | void HT1632_LedMatrix::drawLine(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 3:48f430fe186e | 510 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 511 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 512 | int dx, dy, stepx, stepy, fraction; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 513 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 514 | /* Calculate differential form */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 515 | /* dy y2 - y1 */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 516 | /* -- = ------- */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 517 | /* dx x2 - x1 */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 518 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 519 | /* Take differences */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 520 | dy = y2 - y1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 521 | dx = x2 - x1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 522 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 523 | /* dy is negative */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 524 | if ( dy < 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 525 | dy = -dy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 526 | stepy = -1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 527 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 528 | stepy = 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 529 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 530 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 531 | /* dx is negative */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 532 | if ( dx < 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 533 | dx = -dx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 534 | stepx = -1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 535 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 536 | stepx = 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 537 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 538 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 539 | dx <<= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 540 | dy <<= 1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 541 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 542 | /* Draw initial position */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 543 | plot( x1, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 544 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 545 | /* Draw next positions until end */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 546 | if ( dx > dy ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 547 | /* Take fraction */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 548 | fraction = dy - ( dx >> 1); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 549 | while ( x1 != x2 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 550 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 551 | y1 += stepy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 552 | fraction -= dx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 553 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 554 | x1 += stepx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 555 | fraction += dy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 556 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 557 | /* Draw calculated point */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 558 | plot( x1, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 559 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 560 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 561 | /* Take fraction */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 562 | fraction = dx - ( dy >> 1); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 563 | while ( y1 != y2 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 564 | if ( fraction >= 0 ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 565 | x1 += stepx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 566 | fraction -= dy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 567 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 568 | y1 += stepy; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 569 | fraction += dx; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 570 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 571 | /* Draw calculated point */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 572 | plot( x1, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 573 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 574 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 575 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 576 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 577 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 578 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 579 | * Name : drawRectangle |
SomeRandomBloke | 0:b3e0f5bb3b87 | 580 | * Description : Draw a rectangle given to top left and bottom right points |
SomeRandomBloke | 0:b3e0f5bb3b87 | 581 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 582 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 3:48f430fe186e | 583 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 0:b3e0f5bb3b87 | 584 | * Return value : none |
SomeRandomBloke | 0:b3e0f5bb3b87 | 585 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 586 | void HT1632_LedMatrix::drawRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 3:48f430fe186e | 587 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 588 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 589 | drawLine( x1, y1, x2, y1, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 590 | drawLine( x1, y1, x1, y2, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 591 | drawLine( x1, y2, x2, y2, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 592 | drawLine( x2, y1, x2, y2, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 593 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 594 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 595 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 596 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 597 | * Name : drawFilledRectangle |
SomeRandomBloke | 0:b3e0f5bb3b87 | 598 | * Description : Draw a filled rectangle given to top left and bottom right points |
SomeRandomBloke | 0:b3e0f5bb3b87 | 599 | * just simply draws horizontal lines where the rectangle would be |
SomeRandomBloke | 0:b3e0f5bb3b87 | 600 | * Argument(s) : x1, y1 - Absolute pixel coordinates for top left corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 601 | * x2, y2 - Absolute pixel coordinates for bottom right corner |
SomeRandomBloke | 0:b3e0f5bb3b87 | 602 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 0:b3e0f5bb3b87 | 603 | * Return value : none |
SomeRandomBloke | 0:b3e0f5bb3b87 | 604 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 605 | void HT1632_LedMatrix::drawFilledRectangle(unsigned char x1, unsigned char y1, |
SomeRandomBloke | 3:48f430fe186e | 606 | unsigned char x2, unsigned char y2, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 607 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 608 | for(int i=y1; i <= y2; i++ ) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 609 | drawLine( x1, i, x2, i, c ); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 610 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 611 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 612 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 613 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 614 | /* |
SomeRandomBloke | 0:b3e0f5bb3b87 | 615 | * Name : drawCircle |
SomeRandomBloke | 3:48f430fe186e | 616 | * Description : Draw a circle using Bresenham's algorithm. |
SomeRandomBloke | 0:b3e0f5bb3b87 | 617 | * Some small circles will look like squares!! |
SomeRandomBloke | 0:b3e0f5bb3b87 | 618 | * Argument(s) : xc, yc - Centre of circle |
SomeRandomBloke | 0:b3e0f5bb3b87 | 619 | * r - Radius |
SomeRandomBloke | 0:b3e0f5bb3b87 | 620 | * c - either PIXEL_ON, PIXEL_OFF |
SomeRandomBloke | 3:48f430fe186e | 621 | * Return value : None |
SomeRandomBloke | 0:b3e0f5bb3b87 | 622 | */ |
SomeRandomBloke | 0:b3e0f5bb3b87 | 623 | void HT1632_LedMatrix::drawCircle(unsigned char xc, unsigned char yc, |
SomeRandomBloke | 3:48f430fe186e | 624 | unsigned char r, unsigned char c) |
SomeRandomBloke | 3:48f430fe186e | 625 | { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 626 | int x=0; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 627 | int y=r; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 628 | int p=3-(2*r); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 629 | |
SomeRandomBloke | 3:48f430fe186e | 630 | plot( xc+x,yc-y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 631 | |
SomeRandomBloke | 3:48f430fe186e | 632 | for(x=0; x<=y; x++) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 633 | if (p<0) { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 634 | y=y; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 635 | p=(p+(4*x)+6); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 636 | } else { |
SomeRandomBloke | 0:b3e0f5bb3b87 | 637 | y=y-1; |
SomeRandomBloke | 0:b3e0f5bb3b87 | 638 | p=p+((4*(x-y)+10)); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 639 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 640 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 641 | plot(xc+x,yc-y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 642 | plot(xc-x,yc-y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 643 | plot(xc+x,yc+y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 644 | plot(xc-x,yc+y, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 645 | plot(xc+y,yc-x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 646 | plot(xc-y,yc-x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 647 | plot(xc+y,yc+x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 648 | plot(xc-y,yc+x, c); |
SomeRandomBloke | 0:b3e0f5bb3b87 | 649 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 650 | } |
SomeRandomBloke | 0:b3e0f5bb3b87 | 651 | #endif |
SomeRandomBloke | 0:b3e0f5bb3b87 | 652 | |
SomeRandomBloke | 0:b3e0f5bb3b87 | 653 | // The end! |