Skip to content

Commit

Permalink
Merge pull request #79 from tvoverbeek/button-example+root-check
Browse files Browse the repository at this point in the history
Button example+root check
  • Loading branch information
francesco-vannini authored Mar 15, 2017
2 parents 8800978 + ac22ebc commit e747bca
Show file tree
Hide file tree
Showing 13 changed files with 241 additions and 174 deletions.
Binary file added bin/.papirus-twitter.py.swp
Binary file not shown.
66 changes: 54 additions & 12 deletions bin/papirus-buttons
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,84 @@

import os
import sys
import string
from papirus import Papirus
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from time import sleep
import RPi.GPIO as GPIO


user = os.getuid()
if user != 0:
print "Please run script as root"
sys.exit()
# Running as root only needed for older Raspbians without /dev/gpiomem
if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | os.W_OK)):
user = os.getuid()
if user != 0:
print("Please run script as root")
sys.exit()

# Command line usage
# papirus-buttons

hatdir = '/proc/device-tree/hat'

WHITE = 1
BLACK = 0

SIZE = 27

SW1 = 16
SW2 = 26
SW3 = 20
SW4 = 21
# Assume Papirus Zero
SW1 = 21
SW2 = 16
SW3 = 20
SW4 = 19
SW5 = 26

# Check for HAT, and if detected redefine SW1 .. SW5
if (os.path.exists(hatdir + '/product')) and (os.path.exists(hatdir + '/vendor')) :
f = open(hatdir + '/product')
prod = f.read()
f.close()
f = open(hatdir + '/vendor')
vend = f.read()
f.close
if (string.find(prod, 'PaPiRus ePaper HAT') == 0) and (string.find(vend, 'Pi Supply') == 0) :
# Papirus HAT detected
SW1 = 16
SW2 = 26
SW3 = 20
SW4 = 21
SW5 = -1

def main():
global SIZE

GPIO.setmode(GPIO.BCM)

GPIO.setup(SW1, GPIO.IN)
GPIO.setup(SW2, GPIO.IN)
GPIO.setup(SW3, GPIO.IN)
GPIO.setup(SW4, GPIO.IN)
if SW5 != -1:
GPIO.setup(SW5, GPIO.IN)

papirus = Papirus()

write_text(papirus, "Ready...", SIZE)
# Use smaller font for smaller dislays
if papirus.height <= 96:
SIZE = 18

papirus.clear()

write_text(papirus, "Ready... SW1 + SW2 to exit.", SIZE)

while True:
# Exit when SW1 and SW2 are pressed simultaneously
if (GPIO.input(SW1) == False) and (GPIO.input(SW2) == False) :
write_text(papirus, "Exiting ...", SIZE)
sleep(0.2)
papirus.clear()
sys.exit()

if GPIO.input(SW1) == False:
write_text(papirus, "One", SIZE)

Expand All @@ -53,6 +92,9 @@ def main():
if GPIO.input(SW4) == False:
write_text(papirus, "Four", SIZE)

if (SW5 != -1) and (GPIO.input(SW5) == False):
write_text(papirus, "Five", SIZE)

sleep(0.1)

def write_text(papirus, text, size):
Expand All @@ -63,7 +105,7 @@ def write_text(papirus, text, size):
# prepare for drawing
draw = ImageDraw.Draw(image)

font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMono.ttf', size)
font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', size)

# Calculate the max number of char to fit on line
line_size = (papirus.width / (size*0.65))
Expand All @@ -88,7 +130,7 @@ def write_text(papirus, text, size):
draw.text( (0, ((size*current_line)-size)) , l, font=font, fill=BLACK)

papirus.display(image)
papirus.update()
papirus.partial_update()

if __name__ == '__main__':
main()
12 changes: 7 additions & 5 deletions bin/papirus-clear
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import os
import sys
from papirus import Papirus

user = os.getuid()
if user != 0:
print "Please run script as root"
sys.exit()

