Commits

Trammell Hudson committed ed84fd9

Translate the alphabet into binary

  • Participants
  • Parent commits 075bbba

Comments (0)

Files changed (2)

-/* Keyboard example with debug channel, for Teensy USB Development Board
+/** \file
+ * Iambic USB keyer.
+ *
+ * Read an iambic keyer on port D and generate key press events
+ * based on the Morse code input.
+ *
+ * (c) 2012 Trammell Hudson <hudson@osresearch.net>
+ *
+ * Based on the
+ * Keyboard example with debug channel, for Teensy USB Development Board
  * http://www.pjrc.com/teensy/usb_keyboard.html
  * Copyright (c) 2008 PJRC.COM, LLC
  * 
 	// and do whatever it does to actually be ready for input
 	_delay_ms(1000);
 
-	// Configure timer 0 to generate a timer overflow interrupt every
+	// 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
+*
+* International Morse code is composed of five elements:
+*
+* short mark, dot or 'dit' (·) — 'dot duration' is one unit long
+* longer mark, dash or 'dah' (–) — three units long
+* inter-element gap between the dots and dashes within a character
+*   - one dot duration or one unit long
+* short gap (between letters) — three units long
+* medium gap (between words) — seven units long[19]
+*
+* Based upon a 50 dot duration standard word such as PARIS, the time
+* for one dot duration or one unit can be computed by the formula:
+* T = 1200 / W
+* or
+* T = 6000 / C
+* Where: T is the unit time, or dot duration, in milliseconds,
+* 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 -----
+*/
+
+
+
+	// 
 	// 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
 	// This demonstrates how to use interrupts to implement a simple
 	// inactivity timeout.
 	TCCR0A = 0x00;
 	TCCR0B = 0x05;
-	TIMSK0 = (1<<TOIE0);
+	//TIMSK0 = (1<<TOIE0);
 
-	print("Begin keyboard example program\n");
-	print("All Port B or Port D pins are inputs with pullup resistors.\n");
-	print("Any connection to ground on Port B or D pins will result in\n");
-	print("keystrokes sent to the PC (and debug messages here).\n");
-	while (1) {
-		// read all port B and port D pins
-		b = PINB;
-		d = PIND;
+	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);
+
+		// If neither is pressed, we have nothing to do.
+		if (!dit && !dah)
+			continue;
+
+		// One of them has changed; figure out which one
+
 		// check if any pins are low, but were high previously
 		mask = 1;
 		reset_idle = 0;
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+print "static const uint8_t morse[] PROGMEN = {\n";
+
+while (<DATA>)
+{
+	chomp;
+	my ($c,$m) = split;
+	my $v = 1;
+
+	for my $b (split //, $m)
+	{
+		$v <<= 1;
+		$v |= 1 if $b eq '-';
+	}
+
+	printf "\t[0x%04x] = '%s', // %s\n",
+		$v,
+		$c,
+		$m,
+		;
+}
+
+print "};\n";
+
+__DATA__
+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 -----
+. .-.-.-
+, --..--
+? ..--..
+' .----.
+! -.-.--
+/ -..-.
+( -.--.
+) -.--.-
+& .-...
+: ---...
+; -.-.-.
+= -..-
++ .-.-.
+- -....-
+_ ..--.-
+" .-..-.
+$ ...-..-
+@ .--.-.