Commits

Trammell Hudson committed 6a0319a Draft

first cut -- it works!

Comments (0)

Files changed (1)

 #include "print.h"
 
 #define LED_CONFIG	(DDRD |= (1<<6))
-#define LED_ON		(PORTD &= ~(1<<6))
-#define LED_OFF		(PORTD |= (1<<6))
 #define CPU_PRESCALE(n)	(CLKPR = 0x80, CLKPR = (n))
 
+static inline void
+led_on(void)
+{
+	PORTD |= (1<<6);
+}
+
+static inline void
+led_off(void)
+{
+	PORTD &= ~(1<<6);
+}
+
 uint8_t number_keys[10]=
 	{KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,KEY_6,KEY_7,KEY_8,KEY_9};
 
 	// If the Teensy is powered without a PC connected to the USB port,
 	// this will wait forever.
 	usb_init();
-	while (!usb_configured()) /* wait */ ;
+	while (!usb_configured())
+	{
+		/* busy wait */
+	}
 
 	// Wait an extra second for the PC's operating system to load drivers
 	// and do whatever it does to actually be ready for input
 * W is the speed in wpm, and C is the speed in cpm.
 */
 
-/*
- * The morse alphabet is encoded into a tree with . == 0, - == 1 and
- * a leading 1 to differentiate between the ambiguous cases.
-A .-
-B -...
-C -.-.
-D -..
-E .
-F ..-.
-G --.
-H ....
-I ..
-J .---
-K -.-
-L .-..
-M --
-N -.
-O ---
-P .--.
-Q --.-
-R .-.
-S ...
-T -
-U ..-
-V ...-
-W .--
-X -..-
-Y -.--
-Z --..
-1 .----
-2 ..---
-3 ...--
-4 ....-
-5 .....
-6 -....
-7 --...
-8 ---..
-9 ----.
-0 -----
-*/
-
-
+#include "morse.h"
 
 	// 
 	// 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
 	TCCR0B = 0x05;
 	//TIMSK0 = (1<<TOIE0);
 
+	// Hard code at 5 wpm
+	const uint16_t dit_time = 240;
+
+	uint8_t value = 1;
+	uint8_t bits = 0;
+	uint8_t last_bit = 0;
+
 	while (1)
 	{
-		// Read PIND, which has both input pins
-		const uint8_t pins = PIND;
-		const uint8_t dit = pins & (1 << 5);
-		const uint8_t dah = pins & (1 << 4);
+		// 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 neither is pressed, we have nothing to do.
-		if (!dit && !dah)
+		if (bits > 7)
+		{
+			// Too many symbols.  Ignore it
+			print("?\n");
+			goto reset;
+		} else
+		if (dit && (last_bit == 1 || !dah))
+		{
+			// 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;
+			bits++;
+			value <<= 1;
+			print(".");
+			led_on();
+			_delay_ms(dit_time);
+			led_off();
+			_delay_ms(dit_time);
 			continue;
+		} else
+		if (dah)
+		{
+			last_bit = 1;
+			bits++;
+			value = (value << 1) | 1;
+			print("-");
+			led_on();
+			_delay_ms(3*dit_time);
+			led_off();
+			_delay_ms(dit_time);
+			continue;
+		} else
+		if (bits == 0)
+		{
+#if 0
+			if (last_send_time && now() > last_send_time)
+				usb_keyboard_press(KEY_SPACE, 0);
+			last_send_time = 0;
+#endif
+			continue;
+		}
 
-		// One of them has changed; figure out which one
+		// Neither key is held down and the single space time
+		// has elapsed (since wait_delay is blocking).
 
-		// check if any pins are low, but were high previously
-		mask = 1;
-		reset_idle = 0;
-		for (i=0; i<8; i++) {
-			if (((b & mask) == 0) && (b_prev & mask) != 0) {
-				usb_keyboard_press(KEY_B, KEY_SHIFT);
-				usb_keyboard_press(number_keys[i], 0);
-				print("Port B, bit ");
-				phex(i);
-				print("\n");
-				reset_idle = 1;
-			}
-			if (((d & mask) == 0) && (d_prev & mask) != 0) {
-				usb_keyboard_press(KEY_D, KEY_SHIFT);
-				usb_keyboard_press(number_keys[i], 0);
-				print("Port D, bit ");
-				phex(i);
-				print("\n");
-				reset_idle = 1;
-			}
-			mask = mask << 1;
+		// Timeout has passed; check to see if the
+		// value exist in the map
+		uint8_t c = pgm_read_byte(&morse[value]);
+
+		//last_send_time = now();
+		print(" = ");
+		phex(value);
+
+		if (!c)
+		{
+			print("!!!\n");
+			goto reset;
 		}
-		// if any keypresses were detected, reset the idle counter
-		if (reset_idle) {
-			// variables shared with interrupt routines must be
-			// accessed carefully so the interrupt routine doesn't
-			// try to use the variable in the middle of our access
-			cli();
-			idle_count = 0;
-			sei();
-		}
-		// now the current pins will be the previous, and
-		// wait a short delay so we're not highly sensitive
-		// to mechanical "bounce".
-		b_prev = b;
-		d_prev = d;
-		_delay_ms(2);
+
+		print(" ");
+		pchar(c);
+		pchar('\n');
+		
+		//usb_keyboard_press(KEY_SPACE, 0);
+
+reset:
+		bits = 0;
+		value = 1;
 	}
 }
 
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.