Commits

Trammell Hudson  committed bac9f49

Whitespace formatting

  • Participants
  • Parent commits 2384d1e

Comments (0)

Files changed (1)

File alphaclock_18_Rev1_0/alphaclock_18_Rev1_0.pde

 #define bit_is_set(PORT, BIT) ((PORT) & (1 << (BIT)))
 
 
+/** Deslect all LED drivers to disable all digits */
 static inline void
 led_all_off(void)
 {
-  PORTA |= 0x7F;
+	PORTA |= 0x7F;
 }
 
+
+/** Select a single LED digit driver */
 static inline void
 led_digit_select(
-  const byte j
+	const byte j
 )
 {
-  cbi(PORTA, j);
+	cbi(PORTA, j);
 }
 
+
+/** Turn off the currently selected LED digit driver */
 static inline void
 led_digit_off(void)
 {
-  sbi(PORTA, 6);
+	sbi(PORTA, 6);
 }
 
+
+/** Turn on the currently selected LED digit driver */
 static inline void
 led_digit_on(void)
 {
-  cbi(PORTA, 6);
+	cbi(PORTA, 6);
 }
 
 
 
 char stringTemp[5];
 
-/* 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 font for the LED digit display.
+ * Start at ASCII 32 (decimal), character ' ' (space) and stores 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.  When reading it, the
+ * pgm_read_word_near() function must be used.
  *
  * The bits in the packed form of the digit are:
  *
-.- -    0 1
-|\|/|  9ABC6
- - -    4 5
-|/|\|  8FED7
- - -.   2 3
+ * .- -    0 1
+ * |\|/|  9ABC6
+ *  - -    4 5
+ * |/|\|  8FED7
+ *  - -.   2 3
  *
  */
- static const uint16_t AlphaArray[] PROGMEM = {
+ static const uint16_t
+AlphaArray[] PROGMEM = {
 	0x0000, // [space]  ASCII 32
 	0x00c0, // ! (Yes, lame, but it's the "standard." Better Suggestions welcome.)
 	0x0a00, // "
         1 << 9,
 };
 
+
 // Starting offset of our ASCII array:
-#define asciiOffset 32  
+#define asciiOffset ' '
 
 // Starting offset for number zero
-#define numberOffset 16
+#define numberOffset ('0' - asciiOffset)
 
 
-//Faster latch macro!
+/**
+ * Faster latch macro!
+ * \todo What does this do?
+ */
 static inline void
 Latch(void)
 {
-    sbi(PORTC, 2);
-    cbi(PORTC, 2);
+	sbi(PORTC, 2);
+	cbi(PORTC, 2);
 }
 
-/** Blocking send a byte via SPI */
+
+/** Blocking send a byte via SPI.
+ *
+ * \todo Move SPI to an interrupt driven work queue so that
+ * it is not necessary to busy wait.
+ */
 static inline void
 spi_write(
-  const byte b
+	const byte b
 )
 {
-  SPDR = b;
-  while (bit_is_clear(SPSR, SPIF))
-    ;
+	SPDR = b;
+	while (bit_is_clear(SPSR, SPIF))
+		;
 }
 
 
-/* Send the three byte command to the LED driver via SPI.
- * Blocks until it is done.
+/*
+ * Send the three byte command to the LED driver via SPI.
+ * \note Blocks until it is done.
  */
-void
-AlphaWrite(byte l1, byte h1, byte h2)
+static void
+AlphaWrite(
+	const byte l1,
+	const byte h1,
+	const byte h2
+)
 {
-  led_all_off();
-  spi_write(l1);
-  spi_write(h1);
-  spi_write(h2);
+	led_all_off();
+	spi_write(l1);
+	spi_write(h1);
+	spi_write(h2);
 }
 
 
 
 
 
-  // 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.
+ *
+ * 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)
 {
-  for (byte j = 0 ; j < 5 ; 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;
-  }
+	for (byte j = 0 ; j < 5 ; 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;
+	}
 }
 
         
