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

Dependents:   HT1632MsgScroller SMS_LEDMatrixPrinter

Committer:
SomeRandomBloke
Date:
Thu Nov 08 21:41:33 2012 +0000
Revision:
0:b3e0f5bb3b87
Child:
1:96695118ee13
Initial commit after converting from Arduino to mbed

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