1. Trammell Hudson
  2. alphaclock

Commits

Trammell Hudson  committed 834a774

Pack font into 16-bits, some bitmask cleanup

  • Participants
  • Parent commits 9c9af3b
  • Branches default

Comments (0)

Files changed (2)

File alphaclock.c

View file
  • Ignore whitespace
-
 /*
 alphaclock_18.pde 
  
  */
 
 
+#if 0
 #include <EEPROM.h>            // For saving settings
 #include <Wire.h>              // For optional RTC module
 #include <DateTime.h>          // For optional Serial Sync
 #include <DateTimeStrings.h>   // For optional Serial Sync
+#endif
 
+#include <avr/io.h>
+#include <stdint.h>
 
-#define AllLEDsOff();  PORTA |= 127;  
+
+
+static inline void
+AllLEDsOff()
+{
+	PORTA |= 127;
+}
 
 // The buttons are located at PB0, PB1, PB2, PB3
 #define buttonmask 15 
  
  */
 
-byte AlphaArray[] = {
-  0,0,0,    // [space]  ASCII 32
-  0,0,3,    // ! (Yes, lame, but it's the "standard." Better Suggestions welcome.)
-  0,0,40,  // "
-  63,3,255,    //#
-  63,1,42,    //$
-  57,3,106,    //%
-  14,3,98,    //&
-  0,0,64,      // '
-  0,0,192,      // (
-  0,2,16,      // )
-  48,3,240,    // *
-  48,1,32,      //+
-  0,2,0,      //  ,
-  48,0,0,      // -
-  64,0,0,      // . (lower DP)
-  0,2,64,    //  /
-  15,0,15,  // 0
-  0,0,3,     //1
-  63,0,5,    //2
-  47,0,3,    //3
-  48,0,11,    //4
-  63,0,10,    //5
-  63,0,14,    //6
-  3,0,3,      //7
-  63,0,15,    //8
-  63,0,11,    //9
-  0,1,32,      //:
-  0,2,32,      //;
-  0,0,192,      //<
-  60,0,0,      //=
-  0,2,16,      //>
-  34,1,1,      //?
-  47,0,13,      //@
-  51,0,15,      //A
-  47,1,35,      //B
-  15,0,12,      //C
-  15,1,35,     //D
-  31,0,12,      //E
-  19,0,12,    //F
-  47,0,14,      //G
-  48,0,15,      //H
-  15,1,32,        //I
-  12,0,7,        //J
-  16,0,204,      //K
-  12,0,12,       //L
-  0,0,95,        //M
-  0,0,159,      //N
-  15,0,15,      //O
-  51,0,13,      //P
-  15,0,143,    //Q
-  51,0,141,    //R
-  63,0,10,      //S
-  3,1,32,      //T
-  12,0,15,      //U
-  0,2,76,      //V
-  0,2,143,      //W
-  0,2,208,      //X
-  0,1,80,      //Y
-  15,2,64,      //Z
-  10,1,32,      // [
-  0,0,144,      // backslash
-  5,1,32,        // ]
-  0,2,128,      // ^
-  12,0,0,      // _
-  0,0,16        // `
+typedef struct {
+	uint8_t low;
+	uint8_t h1;
+	uint8_t h2;
+} font_t;
+
+static const font_t AlphaArray[] = {
+	[' ' - ' '] = { 0x00, 0x00, 0x00 },
+	['!' - ' '] = { 0x00, 0x00, 0x33 },
+	['"' - ' '] = { 0x00, 0x00, 0x28 },
+	['#' - ' '] = { 0x3F, 0x03, 0xFF },
+	['$' - ' '] = { 0x3F, 0x01, 0x2A },
+	['%' - ' '] = { 0x39, 0x03, 0x6A },
+  { 14,3,98 },    //&
+  { 0,0,64 },      // '
+  { 0,0,192 },      // (
+  { 0,2,16 },      // )
+  { 48,3,240 },    // *
+  { 48,1,32 },      //+
+  { 0,2,0 },      //  ,
+  { 48,0,0 },      // -
+  { 64,0,0 },      // . (lower DP)
+  { 0,2,64 },    //  /
+  { 15,0,15 },  // 0
+  { 0,0,3 },     //1
+  { 63,0,5 },    //2
+  { 47,0,3 },    //3
+  { 48,0,11 },    //4
+  { 63,0,10 },    //5
+  { 63,0,14 },    //6
+  { 3,0,3 },      //7
+  { 63,0,15 },    //8
+  { 63,0,11 },    //9
+  { 0,1,32 },      //:
+  { 0,2,32 },      //;
+  { 0,0,192 },      //<
+  { 60,0,0 },      //=
+  { 0,2,16 },      //>
+  { 34,1,1 },      //?
+  { 47,0,13 },      //@
+  { 51,0,15 },      //A
+  { 47,1,35 },      //B
+  { 15,0,12 },      //C
+  { 15,1,35 },     //D
+  { 31,0,12 },      //E
+  { 19,0,12 },    //F
+  { 47,0,14 },      //G
+  { 48,0,15 },      //H
+  { 15,1,32 },        //I
+  { 12,0,7 },        //J
+  { 16,0,204 },      //K
+  { 12,0,12 },       //L
+  { 0,0,95 },        //M
+  { 0,0,159 },      //N
+  { 15,0,15 },      //O
+  { 51,0,13 },      //P
+  { 15,0,143 },    //Q
+  { 51,0,141 },    //R
+  { 63,0,10 },      //S
+  { 3,1,32 },      //T
+  { 12,0,15 },      //U
+  { 0,2,76 },      //V
+  { 0,2,143 },      //W
+  { 0,2,208 },      //X
+  { 0,1,80 },      //Y
+  { 15,2,64 },      //Z
+  { 10,1,32 },      // [
+  { 0,0,144 },      // backslash
+  { 5,1,32 },        // ]
+  { 0,2,128 },      // ^
+  { 12,0,0 },      // _
+  { 0,0,16 },        // `
 };
 
 // Starting offset of our ASCII array:
-#define asciiOffset 32  
+#define asciiOffset ' '  
 
 // Starting offset for number zero
 #define numberOffset 16
 
 
 //Faster latch macro!
-#define Latch();    PORTC |= 4;PORTC &= 251;    
+static inline void
+Latch(void)
+{
+	sbi(PORTC, 2);
+	cbi(PORTC, 2);
+}
 
 
-void AlphaWrite (byte l1, byte h1, byte h2)
-{  // Transmit data to to LED drivers through SPI
-  PORTA |= 127; // Blank all character "rows" (A0-A5) and LED driver
-  SPDR = l1;
-  while (!(SPSR & _BV(SPIF))) {
-  }  //Wait for transmission complete
-  SPDR = h1;
-  while (!(SPSR & _BV(SPIF))) {
-  }  //Wait for transmission complete
-  SPDR = h2;
-  while (!(SPSR & _BV(SPIF))) {
-  }  //Wait for transmission complete
+/** Blocking send a byte on the SPI port */
+static inline void
+spi_send(
+	uint8_t byte
+)
+{
+	SPDR = byte;
+	while (bit_is_clear(SPSR, SPIF))
+		;
+}
+
+
+/* Transmit a character data to to LED drivers through SPI */
+static void
+AlphaWrite(
+	const font_t * const f
+)
+{
+	PORTA |= 127; // Blank all character "rows" (A0-A5) and LED driver
+	spi_send(f->low);
+	spi_send((f->high >> 8) & 0xFF);
+	spi_send((f->high >> 0) & 0xFF);
 }
 
 

File alphaclock_18_Rev1_0/alphaclock_18_Rev1_0.pde

View file
  • Ignore whitespace
-
 /*
 alphaclock_18.pde 
  
 #include <Wire.h>              // For optional RTC module
 #include <DateTime.h>          // For optional Serial Sync
 #include <DateTimeStrings.h>   // For optional Serial Sync
+#include <avr/pgmspace.h>
 
 
 #define AllLEDsOff();  PORTA |= 127;  
 
 char stringTemp[5];
 
-/* Start at ASCII 32 (decimal), character ' ' (space).
- Byte format: A, B, C, order sent to shift register.
- A: First byte,  "low power"
- B: Second byte, "high power," MSB
- C: Third byte,  "high power," LSB
- 
+/* Start at ASCII 32 (decimal), character ' ' (space) and store the 16 non-dot segments
+ * packed into an unsigned 16-bit word.  This array is stored in flash memory and does
+ * not need to be copied into SRAM, so it is flagged as PROGMEM.
+ *
+ * The bits in the packed form of the digit are:
+ *
+.- -    0 1
+|\|/|  9ABC6
+ - -    4 5
+|/|\|  8FED7
+ - -.   2 3
+ *
  */
+ static const uint16_t AlphaArray[] PROGMEM = {
+	0x0000, // [space]  ASCII 32
+	0x00c0, // ! (Yes, lame, but it's the "standard." Better Suggestions welcome.)
+	0x0a00, // "
+	0xffff, //#
+	0x4abf, //$
+	0xdab9, //%
+	0xd88e, //&
+	0x1000, // '
+	0x3000, // (
+	0x8400, // )
+	0xfc30, // *
+	0x4830, //+
+	0x8000, //  ,
+	0x0030, // -
+	0x0040, // . (lower DP)
+	0x9000, //  /
+	0x03cf, // 0
+	0x00c0, //1
+	0x017f, //2
+	0x00ef, //3
+	0x02f0, //4
+	0x02bf, //5
+	0x03bf, //6
+	0x00c3, //7
+	0x03ff, //8
+	0x02ff, //9
+	0x4800, //:
+	0x8800, //;
+	0x3000, //<
+	0x003c, //=
+	0x8400, //>
+	0x4062, //?
+	0x036f, //@
+	0x03f3, //A
+	0x48ef, //B
+	0x030f, //C
+	0x48cf, //D
+	0x031f, //E
+	0x0313, //F
+	0x03af, //G
+	0x03f0, //H
+	0x480f, //I
+	0x01cc, //J
+	0x3310, //K
+	0x030c, //L
+	0x17c0, //M
+	0x27c0, //N
+	0x03cf, //O
+	0x0373, //P
+	0x23cf, //Q
+	0x2373, //R
+	0x02bf, //S
+	0x4803, //T
+	0x03cc, //U
+	0x9300, //V
+	0xa3c0, //W
+	0xb400, //X
+	0x5400, //Y
+	0x900f, //Z
+	0x480a, // [
+	0x2400, // backslash
+	0x4805, // ]
+	0xa000, // ^
+	0x000c, // _
+	0x0400, // `
 
-byte AlphaArray[] = {
-  0,0,0,    // [space]  ASCII 32
-  0,0,3,    // ! (Yes, lame, but it's the "standard." Better Suggestions welcome.)
-  0,0,40,  // "
-  63,3,255,    //#
-  63,1,42,    //$
-  57,3,106,    //%
-  14,3,98,    //&
-  0,0,64,      // '
-  0,0,192,      // (
-  0,2,16,      // )
-  48,3,240,    // *
-  48,1,32,      //+
-  0,2,0,      //  ,
-  48,0,0,      // -
-  64,0,0,      // . (lower DP)
-  0,2,64,    //  /
-  15,0,15,  // 0
-  0,0,3,     //1
-  63,0,5,    //2
-  47,0,3,    //3
-  48,0,11,    //4
-  63,0,10,    //5
-  63,0,14,    //6
-  3,0,3,      //7
-  63,0,15,    //8
-  63,0,11,    //9
-  0,1,32,      //:
-  0,2,32,      //;
-  0,0,192,      //<
-  60,0,0,      //=
-  0,2,16,      //>
-  34,1,1,      //?
-  47,0,13,      //@
-  51,0,15,      //A
-  47,1,35,      //B
-  15,0,12,      //C
-  15,1,35,     //D
-  31,0,12,      //E
-  19,0,12,    //F
-  47,0,14,      //G
-  48,0,15,      //H
-  15,1,32,        //I
-  12,0,7,        //J
-  16,0,204,      //K
-  12,0,12,       //L
-  0,0,95,        //M
-  0,0,159,      //N
-  15,0,15,      //O
-  51,0,13,      //P
-  15,0,143,    //Q
-  51,0,141,    //R
-  63,0,10,      //S
-  3,1,32,      //T
-  12,0,15,      //U
-  0,2,76,      //V
-  0,2,143,      //W
-  0,2,208,      //X
-  0,1,80,      //Y
-  15,2,64,      //Z
-  10,1,32,      // [
-  0,0,144,      // backslash
-  5,1,32,        // ]
-  0,2,128,      // ^
-  12,0,0,      // _
-  0,0,16        // `
+        // Spinners
+        1 << 0xB,
+        1 << 0xC,
+        1 << 0x5,
+        1 << 0xD,
+        1 << 0xE,
+        1 << 0xF,
+        1 << 0x4,
+        1 << 0xA,
 };
 
 // Starting offset of our ASCII array:
 
   // VARIABLES THAT HAVE EEPROM STORAGE AND DEFAULTS...
   // FadeMode = FadeModeDefault; 
-
   MainBright = MainBrightDefault;
   HourMode24 = HourMode24Default;
   AlarmEnabled = AlarmEnabledDefault;
 
 
 
-
+  // Map the 5-character cotents of the ASCII Character Buffer into the 15 SPI output bytes
+  //  (three bytes per LED character) needed to draw those characters.
+  // The font is packed so that the dots are not included in the bitmap and the
+  // h1 only ever has two bits set, so it is stored at the top of the word.
 void LoadShiftRegBuffers( void)
 {
-  // Map the 5-character cotents of the ASCII Character Buffer into the 15 SPI output bytes
-  //  (three bytes per LED character) needed to draw those characters.
-  byte alphaPosTemp;
-  byte j = 0;
-  while (j < 5)
+  for (byte j = 0 ; j < 5 ; j++)
   {
-    alphaPosTemp = 3 * CharacterBuffer[j];
-    bufl1[j] = AlphaArray[alphaPosTemp++];
-    bufh1[j] = AlphaArray[alphaPosTemp++];
-    bufh2[j] = AlphaArray[alphaPosTemp];     
-    j++;
+    const uint16_t word = pgm_read_word_near(AlphaArray + CharacterBuffer[j]);
+    bufl1[j] = (word >> 0) & 0x3F; // mask out the dots
+    bufh2[j] = word >> 6;
+    bufh1[j] = word >> 14;
   }
 }
 
         AlphaWrite(bufl1[j],bufh1[j],bufh2[j]);      
         Latch();
         PORTA &= ~(1 << j);  // Enable character
+        /*
         //PORTA &= 191;  // Enable LED Driver
 
-        k1 = PORTA & 191;   // Enable LED Driver
-        k2 = PORTA | 64;    // Blank LED driver    //  Was  PORTA |= _BV(6);
-
+        k1 = PORTA & ~0x40;   // Enable LED Driver
+        k2 = PORTA |  0x40;    // Blank LED driver    //  Was  PORTA |= _BV(6);
+*/
 
         // For low brightness levels, we use a very low duty cycle PWM.
         // That's normally fine, but the Arduino interrupts in the background
 
         byte SREGtemp = SREG;
         cli(); // Disable interrupts
-        PORTA = k1;
-
+        PORTA &= ~0x40;
+        
         asm volatile("nop\n\t"
           "nop\n\t"
           "nop\n\t"
           "nop\n\t"
           ::);
 
-        PORTA = k2;
+        PORTA |= 0x40;
         SREG = SREGtemp; // reenable interrupts.
 
         delayMicroseconds(offPeriod);
     }
     else {
       // "Normal" time display: 
-      CharacterBuffer[0] = (AMPM24HdisplayNow - asciiOffset);
+      //CharacterBuffer[0] = (AMPM24HdisplayNow - asciiOffset);
+      CharacterBuffer[0] = (SecNow % 8) + 1 + '`' - asciiOffset; // spinner
       CharacterBuffer[1] = (MinNowOnes + numberOffset);
       CharacterBuffer[2] = (MinNowTens + numberOffset);
       CharacterBuffer[3] = (HrNowOnes + numberOffset);