Commits

Trammell Hudson committed 36dbedf

Alternating works

Comments (0)

Files changed (1)

 #include "usb_keyboard_debug.h"
 #include "print.h"
 
-#define LED_CONFIG	(DDRD |= (1<<6))
 #define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))
 
+#define LED_PIN (1 << 6)
+
 static inline void
 led_on(void)
 {
-	PORTD |= (1<<6);
+	PORTD |= LED_PIN;
 }
 
 static inline void
 led_off(void)
 {
-	PORTD &= ~(1<<6);
+	PORTD &= ~LED_PIN;
+}
+
+static inline void
+led_config(void)
+{
+	DDRD |= LED_PIN;
 }
 
 uint8_t number_keys[10]=
 
 uint16_t idle_count=0;
 
+
+static inline int
+is_dah(
+	const uint8_t value
+)
+{
+	return bit_is_clear(value, 5);
+}
+
+
+static inline int
+is_dit(
+	const uint8_t value
+)
+{
+	return bit_is_clear(value, 4);
+}
+
+
+static uint8_t
+busy_wait(
+	uint16_t delay_ticks
+)
+{
+	uint8_t port = 0xFF;
+
+	OCR1A = TCNT1 + delay_ticks;
+	TIFR1 |= (1 << OCF1A);
+
+	while (bit_is_clear(TIFR1, OCF1A))
+		port &= PINB;
+
+	return port;
+}
+
+
+static uint8_t
+wait(
+	uint16_t on_ticks,
+	uint16_t off_ticks
+)
+{
+	uint8_t port = 0xFF;
+
+	led_on();
+	port &= busy_wait(on_ticks);
+
+	led_off();
+	port &= busy_wait(off_ticks);
+
+	return port;
+}
+
+
 int main(void)
 {
 	uint8_t b, d, mask, i, reset_idle;
 
 	// set for 16 MHz clock
 	CPU_PRESCALE(0);
+	led_config();
 
 	// Configure all port B and port D pins as inputs with pullup resistors.
 	// See the "Using I/O Pins" page for details.
 	// and do whatever it does to actually be ready for input
 	_delay_ms(1000);
 
-	// Timer 0 will be configured to generate an overflow event
-	// at the desired code rate.  For dits, 
 /*
 * http://en.wikipedia.org/wiki/Morse_code#Representation.2C_timing_and_speeds
 *
 	TCCR0B = 0x05;
 	//TIMSK0 = (1<<TOIE0);
 
-	// Hard code at 5 wpm
-	const uint16_t dit_time = 240;
+	// TCNT1 runs at CLK/1024, which is 0.064 ms per tick
+	// This is pretty close to 16 ticks/ms.  The output compare
+	// sets OCF flag on match
+	TCCR1B = 5; // clk/1024
+	const uint8_t cpm = 10;
+	const uint16_t dit_time = (1600 / cpm) * 16;
 
 	uint8_t value = 1;
 	uint8_t bits = 0;
 		// Read PINB, which has both input pins
 		// since there is a pull-up, the pins will be pulled
 		// to ground when the switch is hit.
-		const uint8_t pins = PINB;
-		const uint8_t dit = (pins & (1 << 5)) ? 0 : 1;
-		const uint8_t dah = (pins & (1 << 4)) ? 0 : 1;
 
 		if (bits > 7)
 		{
 			print("?\n");
 			goto reset;
 		} else
-		if (dit && (last_bit == 1 || !dah))
+		if (is_dit(PINB))
 		{
 			// The dit key is held down and
 			// If the last bit sent was also a dit and the
 			// dah switch is enabled, send a dah instead.
-			last_bit = 0;
+start_dit_bit:
 			bits++;
 			value <<= 1;
 			print(".");
-			led_on();
-			_delay_ms(dit_time);
-			led_off();
-			_delay_ms(dit_time);
+
+			const uint8_t port = wait(dit_time, dit_time);
+			if (is_dah(port))
+				goto start_dah_bit;
+			//if (is_dit(port))
+				//goto start_dit_bit;
+
 			continue;
 		} else
-		if (dah)
+		if (is_dah(PINB))
 		{
-			last_bit = 1;
+start_dah_bit:
 			bits++;
 			value = (value << 1) | 1;
 			print("-");
-			led_on();
-			_delay_ms(3*dit_time);
-			led_off();
-			_delay_ms(dit_time);
+
+			const uint8_t port = wait(3*dit_time, dit_time);
+			if (is_dit(port))
+				goto start_dit_bit;
+			//if (is_dah(port))
+				//goto start_dah_bit;
+
 			continue;
 		} else
 		if (bits == 0)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.