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

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Committer:
SomeRandomBloke
Date:
Mon Nov 12 23:13:04 2012 +0000
Revision:
5:33b2bfce06b7
Parent:
4:7513dd37efed
Child:
6:80f554fd77a0
Added displayOn/Off functions

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