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

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Committer:
SomeRandomBloke
Date:
Fri Nov 09 08:48:49 2012 +0000
Revision:
1:96695118ee13
Parent:
0:b3e0f5bb3b87
Child:
2:3e477c909f7a
Updates to set unused CS lines to high

Who changed what in which revision?

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