+/** Send a digit to one LED.
+ * Blocks until the digit is written and PWM'ed to create the effect.
+ */
 static inline void
 AlphaWritePwm(
-  const byte j,
-  const byte on_period,
-  const byte off_period
+	const byte j,
+	const byte on_period,
+	const byte off_period
 )
 {  
-        AlphaWrite(bufl1[j],bufh1[j],bufh2[j]);      
-        Latch();
-        led_digit_select(j);
- 
-        
-        // For low brightness levels, we use a very low duty cycle PWM.
-        // That's normally fine, but the Arduino interrupts in the background
-        // (for example, the one that keeps our millisecond timer accurate!)
-        // create tiny timing varations, and so these short "on" bursts
-        // can have *very* poor consistency, leading to a jittery, flickery
-        // display.  To get around this, we temporarily turn off interrupts,
-        // for just long enough to turn on the LEDs for a few clock cycles
-        // (under 1 us).   Once interrupts are back on, pending interrupt
-        // requests will be serviced, so we should not lose any time in the
-        // process.  *However* take great care if you extend this 
-        // "interrupt free" section of the code to any longer duration.
-        if (on_period != 0)
-        {
-          led_digit_on();
-          delayMicroseconds(on_period);
-          led_digit_off();
-        } else
-        {
-          // Lower power mode
-          const byte SREGtemp = SREG;
-          cli(); // Disable interrupts
-          led_digit_on();
-          
-          asm volatile("nop\n\t"
-            "nop\n\t"
-            "nop\n\t"
-            "nop\n\t"
-            "nop\n\t"
-            ::);
-  
-          led_digit_off();
-          SREG = SREGtemp; // reenable interrupts.
-        }
-        
-        if (off_period != 0)
-          delayMicroseconds(off_period);
+	AlphaWrite(bufl1[j],bufh1[j],bufh2[j]);      
+	Latch();
+	led_digit_select(j);
+
+	// For low brightness levels, we use a very low duty cycle PWM.
+	// That's normally fine, but the Arduino interrupts in the background
+	// (for example, the one that keeps our millisecond timer accurate!)
+	// create tiny timing varations, and so these short "on" bursts
+	// can have *very* poor consistency, leading to a jittery, flickery
+	// display.  To get around this, we temporarily turn off interrupts,
+	// for just long enough to turn on the LEDs for a few clock cycles
+	// (under 1 us).   Once interrupts are back on, pending interrupt
+	// requests will be serviced, so we should not lose any time in the
+	// process.  *However* take great care if you extend this 
+	// "interrupt free" section of the code to any longer duration.
+
+	if (on_period != 0)
+	{
+		led_digit_on();
+		delayMicroseconds(on_period);
+		led_digit_off();
+	} else
+	{
+		// Lower power mode
+		const byte SREGtemp = SREG;
+		cli(); // Disable interrupts
+		led_digit_on();
+
+		asm volatile("nop\n\t"
+				"nop\n\t"
+				"nop\n\t"
+				"nop\n\t"
+				"nop\n\t"
+				::);
+
+		led_digit_off();
+		SREG = SREGtemp; // reenable interrupts.
+	}
+
+	if (off_period != 0)
+		delayMicroseconds(off_period);
 }
 
 
-
 typedef struct {
-  byte on_period;
-  byte off_period;
-  byte loops;
+	byte on_period;
+	byte off_period;
+	byte loops;
 } pwm_setting_t;
 
-static const pwm_setting_t pwm_settings[] PROGMEM = {
-  {   0,   0, 32 }, // 0
-  {   0, 240, 32 }, // 1
-  {   0,  60, 32 }, // 2
-  {   0,  15, 32 }, // 3
-  {   0,   3, 32 }, // 4
-  {  17, 161, 16 }, // 5
-  {  23, 155, 16 }, // 6
-  {  33, 145, 16 }, // 7
-  {  47, 131, 16 }, // 8
-  {  65, 113, 16 }, // 9
-  {  87,  91, 16 }, // 10
-  { 113,  65, 16 }, // 11
-  { 143,  35, 16 }, // 12
-  { 177,   1, 16 }, // 13
+static const pwm_setting_t
+pwm_settings[] PROGMEM = {
+	{   0,   0, 32 }, // 0
+	{   0, 240, 32 }, // 1
+	{   0,  60, 32 }, // 2
+	{   0,  15, 32 }, // 3
+	{   0,   3, 32 }, // 4
+	{  17, 161, 16 }, // 5
+	{  23, 155, 16 }, // 6
+	{  33, 145, 16 }, // 7
+	{  47, 131, 16 }, // 8
+	{  65, 113, 16 }, // 9
+	{  87,  91, 16 }, // 10
+	{ 113,  65, 16 }, // 11
+	{ 143,  35, 16 }, // 12
+	{ 177,   1, 16 }, // 13
 };
 
-void refreshDisplay (void)
+
+static void
+refreshDisplay(void)
 {
-  unsigned int onPeriod;
-  unsigned int offPeriod;
+	byte bright = MainBright;
 
-  //MainBright can be 0 to 13; 14 levels of brightness. 0 is OFF.
-  //MainBright = 1;  // for test only
+	if (VCRmode && (SecNow % 2) == 0)
+		bright = 0; 
 
-  byte tempbright = MainBright;
+	// If the brightness == 0, blank the LED driver and do nothing else
+	if (bright == 0)
+	{
+		led_digit_off();
+		return;
+	}
 
-  if (VCRmode && (SecNow % 2) == 0)
-      tempbright = 0; 
+	if (bright > 13)
+		bright = 13;
 
-  // If the brightness == 0, blank the LED driver and do nothing else
-  if (tempbright == 0)
-  {
-    led_digit_off();
-    return;
-  }
+	const pwm_setting_t * const pwm = &pwm_settings[bright];
+	const byte loops = pgm_read_byte_near(&pwm->loops);
+	const byte on_period = pgm_read_byte_near(&pwm->on_period);
+	const byte off_period = pgm_read_byte_near(&pwm->off_period);
 
-  if (tempbright > 13)
-    tempbright = 13;
-
-  const pwm_setting_t * const pwm = &pwm_settings[tempbright];
-  const byte loops = pgm_read_byte_near(&pwm->loops);
-  const byte on_period = pgm_read_byte_near(&pwm->on_period);
-  const byte off_period = pgm_read_byte_near(&pwm->off_period);
-
-  for (byte i = 0 ; i < loops ; i++)
-    for (byte j = 0 ; j < 5 ; j++)
-      AlphaWritePwm(j, on_period, off_period);
+	for (byte i = 0 ; i < loops ; i++)
+		for (byte j = 0 ; j < 5 ; j++)
+			AlphaWritePwm(j, on_period, off_period);
 }
 
 
   AlarmNow = 0;
   noTone(13);
   PORTD |= 32; // Turn off speaker
+}
 
-}
 
 void setup()                    // run once, when the sketch starts
 {