Skip to content

Instantly share code, notes, and snippets.

View martinohanlon's full-sized avatar

Martin O'Hanlon martinohanlon

View GitHub Profile
@martinohanlon
martinohanlon / holdbutton.py
Last active August 1, 2016 13:25
Creating a 'holdable button' with gpiozero
from gpiozero import Button
from threading import Timer
class HoldableButton(Button):
def __init__(self, pin=None, pull_up=True, bounce_time=None, hold_time=1, repeat=False):
super(HoldableButton, self).__init__(pin, pull_up, bounce_time)
# Set Button when_pressed and when_released to call local functions
# cant use super() as it doesn't support setters
@martinohanlon
martinohanlon / sevensegdisplay.py
Last active August 12, 2017 21:05
4 digit 7 segment display
from gpiozero import LEDCollection, LEDBoard, OutputDeviceError, DigitalOutputDevice
from gpiozero.threads import GPIOThread
from gpiozero.exc import OutputDeviceError
from itertools import cycle
from time import sleep
class SevenSegmentDisplay(LEDBoard):
"""
Extends :class:`LEDBoard` for a 7 segment LED display
7 segment displays have either 7 or 8 pins, 7 pins for the digit display
@martinohanlon
martinohanlon / gist:0f7b82393a68482deda056af2672bb9e
Created October 27, 2017 14:24
Python function Threading examples
import threading
from time import sleep
def count(time_to_sleep):
while True:
i = 0
print(i)
i += 1
sleep(time_to_sleep)
@martinohanlon
martinohanlon / slack_streamer.py
Created November 15, 2017 17:14
Slack command line streamer
# A terminal slack streamer
# pre-requisites
# - colorama : pip install colorama
# - slackclient : pip install slackclient
# - a legacy slack api token : https://api.slack.com/custom-integrations/legacy-tokens
from slackclient import SlackClient
from colorama import init, Fore, Back
import time
import os
@martinohanlon
martinohanlon / sensehat_thermalcam.py
Created November 20, 2017 21:54
Python thermal camera example using Sense Hat and AMG8833
"""
An adaptation of Adafruit's thermal camera example which
uses the sense hat to display temperatures.
Martin O'Hanlon
@martinohanlon
stuffaboutco.de
"""
from sense_hat import SenseHat
from Adafruit_AMG88xx import Adafruit_AMG88xx
@martinohanlon
martinohanlon / bluedot_shutdown.py
Last active January 10, 2018 21:28
BlueDot shutdown on double press
from bluedot import BlueDot
from subprocess import call
# create a function which will be called when then bluedot is double pressed / swiped
def shutdown_pi():
call("sudo halt", shell=True)
# create the blue dot
bd = BlueDot()
# call the shutdown_pi function when its double pressed
@martinohanlon
martinohanlon / windows_add_python_to_path.py
Created February 27, 2018 16:20
This python script will find python directories and add them to the windows PATH
# this python script will find the directory python.exe and python/Scripts
# and add them to the windows path
import subprocess
import sys
import os
def add_to_PATH(paths):
# create the paths to add to the PATH
path_to_add = ""
@martinohanlon
martinohanlon / detect_motion.py
Last active June 28, 2022 08:29
picamera motion detection - super hacky!
from picamera import PiCamera, PiCameraCircularIO, PiVideoFrameType
from picamera.array import PiRGBArray
from time import sleep
import io
import cv2
def diff_image(t0, t1, t2):
d1 = cv2.absdiff(t2, t1)
d2 = cv2.absdiff(t1, t0)
return cv2.bitwise_and(d1, d2)
@martinohanlon
martinohanlon / char_count.py
Created June 13, 2018 15:36
guizero character counting example
from guizero import App, TextBox, Text
def count():
result.value = len(text_to_count.value) - 1
app = App(title="character count")
instruction = Text(app, text="Put your text here")
text_to_count = TextBox(app, multiline = True, width=60, height=25, command=count)
text_to_count.when_key_released = count
result = Text(app, text="0")
@martinohanlon
martinohanlon / guizero_pushbutton_animation.py
Created June 15, 2018 15:26
A push button animation with guizero
# Install
# - sudo pip3 install guizero
# - sudo pip3 install imageio
# - Attached a picamera and a button to pin 17
# - Run it
from guizero import App, Picture, PushButton, Text
from picamera import PiCamera
from picamera.array import PiRGBArray
from gpiozero import Button