Commits

Trammell Hudson committed b501ad7 Draft

Brightness and overflow counting

Comments (0)

Files changed (1)

  */
 static uint8_t outputs[4];
 
+/** Brightness goes from 0 to 16 */
+static uint8_t brightness = 16;
+
 
 static void
 draw(void)
 {
-	uint8_t mask = 3;
+	uint8_t mask = 1;
 
-	for (uint8_t i = 0 ; i < 4 ; i++, mask <<= 2)
+	for (uint8_t i = 0 ; i < 7 ; i++, mask <<= 1)
 	{
 		// skip output 6
-		//if (i == 6)
-			////mask <<= 1;
+		if (i == 6)
+			mask <<= 1;
 
-		DDRB = outputs[0] & mask;
-		DDRC = outputs[1] & mask;
-		DDRD = outputs[2] & mask;
+		DDRC = outputs[0] & mask;
+		DDRD = outputs[1] & mask;
+		DDRB = outputs[2] & mask;
 		DDRF = outputs[3] & mask;
-		_delay_us(10);
+		_delay_us(brightness);
+
+		DDRB = 0;
+		DDRC = 0;
+		DDRD = 0;
+		DDRF = 0;
+		_delay_us(16 - brightness);
 	}
 
-	DDRB = 0;
-	DDRC = 0;
-	DDRD = 0;
-	DDRF = 0;
 }
 
 
 	ddr(LED, 1);
 	out(LED, 1);
 
+        // Timer 0 is used for a 64 Hz control loop timer.
+        // Clk/1024 == 15.625 KHz, count up to 244
+        // CTC mode resets the counter when it hits the top
+        TCCR0A = 0
+                | 1 << WGM01 // select CTC
+                | 0 << WGM00
+                ;
+
+        TCCR0B = 0
+                | 0 << WGM02
+                | 1 << CS02 // select Clk/1024
+                | 0 << CS01
+                | 0 << CS00
+                ;
+
+        OCR0A = 244;
+        sbi(TIFR0, OCF0A); // reset the overflow bit
+
+
 	while (!usb_configured())
 		;
 
 	usb_serial_flush_input();
 
 	send_str(PSTR("seven segment\r\n"));
-	uint8_t val = 9;
+	uint16_t val = 0xdead;
 	DDRC = 1 << 7;
 	PORTC = 0;
 
+	outputs[0] = digits[0];
+	outputs[1] = digits[1];
+	outputs[2] = digits[2];
+	outputs[3] = digits[3];
+
+	for (uint16_t i = 0 ; i < 64 ; )
+	{
+		draw();
+		if (bit_is_clear(TIFR0, OCF0A))
+			continue;
+		sbi(TIFR0, OCF0A);
+		i++;
+	}
+
+	uint8_t chase = 0;
 
 	while (1)
 	{
 		int c = usb_serial_getchar();
 		if (c != -1)
 		{
+			if (c == '+')
+			{
+				if (brightness != 16)
+					brightness++;
+			} else
+			if (c == '-')
+			{
+				if (brightness != 0)
+					brightness --;
+			}
+		}
+
+		if (bit_is_clear(TIFR0, OCF0A))
+		{
+			draw();
 			continue;
 		}
 
-		if (val == 10)
-		{
-			//chase(4, 100);
-			val = 0;
-		}
+		sbi(TIFR0, OCF0A); // reset the bit
 
-		const uint8_t output = digits[val++];
-		outputs[0] = outputs[1] = outputs[2] = outputs[3] = output;
-
-		for (uint16_t i = 0 ; i < 20000 ; i++)
-			draw();
+		uint16_t o = (val++) / 128;
+		outputs[3] = digits[o % 10]; o /= 10;
+		outputs[2] = digits[o % 10]; o /= 10;
+		outputs[1] = digits[o % 10]; o /= 10;
+		outputs[0] = digits[o % 10]; o /= 10;
 	}
 }