Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.

Commit 653213f

Browse files
committed
Merges changes from upstream Adafruit lib
Signed-off-by: zachwick <zach@zachwick.com>
2 parents 2a2e883 + 8165c22 commit 653213f

File tree

9 files changed

+783
-777
lines changed

9 files changed

+783
-777
lines changed

Adafruit_Thermal/Adafruit_Thermal.py

Lines changed: 696 additions & 695 deletions
Large diffs are not rendered by default.

examples/calibrate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from __future__ import print_function
2424
from Adafruit_Thermal import *
2525

26-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
26+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
2727

2828
for i in range(0,256,15):
2929
printer.begin(i)

examples/forecast.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22

33
# Weather forecast for Raspberry Pi w/Adafruit Mini Thermal Printer.
4-
# Retrieves data from Yahoo! weather, prints current conditions and
4+
# Retrieves data from DarkSky.net's API, prints current conditions and
55
# forecasts for next two days. See timetemp.py for a different
66
# weather example using nice bitmaps.
77
# Written by Adafruit Industries. MIT license.
@@ -14,51 +14,52 @@
1414
# http://www.adafruit.com/products/600 Printer starter pack
1515

1616
from __future__ import print_function
17-
import urllib, time
1817
from Adafruit_Thermal import *
19-
from xml.dom.minidom import parseString
18+
from datetime import date
19+
from datetime import datetime
20+
import calendar
21+
import urllib, json
2022

21-
# WOEID indicates the geographic location for the forecast. It is
22-
# not a ZIP code or other common indicator. Instead, it can be found
23-
# by 'manually' visiting http://weather.yahoo.com, entering a location
24-
# and requesting a forecast, then copy the number from the end of the
25-
# current URL string and paste it here.
26-
WOEID = '2459115'
23+
API_KEY = "YOUR_API_KEY"
24+
25+
LAT = "40.726019"
26+
LONG = "-74.00536"
2727

2828
# Dumps one forecast line to the printer
2929
def forecast(idx):
30-
tag = 'yweather:forecast'
31-
day = dom.getElementsByTagName(tag)[idx].getAttribute('day')
32-
lo = dom.getElementsByTagName(tag)[idx].getAttribute('low')
33-
hi = dom.getElementsByTagName(tag)[idx].getAttribute('high')
34-
cond = dom.getElementsByTagName(tag)[idx].getAttribute('text')
35-
printer.print(day + ': low ' + lo )
36-
printer.print(deg)
37-
printer.print(' high ' + hi)
38-
printer.print(deg)
39-
printer.println(' ' + cond)
4030

41-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
31+
date = datetime.fromtimestamp(int(data['daily']['data'][idx]['time']))
32+
33+
day = calendar.day_name[date.weekday()]
34+
lo = data['daily']['data'][idx]['temperatureMin']
35+
hi = data['daily']['data'][idx]['temperatureMax']
36+
cond = data['daily']['data'][idx]['summary']
37+
printer.print(day + ': low ' + str(lo) )
38+
printer.print(deg)
39+
printer.print(' high ' + str(hi))
40+
printer.print(deg)
41+
printer.println(' ' + cond.replace(u'\u2013', '-').encode('utf-8')) # take care of pesky unicode dash
42+
43+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
4244
deg = chr(0xf8) # Degree symbol on thermal printer
4345

44-
# Fetch forecast data from Yahoo!, parse resulting XML
45-
dom = parseString(urllib.urlopen(
46-
'http://weather.yahooapis.com/forecastrss?w=' + WOEID).read())
46+
url = "https://api.darksky.net/forecast/"+API_KEY+"/"+LAT+","+LONG+"?exclude=[alerts,minutely,hourly,flags]&units=us"
47+
response = urllib.urlopen(url)
48+
data = json.loads(response.read())
4749

4850
# Print heading
4951
printer.inverseOn()
50-
printer.print('{:^32}'.format(
51-
dom.getElementsByTagName('description')[0].firstChild.data))
52+
printer.print('{:^32}'.format("DarkSky.Net Forecast"))
5253
printer.inverseOff()
5354

5455
# Print current conditions
5556
printer.boldOn()
5657
printer.print('{:^32}'.format('Current conditions:'))
5758
printer.boldOff()
58-
printer.print('{:^32}'.format(
59-
dom.getElementsByTagName('pubDate')[0].firstChild.data))
60-
temp = dom.getElementsByTagName('yweather:condition')[0].getAttribute('temp')
61-
cond = dom.getElementsByTagName('yweather:condition')[0].getAttribute('text')
59+
60+
61+
temp = data['currently']['temperature']
62+
cond = data['currently']['summary']
6263
printer.print(temp)
6364
printer.print(deg)
6465
printer.println(' ' + cond)

examples/iot_printer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from __future__ import print_function
1818
import RPi.GPIO as GPIO
19-
import subprocess, time, Image, socket
19+
import subprocess, time, socket
20+
from PIL import Image
2021
from Adafruit_Thermal import *
2122

2223
ledPin = 18
@@ -26,7 +27,7 @@
2627
nextInterval = 0.0 # Time of next recurring operation
2728
dailyFlag = False # Set after daily trigger occurs
2829
lastId = '1' # State information passed to/from interval script
29-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
30+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
3031

3132

3233
# Called when button is briefly tapped. Invokes time/temperature script.

examples/printertest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from Adafruit_Thermal import *
44

5-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
5+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
66

77
# Test inverse on & off
88
printer.inverseOn()
@@ -61,7 +61,7 @@
6161
import gfx.adaqrcode as adaqrcode
6262
printer.printBitmap(adaqrcode.width, adaqrcode.height, adaqrcode.data)
6363
printer.println("Adafruit!")
64-
printer.feed(1)
64+
printer.feed(2)
6565

6666
printer.sleep() # Tell printer to sleep
6767
printer.wake() # Call wake() before printing again, even if reset

examples/sudoku-gfx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
from __future__ import print_function
2424
import sys, os, random, getopt, re
2525
from Adafruit_Thermal import *
26-
import Image
26+
from PIL import Image
2727

28-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
28+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
2929
bg = Image.new("1", [384, 426], "white") # Working 'background' image
3030
img = Image.open('gfx/sudoku.png') # Source bitmaps
3131
xcoord = [ 15, 55, 95, 139, 179, 219, 263, 303, 343 ]

examples/sudoku-txt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import sys, os, random, getopt, re
2323
from Adafruit_Thermal import *
2424

25-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
25+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
2626

2727
def main():
2828
printer.setLineHeight(24) # So graphical chars fit together

examples/timetemp.py

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22

33
# Current time and temperature display for Raspberry Pi w/Adafruit Mini
4-
# Thermal Printer. Retrieves data from Yahoo! weather, prints current
4+
# Thermal Printer. Retrieves data from DarkSky.net's API, prints current
55
# conditions and time using large, friendly graphics.
66
# See forecast.py for a different weather example that's all text-based.
77
# Written by Adafruit Industries. MIT license.
@@ -15,31 +15,32 @@
1515

1616
from __future__ import print_function
1717
from Adafruit_Thermal import *
18-
from xml.dom.minidom import parseString
19-
import Image, ImageDraw, time, urllib
18+
import time, urllib, json
19+
from PIL import Image, ImageDraw
2020

21-
# WOEID indicates the geographic location for the forecast. It is
22-
# not a ZIP code or other common indicator. Instead, it can be found
23-
# by 'manually' visiting http://weather.yahoo.com, entering a location
24-
# and requesting a forecast, then copy the number from the end of the
25-
# current URL string and paste it here.
26-
WOEID = '2459115'
21+
API_KEY = "YOUR_API_KEY"
2722

28-
# Fetch weather data from Yahoo!, parse resulting XML
29-
dom = parseString(urllib.urlopen(
30-
'http://weather.yahooapis.com/forecastrss?w=' + WOEID).read())
23+
LAT = "40.726019"
24+
LONG = "-74.00536"
25+
26+
# Fetch weather data from DarkSky, parse resulting JSON
27+
url = "https://api.darksky.net/forecast/"+API_KEY+"/"+LAT+","+LONG+"?exclude=[alerts,minutely,hourly,flags]&units=us"
28+
response = urllib.urlopen(url)
29+
data = json.loads(response.read())
3130

3231
# Extract values relating to current temperature, humidity, wind
33-
temperature = int(dom.getElementsByTagName(
34-
'yweather:condition')[0].getAttribute('temp'))
35-
humidity = int(dom.getElementsByTagName(
36-
'yweather:atmosphere')[0].getAttribute('humidity'))
37-
windSpeed = int(dom.getElementsByTagName(
38-
'yweather:wind')[0].getAttribute('speed'))
39-
windDir = int(dom.getElementsByTagName(
40-
'yweather:wind')[0].getAttribute('direction'))
41-
windUnits = dom.getElementsByTagName(
42-
'yweather:units')[0].getAttribute('speed')
32+
33+
temperature = int(data['currently']['temperature'])
34+
humidity = int(data['currently']['humidity'] * 100);
35+
windSpeed = int(data['currently']['windSpeed'])
36+
windDir = data['currently']['windBearing']
37+
windUnits = "mph"
38+
39+
# print(temperature)
40+
# print(humidity)
41+
# print(windSpeed)
42+
# print(windDir)
43+
# print(windUnits)
4344

4445
# Although the Python Imaging Library does have nice font support,
4546
# I opted here to use a raster bitmap for all of the glyphs instead.
@@ -62,11 +63,11 @@
6263

6364
# Generate a list of sub-image glyphs cropped from the symbols image
6465
def croplist(widths, x, y, height):
65-
list = []
66-
for i in range(len(widths)):
67-
list.append(symbols.crop(
68-
[x, y+i*height, x+widths[i], y+(i+1)*height]))
69-
return list
66+
list = []
67+
for i in range(len(widths)):
68+
list.append(symbols.crop(
69+
[x, y+i*height, x+widths[i], y+(i+1)*height]))
70+
return list
7071

7172
# Crop glyph lists (digits, days of week, etc.)
7273
TimeDigit = croplist(TimeDigitWidth, 0, 0, 44)
@@ -91,20 +92,20 @@ def croplist(widths, x, y, height):
9192

9293
# Paste a series of glyphs (mostly numbers) from string to img
9394
def drawNums(str, x, y, list):
94-
for i in range(len(str)):
95-
d = ord(str[i]) - ord('0')
96-
img.paste(list[d], (x, y))
97-
x += list[d].size[0] + 1
98-
return x
95+
for i in range(len(str)):
96+
d = ord(str[i]) - ord('0')
97+
img.paste(list[d], (x, y))
98+
x += list[d].size[0] + 1
99+
return x
99100

100101
# Determine total width of a series of glyphs in string
101102
def numWidth(str, list):
102-
w = 0 # Cumulative width
103-
for i in range(len(str)):
104-
d = ord(str[i]) - ord('0')
105-
if i > 0: w += 1 # Space between digits
106-
w += list[d].size[0] # Digit width
107-
return w
103+
w = 0 # Cumulative width
104+
for i in range(len(str)):
105+
d = ord(str[i]) - ord('0')
106+
if i > 0: w += 1 # Space between digits
107+
w += list[d].size[0] # Digit width
108+
return w
108109

109110
# Render current time (always 24 hour XX:XX format)
110111
t = time.localtime()
@@ -134,12 +135,13 @@ def numWidth(str, list):
134135
s2 = str(windSpeed)
135136
winDirNum = 0 # Wind direction glyph number
136137
if windSpeed > 0:
137-
for winDirNum in range(len(DirAngle) - 1):
138-
if windDir < DirAngle[winDirNum]: break
138+
for winDirNum in range(len(DirAngle) - 1):
139+
if windDir < DirAngle[winDirNum]: break
140+
winDirNum+=1
139141
w = Humidity.size[0] + 5 + numWidth(s, HumiDigit)
140142
w2 = Wind.size[0] + 5 + numWidth(s2, HumiDigit)
141143
if windSpeed > 0:
142-
w2 += 3 + Dir[winDirNum].size[0]
144+
w2 += 3 + Dir[winDirNum].size[0]
143145
if windUnits == 'kph': w2 += 3 + Kph.size[0]
144146
else: w2 += 3 + Mph.size[0]
145147
if w2 > w: w = w2
@@ -154,14 +156,15 @@ def numWidth(str, list):
154156
y += 23 # And advance to next line
155157
img.paste(Wind, (x, y))
156158
x += Wind.size[0] + 5
159+
157160
if windSpeed > 0:
158-
img.paste(Dir[winDirNum], (x, y))
159-
x += Dir[winDirNum].size[0] + 3
161+
img.paste(Dir[winDirNum], (x, y))
162+
x += Dir[winDirNum].size[0] + 3
160163
x = drawNums(s2, x, y, HumiDigit) + 3
161164
if windUnits == 'kph': img.paste(Kph, (x, y))
162165
else: img.paste(Mph, (x, y))
163166

164167
# Open connection to printer and print image
165-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
168+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
166169
printer.printImage(img, True)
167170
printer.feed(3)

examples/twitter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
# Other globals. You probably won't need to change these. -----------------
4949

50-
printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
50+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
5151
host = 'api.twitter.com'
5252
authUrl = '/oauth2/token'
5353
searchUrl = '/1.1/search/tweets.json?'

0 commit comments

Comments
 (0)