# Running as root only needed for older Raspbians without /dev/gpiomem
if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | os.W_OK)):
user = os.getuid()
if user != 0:
print("Please run script as root")
sys.exit()

def main():
papirus = Papirus()
print("Clearing Papirus.......")
Expand Down
16 changes: 9 additions & 7 deletions bin/papirus-clock
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ from datetime import datetime
import time
from papirus import Papirus

user = os.getuid()
if user != 0:
print ("Please run script as root")
sys.exit()

# Running as root only needed for older Raspbians without /dev/gpiomem
if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | os.W_OK)):
user = os.getuid()
if user != 0:
print("Please run script as root")
sys.exit()

WHITE = 1
BLACK = 0

Expand Down Expand Up @@ -91,9 +93,9 @@ def demo(papirus):
while True:
while True:
now = datetime.today()
if now.second % 5 == 0:
if now.second != previous_second:
break
time.sleep(0.5)
time.sleep(0.1)

if now.day != previous_day:
draw.rectangle((2, 2, width - 2, height - 2), fill=WHITE, outline=BLACK)
Expand Down
Empty file modified bin/papirus-config
100644 → 100755
Empty file.
7 changes: 7 additions & 0 deletions bin/papirus-draw
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import argparse
# Command line usage
# papirus-draw "filepath" -t "crop|resize"

# Running as root only needed for older Raspbians without /dev/gpiomem
if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | os.W_OK)):
user = os.getuid()
if user != 0:
print("Please run script as root")
sys.exit()

def main():
papirus = Papirus()
p = argparse.ArgumentParser()
Expand Down
11 changes: 6 additions & 5 deletions bin/papirus-gol
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ BLACK = 0
CELLSIZE = 5


user = os.getuid()
if user != 0:
print "Please run script as root"
sys.exit()

# Running as root only needed for older Raspbians without /dev/gpiomem
if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | os.W_OK)):
user = os.getuid()
if user != 0:
print("Please run script as root")
sys.exit()

#Colours the cells green for life and white for no life
def colourGrid(draw, item, lifeDict):
Expand Down
Empty file modified bin/papirus-set
100644 → 100755
Empty file.
Empty file modified bin/papirus-setup
100644 → 100755
Empty file.
Empty file modified bin/papirus-temp
100644 → 100755
Empty file.
152 changes: 152 additions & 0 deletions bin/papirus-twitter
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# !/usr/bin/env python
# coding: utf-8
# ------------------------------------------------------
# Filename papirus-twitter
# ------------------------------------------------------
# Displays Tweets on PaPiRus Zero or PaPiRus HAT display
#
# v1.0 by James West June 2016
#
# I used the Twython tutorials at tecoed.co.uk
# to get me started
#
# v1.1 added HAT support and some cleanup
# by Ton van Overbeek March 2017
# ------------------------------------------------------

# import libraries to make it work
import os
import sys
import string
import re
import time
import RPi.GPIO as GPIO
from twython import Twython
from papirus import Papirus
from papirus import PapirusText

# Running as root only needed for older Raspbians without /dev/gpiomem
if not (os.path.exists('/dev/gpiomem') and os.access('/dev/gpiomem', os.R_OK | os.W_OK)):
user = os.getuid()
if user != 0:
print("Please run script as root")
sys.exit()

hatdir = '/proc/device-tree/hat'

# set up PaPiRus
screen = Papirus()
text = PapirusText()

# Twitter authorisation keys
CONSUMER_KEY = 'YOUR CONSUMER KEY HERE'
CONSUMER_SECRET = 'YOUR CONSUMER SECRET HERE'
ACCESS_KEY = 'YOUR ACCESS KEY HERE'
ACCESS_SECRET = 'YOUR ACCESS SECRET HERE'

api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET)

# Identifies the GPIOs that the buttons operate on
# Assume Papirus Zero
SW1 = 21
SW2 = 16
SW3 = 20
SW4 = 19
SW5 = 26
off_text = '5 = Off'

