From 6710912e47016b8fd07b56cd679e017390150984 Mon Sep 17 00:00:00 2001 From: Matt Kimmel Date: Fri, 15 May 2015 10:55:13 -0400 Subject: [PATCH 1/5] Move forecast woeid and twitter credentials to config file --- .gitignore | 9 +++++++++ forecast.py | 9 +++++++-- options.cfg.template | 6 ++++++ twitter.py | 8 ++++++-- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 options.cfg.template 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/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..5b7a084 --- /dev/null +++ b/options.cfg.template @@ -0,0 +1,6 @@ +[forecast] +woeid= + +[twitter] +consumer-key= +consumer-secret= diff --git a/twitter.py b/twitter.py index 662b1fd..d183c30 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,8 +36,11 @@ # 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() +config.read('options.cfg') +consumer_key = config.get('twitter', 'consumer-key') +consumer_secret = config.get('twitter', 'consumer-secret') # queryString can be any valid Twitter API search string, including # boolean operators. See http://dev.twitter.com/docs/using-search From 5c13e1f89db1fb26f48725bfe844aaa95615eab2 Mon Sep 17 00:00:00 2001 From: Matt Kimmel Date: Fri, 15 May 2015 16:19:16 -0400 Subject: [PATCH 2/5] Move twitter query string to config file --- options.cfg.template | 1 + twitter.py | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/options.cfg.template b/options.cfg.template index 5b7a084..333ad11 100644 --- a/options.cfg.template +++ b/options.cfg.template @@ -4,3 +4,4 @@ woeid= [twitter] consumer-key= consumer-secret= +query-string=from:Adafruit \ No newline at end of file diff --git a/twitter.py b/twitter.py index d183c30..ef36580 100755 --- a/twitter.py +++ b/twitter.py @@ -37,16 +37,17 @@ # Twitter application credentials -- see notes above -- DO NOT SHARE. # These have been moved to the config file. -config = ConfigParser.SafeConfigParser() +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. ----------------- From 6d67a87cf8f871b40317ff1702f71af477afe496 Mon Sep 17 00:00:00 2001 From: Matt Kimmel Date: Sun, 17 May 2015 13:25:52 -0400 Subject: [PATCH 3/5] Add initial requirements.txt --- requirements.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 requirements.txt 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 From 0199fdbed442c721d19d75add4de7624f3e7452a Mon Sep 17 00:00:00 2001 From: Matt Kimmel Date: Wed, 20 May 2015 19:17:56 -0400 Subject: [PATCH 4/5] Tweak driver for v2.68 printer --- Adafruit_Thermal.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index f5e5162..8305ee6 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 = 80 def __init__(self, *args, **kwargs): # If no parameters given, use default port & baud rate. @@ -101,9 +101,9 @@ def __init__(self, *args, **kwargs): self.writeBytes( 27, # Esc 55, # 7 (print settings) - 20, # Heat dots (20 = balance darkness w/no jams) + 7, # Heat dots (20 = balance darkness w/no jams) heatTime, # Lib default = 45 - 250) # Heat interval (500 uS = slower but darker) + 2) # Heat interval (500 uS = slower but darker) # Description of print density from page 23 of the manual: # DC2 # n Set printing density @@ -317,12 +317,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) From dd916ed5b0a4f6c38653cf9b473fad2b29aebbef Mon Sep 17 00:00:00 2001 From: Matt Kimmel Date: Fri, 22 May 2015 08:40:34 -0400 Subject: [PATCH 5/5] Move printer options to config file --- Adafruit_Thermal.py | 30 ++++++++++++++++++++++++++---- options.cfg.template | 13 ++++++++++++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/Adafruit_Thermal.py b/Adafruit_Thermal.py index 8305ee6..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): @@ -50,9 +51,28 @@ class Adafruit_Thermal(Serial): lineSpacing = 8 barcodeHeight = 50 printMode = 0 - defaultHeatTime = 80 + 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) - 7, # Heat dots (20 = balance darkness w/no jams) + heatDots, # Heat dots (20 = balance darkness w/no jams) heatTime, # Lib default = 45 - 2) # 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 diff --git a/options.cfg.template b/options.cfg.template index 333ad11..1db1c9f 100644 --- a/options.cfg.template +++ b/options.cfg.template @@ -4,4 +4,15 @@ woeid= [twitter] consumer-key= consumer-secret= -query-string=from:Adafruit \ No newline at end of file +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 +