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

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Committer:
SomeRandomBloke
Date:
Fri Nov 09 22:27:39 2012 +0000
Revision:
4:7513dd37efed
Parent:
3:48f430fe186e
Child:
5:33b2bfce06b7
updates to fix scrolling to just first row of displays when used with multiple rows

Who changed what in which revision?

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