Library for Sure Electronics HT1632 based LED matrix displays. Supports multiple displays connected together.

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Committer:
SomeRandomBloke
Date:
Mon Jul 29 21:15:53 2013 +0000
Revision:
15:9323fab1db01
Parent:
14:b051965066db
minor fix

Who changed what in which revision?

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