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

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Committer:
SomeRandomBloke
Date:
Mon Apr 08 20:54:20 2013 +0000
Revision:
13:9a869360d0ae
Parent:
12:e621d8c89cc3
Child:
14:b051965066db
updates

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