main.py

00001 # This file is part of the Python-on-a-Chip program.
00002 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00003 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00004 # 
00005 # Python-on-a-Chip is distributed in the hope that it will be useful,
00006 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00007 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00008 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00009 # is seen in the file COPYING up one directory from this.
00010 
00011 #
00012 # This is a sample application that duplicates a stripped-back version of Teensy's own "blinky.c"
00013 # in Python - blinking an LED with morse code and concurrently outputting a text version via usb serial
00014 #
00015 
00016 # Implementation by Angus Gratton, 2010.
00017 
00018 
00019 print "Hello world."
00020 
00021 import avr
00022 
00023 # Swap this number to 2 if you have a Teensy++ 2.0 (changes LED pins & polarity)
00024 teensyVersion = 1
00025 
00026 # NB: 8kb is not enough RAM to have a full translation table character -> morse code string 
00027 # in a dictionary!
00028 #
00029 # So we're going with a stripped down morse code(!) If you comment out
00030 # some of the other functions in avr.py (portA & ddrA are uncommented
00031 # by default) & __bi.py then you save a bit more RAM and can uncomment
00032 # more morse characters without running out of RAM!
00033 morse = {
00034 #    'A' : '.-',
00035 #    'B' : '-...',
00036 #    'C' : '-.-.',
00037     'D' : '-..',
00038     'E' : '.',
00039 #    'F' : '..-.',
00040 #    'G' : '--.',
00041 #    'H' : '....',
00042 #    'I' : '..',
00043 #    'J' : '.---',
00044 #    'K' : '-.-',
00045     'L' : '.-..',
00046     'M' : '--',
00047 #    'N' : '-.',
00048     'O' : '---',
00049 #    'P' : '.--.',
00050 #    'Q' : '--.-',
00051 #    'R' : '.-.',
00052     'S' : '...',
00053 #    'T' : '-',
00054 #    'U' : '..-',
00055 #    'V' : '...-',
00056 #    'W' : '.--',
00057 #    'X' : '-..-',
00058 #    'Y' : '-.--',
00059 #    'Z' : '--..',
00060 }
00061 
00062 def loop():
00063     for i in range(1,6):
00064         printMorse("SOS")
00065         avr.delay(1500)
00066     printMorse("DOES ANYBODY STILL KNOW MORSE CODE?")
00067     avr.delay(4000)
00068 
00069 def printMorseCharacter(c):
00070     print "Char " + c,
00071     if c in morse:
00072         for s in morse[c]:
00073             # Some constants, in here because we run out of memory if we put them at the top-level            
00074             ledOn = False if teensyVersion == 1 else True
00075             ledPin = 6 if teensyVersion == 1 else 1
00076             ledPort = 'D' if teensyVersion == 1 else 'C'
00077             dit = 80
00078 
00079             avr.digitalWrite(ledPort, ledPin, ledOn)
00080             if s == '.':
00081                 print " dit",
00082                 avr.delay(dit)
00083             elif s == '-':
00084                 print " dah",
00085                 dah = dit * 3
00086                 avr.delay(dah)
00087             else:
00088                 print " ?",            
00089             avr.digitalWrite(ledPort, ledPin, not ledOn)
00090             avr.delay(dit)  
00091     else:
00092         print " ?",
00093     print
00094 
00095 def printMorse(msg):
00096     print "Message " + msg
00097     for c in msg:
00098         printMorseCharacter(c)
00099 
00100 while True:
00101     loop()

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9