diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb8284a --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# PyCharm +.idea/ +*.iml + +# Config files not suitable for Github +options.cfg + +# Object files +*.pyc \ No newline at end of file diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index f5e5162..2b353db 100644 --- a/Adafruit_Thermal.py +++ b/Adafruit_Thermal.py @@ -36,6 +36,7 @@ from __future__ import print_function from serial import Serial import time +import ConfigParser class Adafruit_Thermal(Serial): @@ -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. @@ -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 @@ -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) diff --git a/forecast.py b/forecast.py index d985497..5b41d41 100755 --- a/forecast.py +++ b/forecast.py @@ -15,6 +15,7 @@ from __future__ import print_function import urllib, time +import ConfigParser from Adafruit_Thermal import * from xml.dom.minidom import parseString @@ -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): @@ -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() diff --git a/options.cfg.template b/options.cfg.template new file mode 100644 index 0000000..1db1c9f --- /dev/null +++ b/options.cfg.template @@ -0,0 +1,18 @@ +[forecast] +woeid= + +[twitter] +consumer-key= +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 + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0f9d3d9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +PIL==1.1.7 +pyserial==2.7 +Unidecode==0.4.17 diff --git a/twitter.py b/twitter.py index 662b1fd..ef36580 100755 --- a/twitter.py +++ b/twitter.py @@ -28,6 +28,7 @@ from __future__ import print_function import base64, HTMLParser, httplib, json, sys, urllib, zlib +import ConfigParser from unidecode import unidecode from Adafruit_Thermal import * @@ -35,14 +36,18 @@ # 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. -----------------