# Check for HAT, and if detected redefine SW1 .. SW5
if (os.path.exists(hatdir + '/product')) and (os.path.exists(hatdir + '/vendor')) :
f = open(hatdir + '/product')
prod = f.read()
f.close()
f = open(hatdir + '/vendor')
vend = f.read()
f.close
if (string.find(prod, 'PaPiRus ePaper HAT') == 0) and (string.find(vend, 'Pi Supply') == 0) :
# Papirus HAT detected
SW1 = 16
SW2 = 26
SW3 = 20
SW4 = 21
SW5 = -1
off_text = '1+2 = Off'

def display_tweets(tweets):
for tweet in tweets:
clean_tweet = '%s: %s' % (tweet['user']['screen_name'].encode('utf-8'),
tweet['text'].encode('utf-8'))

# These lines clear URLs from the tweets and tidies up other elements
# the display doesn't seem to like
clean_tweet = re.sub(r"(https?\://|http?\://|https?\:)\S+", "", clean_tweet)
clean_tweet = re.sub(r"&amp;", "&", clean_tweet)
clean_tweet = re.sub(r"&horbar|&hyphen|&mdash|&ndash", "-", clean_tweet)
clean_tweet = re.sub(r"&apos|&rsquo|&rsquor|&prime", "'", clean_tweet)
clean_tweet = re.sub(r"£", "", clean_tweet)
text.write(clean_tweet, 14)

# Sets how many seconds each tweet is on screen for
time.sleep(5)

def main():
GPIO.setmode(GPIO.BCM)

GPIO.setup(SW1, GPIO.IN)
GPIO.setup(SW2, GPIO.IN)
GPIO.setup(SW3, GPIO.IN)
GPIO.setup(SW4, GPIO.IN)
if SW5 != -1:
GPIO.setup(SW5, GPIO.IN)

# Writes the menu to the PaPiRus - 14 is the font size
text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n' + off_text, 14)
while True:
# Test for only SW1 to allow for both SW1 and SW2 to select 'Off' on HAT
if GPIO.input(SW1) == False and GPIO.input(SW2) == True:

# Put the Twitter usernames of the news organisations you want to read here
twits = ["BBCNews", "GranadaReports", "SkyNews", "itvnews", "MENnewsdesk"]
for index in range (len(twits)):
# The count parameter defines how many tweets from each account you'll read
tweets = api.get_user_timeline(screen_name=twits[index], count=4)
display_tweets(tweets)

text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n' + off_text, 14)

# Test for only SW2 to allow for both SW1 and SW2 to select 'Off' on HAT
if GPIO.input(SW2) == False and GPIO.input(SW1) == True:
# usernames of the weather accounts you'll be using
twitweather = ["mcrweather", "Manches_Weather"]
for index in range (len(twitweather)):
tweets = api.get_user_timeline(screen_name=twitweather[index], count=1)
display_tweets(tweets)

text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n' + off_text, 14)

if GPIO.input(SW3) == False:
# Gets your home timeline
tweets = api.get_home_timeline(count=20)
display_tweets(tweets)

text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n' + off_text, 14)

if GPIO.input(SW4) == False:
# gets your mentions
tweets = api.get_mentions_timeline(count=5)
display_tweets(tweets)

text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n' + off_text, 14)

if (((SW5 != -1) and (GPIO.input(SW5) == False)) or
((SW5 == -1) and (GPIO.input(SW1) == False) and (GPIO.input(SW2) == False))):
# Says goodbye, clears the screen and shuts the Pi down
text.write('Goodbye..., Shutting down ...')
text.write(' ')
os.system("sudo shutdown -h now")

# Small delay to allow for SW1 + SW2 detection on HAT
time.sleep(0.1)

if __name__ == '__main__':
main()
Loading

0 comments on commit e747bca

Please sign in to comment.