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