Advertisement
Guest User

Untitled

a guest
Feb 27th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.70 KB | None | 0 0
  1. #!/usr/bin/python -O
  2. # Blinky Script
  3. # This script will blink a LED ON and OFF again.
  4. #
  5. # Created on Feb 2, 2013 by Steven Smethurst
  6. # Version: 1.00
  7. #
  8. # Directions
  9. # Connect a LED between Pin 6 ( Ground) and pin 12 (GPIO18)
  10. #
  11. # -- Revision History --
  12. # 20130210      - Adam Scriven  - Added ability to choose on/off times for blink mode
  13. # 20130206      - Adam Scriven  - Added morse code, adapted from: http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/resources/morse_code.py
  14. # 20130205      - Adam Scriven  - Initial from web
  15. #
  16.  
  17. import time
  18. import RPi.GPIO as GPIO
  19. import sys
  20.  
  21. # Setup variables
  22. #
  23. version         = "v1.1"
  24. ledPin          = 12
  25. morseCodeUnit   = 0.2
  26. MORSECODE = {' ': ' ',
  27.         "'": '.----.',
  28.         '(': '-.--.-',
  29.         ')': '-.--.-',
  30.         ',': '--..--',
  31.         '-': '-....-',
  32.         '.': '.-.-.-',
  33.         '/': '-..-.',
  34.         '0': '-----',
  35.         '1': '.----',
  36.         '2': '..---',
  37.         '3': '...--',
  38.         '4': '....-',
  39.         '5': '.....',
  40.         '6': '-....',
  41.         '7': '--...',
  42.         '8': '---..',
  43.         '9': '----.',
  44.         ':': '---...',
  45.         ';': '-.-.-.',
  46.         '?': '..--..',
  47.         'A': '.-',
  48.         'B': '-...',
  49.         'C': '-.-.',
  50.         'D': '-..',
  51.         'E': '.',
  52.         'F': '..-.',
  53.         'G': '--.',
  54.         'H': '....',
  55.         'I': '..',
  56.         'J': '.---',
  57.         'K': '-.-',
  58.         'L': '.-..',
  59.         'M': '--',
  60.         'N': '-.',
  61.         'O': '---',
  62.         'P': '.--.',
  63.         'Q': '--.-',
  64.         'R': '.-.',
  65.         'S': '...',
  66.         'T': '-',
  67.         'U': '..-',
  68.         'V': '...-',
  69.         'W': '.--',
  70.         'X': '-..-',
  71.         'Y': '-.--',
  72.         'Z': '--..',
  73.         '_': '..--.-'
  74. }
  75.  
  76. # Setup GPIO LED handling and dot/dash config
  77. #
  78. GPIO.setmode(GPIO.BOARD)
  79. GPIO.setup(ledPin, GPIO.OUT )
  80. def dot():
  81.         GPIO.output(ledPin,1)
  82.         time.sleep(morseCodeUnit)
  83.         sys.stdout.write('.')
  84.         sys.stdout.flush()
  85.         GPIO.output(ledPin,0)
  86.         time.sleep(morseCodeUnit)
  87.  
  88. def dash():
  89.         GPIO.output(ledPin,1)
  90.         time.sleep(morseCodeUnit * 3)
  91.         sys.stdout.write('-')
  92.         sys.stdout.flush()
  93.         GPIO.output(ledPin,0)
  94.         time.sleep(morseCodeUnit)
  95.  
  96. # print about info
  97. print "Blinky script, "+ version
  98.  
  99. while True:
  100.         input   = raw_input('Do morse code [y|n]? ')
  101.         if input == 'n':
  102.                 onTime  = raw_input('How long should the light be on in seconds? ')
  103.                 offTime = raw_input('How long should the light be off in seconds? ')
  104.                 print "Blinking, "+ onTime + "s on, " +  offTime + "s off."
  105.                 while 1:
  106.                         GPIO.output(ledPin, GPIO.HIGH )
  107.                         time.sleep(float(onTime))
  108.                         GPIO.output(ledPin, GPIO.LOW )
  109.                         time.sleep(float(offTime))
  110.         else:
  111.                 input = raw_input('What would you like to send? ')
  112.                 for letter in input:
  113.                                 for symbol in MORSECODE[letter.upper()]:
  114.                                         if symbol == '-':
  115.                                                 dash()
  116.                                         elif symbol == '.':
  117.                                                 dot()
  118.                                         else:
  119.                                                 time.sleep(0.5)
  120.                                                 sys.stdout.write(' ')
  121.                                                 sys.stdout.flush()
  122.                                 time.sleep(0.5)
  123.                                 sys.stdout.write(' ')
  124.                                 sys.stdout.flush()
  125.                 print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement