diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3367d69 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# +# generated files +# +*.pyc +# +# temp files +# +*~ diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index f5e5162..c9b7bcc 100644 --- a/Adafruit_Thermal.py +++ b/Adafruit_Thermal.py @@ -50,7 +50,7 @@ class Adafruit_Thermal(Serial): lineSpacing = 8 barcodeHeight = 50 printMode = 0 - defaultHeatTime = 60 + defaultHeatTime = 45 def __init__(self, *args, **kwargs): # If no parameters given, use default port & baud rate. diff --git a/forecast.py b/forecast.py index d985497..6d8489b 100755 --- a/forecast.py +++ b/forecast.py @@ -23,7 +23,7 @@ # by 'manually' visiting http://weather.yahoo.com, entering a location # and requesting a forecast, then copy the number from the end of the # current URL string and paste it here. -WOEID = '2459115' +WOEID = '34918' # Dumps one forecast line to the printer def forecast(idx): diff --git a/gfx/.gitignore b/gfx/.gitignore new file mode 100644 index 0000000..1b808db --- /dev/null +++ b/gfx/.gitignore @@ -0,0 +1,4 @@ +# +# generated files +# +*.pyc diff --git a/gfx/graph.png b/gfx/graph.png new file mode 100644 index 0000000..744745b Binary files /dev/null and b/gfx/graph.png differ diff --git a/gfx/logo_130x80.png b/gfx/logo_130x80.png new file mode 100644 index 0000000..f30f8e2 Binary files /dev/null and b/gfx/logo_130x80.png differ diff --git a/gfx/partlycloudy.gif b/gfx/partlycloudy.gif new file mode 100644 index 0000000..117f06d Binary files /dev/null and b/gfx/partlycloudy.gif differ diff --git a/gfx/result.gif b/gfx/result.gif new file mode 100644 index 0000000..30312a4 Binary files /dev/null and b/gfx/result.gif differ diff --git a/gfx/result.png b/gfx/result.png new file mode 100644 index 0000000..b3b789c Binary files /dev/null and b/gfx/result.png differ diff --git a/imgbw.py b/imgbw.py new file mode 100755 index 0000000..f79fae2 --- /dev/null +++ b/imgbw.py @@ -0,0 +1,8 @@ +from PIL import Image +image_file = Image.open("gfx/partlycloudy.gif") # open colour image +#image_file = image_file.resize((100,100),Image.ANTIALIAS) +image_file = image_file.convert('L') # convert image to black and white +image_file.save('result.png') +image_file = Image.open("result.png") # open colour image +image_file = image_file.convert('1') # convert image to black and white +image_file.save('result.gif') \ No newline at end of file diff --git a/main.py b/main.py index 4af3456..73ed8d0 100755 --- a/main.py +++ b/main.py @@ -39,8 +39,12 @@ def tap(): # Called when button is held down. Prints image, invokes shutdown process. def hold(): GPIO.output(ledPin, GPIO.HIGH) + ht = printer.defaultHeatTime * 2 + if(ht > 255): ht = 255 + printer.begin(ht) # Set temporary dark heat time printer.printImage(Image.open('gfx/goodbye.png'), True) printer.feed(3) + printer.begin() # Reset default heat time subprocess.call("sync") subprocess.call(["shutdown", "-h", "now"]) GPIO.output(ledPin, GPIO.LOW) @@ -61,7 +65,7 @@ def interval(): def daily(): GPIO.output(ledPin, GPIO.HIGH) subprocess.call(["python", "forecast.py"]) - subprocess.call(["python", "sudoku-gfx.py"]) + #subprocess.call(["python", "sudoku-gfx.py"]) GPIO.output(ledPin, GPIO.LOW) @@ -79,13 +83,27 @@ def daily(): # Processor load is heavy at startup; wait a moment to avoid # stalling during greeting. -time.sleep(30) +time.sleep(60) + +# Print greeting image +# Because the hello/goodbye images are overall fairly light, we can +# get away with using a darker heat time for these, then reset to the +# default afterward. +ht = printer.defaultHeatTime * 2 +if(ht > 255): ht = 255 + +printer.begin(ht) # Set temporary dark heat time +printer.printImage(Image.open('gfx/hello.png'), True) +printer.feed(3) +printer.begin() # Reset default heat time # Show IP address (if network is available) try: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(('8.8.8.8', 0)) + printer.justify('C') printer.print('My IP address is ' + s.getsockname()[0]) + printer.justify('L') printer.feed(3) except: printer.boldOn() @@ -96,9 +114,6 @@ def daily(): printer.feed(3) exit(0) -# Print greeting image -printer.printImage(Image.open('gfx/hello.png'), True) -printer.feed(3) GPIO.output(ledPin, GPIO.LOW) # Poll initial button state and time diff --git a/pimage.py b/pimage.py new file mode 100755 index 0000000..a8e1197 --- /dev/null +++ b/pimage.py @@ -0,0 +1,33 @@ + +from __future__ import print_function +import RPi.GPIO as GPIO +import subprocess, time, Image, socket +from Adafruit_Thermal import * + +printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5) + +# Print greeting image +# Because the hello/goodbye images are overall fairly light, we can +# get away with using a darker heat time for these, then reset to the +# default afterward. +ht = printer.defaultHeatTime * 2 +if(ht > 255): ht = 255 + +printer.begin(ht) # Set temporary dark heat time +image_file = Image.open('gfx/partlycloudy.gif') +image_100 = image_file.resize((100,100),Image.ANTIALIAS) +image_200 = image_file.resize((200,200),Image.ANTIALIAS) +image_file = image_file.convert('L') +image_100 = image_100.convert('L') +image_200 = image_200.convert('L') +printer.print("Image 50x50") +printer.printImage(image_file, True) +printer.feed(3) +printer.print("Image 100x100") +printer.printImage(image_100, True) +printer.feed(3) +printer.print("Image 200x200") +printer.printImage(image_200, True) +printer.feed(3) +printer.begin() # Reset default heat time + diff --git a/printertest2.py b/printertest2.py new file mode 100755 index 0000000..a8c1cfc --- /dev/null +++ b/printertest2.py @@ -0,0 +1,88 @@ +#!/usr/bin/python + +from __future__ import print_function +from Adafruit_Thermal import * +import Image + +printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5) + +# Because the hello/goodbye images are overall fairly light, we can +# get away with using a darker heat time for these, then reset to the +# default afterward. +ht = printer.defaultHeatTime * 2 +if(ht > 255): ht = 255 + +printer.begin(ht) # Set temporary dark heat time +img = Image.open('gfx/hello.png') +printer.printImage(img, True) +printer.feed(3) +printer.begin() # Reset default heat time + +# Test inverse on & off +printer.inverseOn() +printer.println("Inverse ON") +printer.inverseOff() + +# Test character double-height on & off +printer.doubleHeightOn() +printer.println("Double Height ON") +printer.doubleHeightOff() + +# Set justification (right, center, left) -- accepts 'L', 'C', 'R' +printer.justify('R') +printer.println("Right justified") +printer.justify('C') +printer.println("Center justified") +printer.justify('L') +printer.println("Left justified") + +# Test more styles +printer.boldOn() +printer.println("Bold text") +printer.boldOff() + +printer.underlineOn() +printer.println("Underlined text") +printer.underlineOff() + +printer.setSize('L') # Set type size, accepts 'S', 'M', 'L' +printer.println("Large") +printer.setSize('M') +printer.println("Medium") +printer.setSize('S') +printer.println("Small") + +printer.justify('C') +printer.println("normal\nline\nspacing") +printer.setLineHeight(50) +printer.println("Taller\nline\nspacing") +printer.setLineHeight() # Reset to default +printer.justify('L') + +# Barcode examples +printer.feed(1) +# CODE39 is the most common alphanumeric barcode +printer.printBarcode("ADAFRUT", printer.CODE39) +printer.setBarcodeHeight(100) +# Print UPC line on product barcodes +printer.printBarcode("123456789123", printer.UPC_A) + +# Print the 75x75 pixel logo in adalogo.py +import gfx.adalogo as adalogo +printer.printBitmap(adalogo.width, adalogo.height, adalogo.data) + +# Print the 135x135 pixel QR code in adaqrcode.py +import gfx.adaqrcode as adaqrcode +printer.printBitmap(adaqrcode.width, adaqrcode.height, adaqrcode.data) +printer.println("Adafruit!") +printer.feed(1) + +printer.begin(ht) # Set temporary dark heat time +img = Image.open('gfx/goodbye.png') +printer.printImage(img, True) +printer.feed(3) +printer.begin() # Reset default heat time + +printer.sleep() # Tell printer to sleep +printer.wake() # Call wake() before printing again, even if reset +printer.setDefault() # Restore printer to defaults diff --git a/sudoku-gfx.py b/sudoku-gfx.py index 91b5204..73194c1 100755 --- a/sudoku-gfx.py +++ b/sudoku-gfx.py @@ -43,8 +43,15 @@ def main(): puzzles = [makepuzzle(solution([None] * 81))] for puzzle in puzzles: printboard(puzzle) # Doesn't print, just modifies 'bg' image + ht = printer.defaultHeatTime + 15 + if(ht > 255): ht = 255 + printer.begin(ht) # Set temporary dark heat time printer.printImage(bg, True) # This does the printing - printer.println("RATING:", ratepuzzle(puzzle, 4)) + rating_lvl="RATING:" + str(ratepuzzle(puzzle, 4)) + scratchpad="1 2 3 4 5 6 7 8 9" + spaces_req=" "*(32-(len(rating_lvl)+len(scratchpad))) + rating_scratchpad=rating_lvl + spaces_req + scratchpad + printer.println(rating_scratchpad) if len(args) > 0: printer.println() printer.println("SOLUTION:") @@ -52,6 +59,7 @@ def main(): if answer is None: printer.println("NO SOLUTION") else: printer.print(printboard(answer)) printer.feed(3) + printer.begin() # Reset default heat time def makepuzzle(board): puzzle = []; deduced = [None] * 81 diff --git a/timetemp.py b/timetemp.py index 2835782..4902f6b 100755 --- a/timetemp.py +++ b/timetemp.py @@ -23,7 +23,7 @@ # by 'manually' visiting http://weather.yahoo.com, entering a location # and requesting a forecast, then copy the number from the end of the # current URL string and paste it here. -WOEID = '2459115' +WOEID = '34918' # Fetch weather data from Yahoo!, parse resulting XML dom = parseString(urllib.urlopen( @@ -163,5 +163,9 @@ def numWidth(str, list): # Open connection to printer and print image printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5) +ht = printer.defaultHeatTime * 2 +if(ht > 255): ht = 255 +printer.begin(ht) # Set temporary dark heat time printer.printImage(img, True) printer.feed(3) +printer.begin() # Reset default heat time diff --git a/twitter.py b/twitter.py index 4b579d7..3c47a7d 100755 --- a/twitter.py +++ b/twitter.py @@ -1,3 +1,4 @@ + #!/usr/bin/python # This is a Python port of Adafruit's "Gutenbird" sketch for Arduino. @@ -27,7 +28,7 @@ # with dummy strings. from __future__ import print_function -import base64, HTMLParser, httplib, json, sys, urllib, zlib +import base64, HTMLParser, httplib, json, sys, urllib, zlib, textwrap from unidecode import unidecode from Adafruit_Thermal import * @@ -35,15 +36,32 @@ # Configurable globals. Edit to your needs. ------------------------------- # Twitter application credentials -- see notes above -- DO NOT SHARE. -consumer_key = 'PUT_YOUR_CONSUMER_KEY_HERE' -consumer_secret = 'PUT_YOUR_CONSUMER_SECRET_HERE' +# Have both key and secret set to load from file, makes my life easier +# pushing to GitHub +# Doesn't require the twitter-key & twitter-secret file; if you're not +# planning to share this file, just enter those details below +try: + file_key = open('/home/pi/twitter-key') + consumer_key = file_key.readline().rstrip('\n') +except: + consumer_key = 'PUT_YOUR_CONSUMER_KEY_HERE' +else: + file_key.close() +try: + file_secret = open('/home/pi/twitter-secret') + consumer_secret = file_secret.readline().rstrip('\n') +except: + consumer_secret = 'PUT_YOUR_CONSUMER_SECRET_HERE' +else: + file_secret.close() # queryString can be any valid Twitter API search string, including # boolean operators. See http://dev.twitter.com/docs/using-search # for options and syntax. Funny characters do NOT need to be URL # encoded here -- urllib takes care of that. -queryString = 'from:Adafruit' - +#queryString = 'from:Adafruit OR from:SmartTube' +#queryString = 'from:SmartTube OR from:SmartSouthWales' +queryString = 'from:SmartArduino' # Other globals. You probably won't need to change these. ----------------- @@ -113,9 +131,11 @@ def issueRequestAndDecodeResponse(method, url, body, headers): # Remove HTML escape sequences # and remap Unicode values to nearest ASCII equivalents - printer.print(unidecode( - HTMLParser.HTMLParser().unescape(tweet['text']))) - + # Use textwrap module to wrap neatly at 32 characters + tweettext=textwrap.wrap(unidecode( + HTMLParser.HTMLParser().unescape(tweet['text'])),32) + for line in tweettext: + printer.print(line + "\n") printer.feed(3) print(data['search_metadata']['max_id_str']) # Piped back to calling process diff --git a/wgforecast.py b/wgforecast.py new file mode 100755 index 0000000..5554ae4 --- /dev/null +++ b/wgforecast.py @@ -0,0 +1,9 @@ +#!/usr/bin/python +# +# Forecast for adafruit raspberry pi thermal +# internet of things printer. +# This version uses wunderground for the +# weather data. Later revisions will have +# weather icon support. +# +