Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python -O
- # Blinky Script
- # This script will blink a LED ON and OFF again.
- #
- # Created on Feb 2, 2013 by Steven Smethurst
- # Version: 1.00
- #
- # Directions
- # Connect a LED between Pin 6 ( Ground) and pin 12 (GPIO18)
- #
- # -- Revision History --
- # 20130210 - Adam Scriven - Added ability to choose on/off times for blink mode
- # 20130206 - Adam Scriven - Added morse code, adapted from: http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/robot/resources/morse_code.py
- # 20130205 - Adam Scriven - Initial from web
- #
- import time
- import RPi.GPIO as GPIO
- import sys
- # Setup variables
- #
- version = "v1.1"
- ledPin = 12
- morseCodeUnit = 0.2
- MORSECODE = {' ': ' ',
- "'": '.----.',
- '(': '-.--.-',
- ')': '-.--.-',
- ',': '--..--',
- '-': '-....-',
- '.': '.-.-.-',
- '/': '-..-.',
- '0': '-----',
- '1': '.----',
- '2': '..---',
- '3': '...--',
- '4': '....-',
- '5': '.....',
- '6': '-....',
- '7': '--...',
- '8': '---..',
- '9': '----.',
- ':': '---...',
- ';': '-.-.-.',
- '?': '..--..',
- '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': '--..',
- '_': '..--.-'
- }
- # Setup GPIO LED handling and dot/dash config
- #
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(ledPin, GPIO.OUT )
- def dot():
- GPIO.output(ledPin,1)
- time.sleep(morseCodeUnit)
- sys.stdout.write('.')
- sys.stdout.flush()
- GPIO.output(ledPin,0)
- time.sleep(morseCodeUnit)
- def dash():
- GPIO.output(ledPin,1)
- time.sleep(morseCodeUnit * 3)
- sys.stdout.write('-')
- sys.stdout.flush()
- GPIO.output(ledPin,0)
- time.sleep(morseCodeUnit)
- # print about info
- print "Blinky script, "+ version
- while True:
- input = raw_input('Do morse code [y|n]? ')
- if input == 'n':
- onTime = raw_input('How long should the light be on in seconds? ')
- offTime = raw_input('How long should the light be off in seconds? ')
- print "Blinking, "+ onTime + "s on, " + offTime + "s off."
- while 1:
- GPIO.output(ledPin, GPIO.HIGH )
- time.sleep(float(onTime))
- GPIO.output(ledPin, GPIO.LOW )
- time.sleep(float(offTime))
- else:
- input = raw_input('What would you like to send? ')
- for letter in input:
- for symbol in MORSECODE[letter.upper()]:
- if symbol == '-':
- dash()
- elif symbol == '.':
- dot()
- else:
- time.sleep(0.5)
- sys.stdout.write(' ')
- sys.stdout.flush()
- time.sleep(0.5)
- sys.stdout.write(' ')
- sys.stdout.flush()
- print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement