Skip to content
This repository was archived by the owner on May 16, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# PyCharm
.idea/
*.iml

# Config files not suitable for Github
options.cfg

# Object files
*.pyc
33 changes: 28 additions & 5 deletions Adafruit_Thermal.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from __future__ import print_function
from serial import Serial
import time
import ConfigParser

class Adafruit_Thermal(Serial):

Expand All @@ -51,8 +52,27 @@ class Adafruit_Thermal(Serial):
barcodeHeight = 50
printMode = 0
defaultHeatTime = 60
defaultHeatDots = 20
defaultHeatInterval = 250

def __init__(self, *args, **kwargs):
# Attempt to read printer options from config
heatTime = self.defaultHeatTime
heatDots = self.defaultHeatDots
heatInterval = self.defaultHeatInterval
try:
config = ConfigParser.SafeConfigParser({
'heat-time': str(heatTime),
'heat-dots': str(heatDots),
'heat-interval': str(heatInterval)
})
config.read('options.cfg')
heatTime = int(config.get('printer', 'heat-time'))
heatDots = int(config.get('printer', 'heat-dots'))
heatInterval = int(config.get('printer', 'heat-interval'))
except:
pass

# If no parameters given, use default port & baud rate.
# If only port is passed, use default baud rate.
# If both passed, use those values.
Expand Down Expand Up @@ -97,13 +117,15 @@ def __init__(self, *args, **kwargs):
# blank page may occur. The more heating interval, the more
# clear, but the slower printing speed.

heatTime = kwargs.get('heattime', self.defaultHeatTime)
# heattime argument overrides config
heatTime = kwargs.get('heattime', heatTime)

self.writeBytes(
27, # Esc
55, # 7 (print settings)
20, # Heat dots (20 = balance darkness w/no jams)
heatDots, # Heat dots (20 = balance darkness w/no jams)
heatTime, # Lib default = 45
250) # Heat interval (500 uS = slower but darker)
heatInterval) # Heat interval (500 uS = slower but darker)

# Description of print density from page 23 of the manual:
# DC2 # n Set printing density
Expand Down Expand Up @@ -317,12 +339,13 @@ def writePrintMode(self):
def normal(self):
self.printMode = 0
self.writePrintMode()
self.inverseOff()

def inverseOn(self):
self.setPrintMode(self.INVERSE_MASK)
self.writeBytes(29, 66, 1)

def inverseOff(self):
self.unsetPrintMode(self.INVERSE_MASK)
self.writeBytes(29, 66, 0)

def upsideDownOn(self):
self.setPrintMode(self.UPDOWN_MASK)
Expand Down
9 changes: 7 additions & 2 deletions forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from __future__ import print_function
import urllib, time
import ConfigParser
from Adafruit_Thermal import *
from xml.dom.minidom import parseString

Expand All @@ -23,7 +24,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'
# (note that this value has moved to the config file)

# Dumps one forecast line to the printer
def forecast(idx):
Expand All @@ -38,12 +39,16 @@ def forecast(idx):
printer.print(deg)
printer.println(' ' + cond)

config = ConfigParser.SafeConfigParser({'woeid': '2459115'}) # Default to NYC
config.read('options.cfg')
woeid = config.get('forecast', 'woeid')

printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5)
deg = chr(0xf8) # Degree symbol on thermal printer

# Fetch forecast data from Yahoo!, parse resulting XML
dom = parseString(urllib.urlopen(
'http://weather.yahooapis.com/forecastrss?w=' + WOEID).read())
'http://weather.yahooapis.com/forecastrss?w=' + woeid).read())

# Print heading
printer.inverseOn()
Expand Down
18 changes: 18 additions & 0 deletions options.cfg.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[forecast]
woeid=<your local woeid>

[twitter]
consumer-key=<your twitter consumer key>
consumer-secret=<your twitter consumer secret>
query-string=from:Adafruit

[printer]
# Defaults from Adafruit
#heat-time=60
#heat-dots=20
#heat-interval=250
# Works better for v2.68 printer
heat-time=80
heat-dots=7
heat-interval=2

3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
PIL==1.1.7
pyserial==2.7
Unidecode==0.4.17
11 changes: 8 additions & 3 deletions twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,26 @@

from __future__ import print_function
import base64, HTMLParser, httplib, json, sys, urllib, zlib
import ConfigParser
from unidecode import unidecode
from Adafruit_Thermal import *


# 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'
# These have been moved to the config file.
config = ConfigParser.SafeConfigParser({'query-string': 'from:Adafruit'})
config.read('options.cfg')
consumer_key = config.get('twitter', 'consumer-key')
consumer_secret = config.get('twitter', 'consumer-secret')
queryString = config.get('twitter', 'query-string')

# 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'
# This has been moved to the config file.


# Other globals. You probably won't need to change these. -----------------
Expand Down