Skip to content

Instantly share code, notes, and snippets.

View davstott's full-sized avatar

Dav Stott davstott

View GitHub Profile
@davstott
davstott / fractals.py
Last active October 1, 2017 11:01
noodling with fractals in python
import matplotlib.pyplot as plt #needs a better plotter
import numpy as np
class doesIt:
ITERATIONS = 30
DIVERGETHRESHOLD = 4.0
DEBUG = False
def iterate(self, x, c):
for i in range(self.ITERATIONS):
@davstott
davstott / blit.py
Last active December 20, 2015 08:09
Drawing images inside Minecraft PI Edition
mc.postToChat("Starting drawing")
#draw blocks at this height
z = 10
#if we assume the most common colour will be white, we can pre-paint the backgrund to save many setBlock()s
#35, 0 is tileID for wool, tileData for white
mc.setBlocks(0,z,0,WIDTH,z,HEIGHT,35,0)
for x in range(WIDTH):
for y in range(HEIGHT):
# fetch the pixel value at the current position, the units are the indexes into our palette
@davstott
davstott / doStuff.py
Last active February 26, 2016 10:27
Raspberry Pi, GPIO over the Network
#!/usr/bin/pythonRoot
# bring in the libraries
import RPi.GPIO as G
from flup.server.fcgi import WSGIServer
import sys, urlparse
# set up our GPIO pins
G.setmode(G.BCM)
G.setup(18, G.OUT)
@davstott
davstott / PWMsnippet.py
Last active December 14, 2015 12:29
A snippet illustrating 20% and 30% PWM as well as the hardware emergency stop switch
def decide(sensors, cycle, lastCommand):
if sensors.userCommand == Commands.NONE: #no changes to input
sensors.userCommand = lastCommand
if sensors.frontBumper: # hardware stop triggered so stop regardless of user's opinion
newState = State(Commands.STOP)
elif sensors.userCommand == Commands.FORWARD_slow:
if cycle in [3,8]: # slow is 20% duty cycle
newState = State(Commands.FORWARD_full)
else:
newState = State(Commands.STOP)
@davstott
davstott / charModeStdin.py
Created March 5, 2013 00:28
Linux python character mode stdin instead of line mode
import sys, fcntl, os, termios, tty
def init():
#take the current settings
fnctlSettings = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
termiosSettings = termios.tcgetattr(sys.stdin.fileno())
# set the new ones
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, os.O_NONBLOCK | fnctlSettings)
tty.setraw(sys.stdin.fileno())
@davstott
davstott / asyncInput.py
Created March 5, 2013 00:23
Linux python asynchronous input from stdin using select()
#Replacing
thisChar = sys.stdin.read(1)
#With
import select
try:
if select.select([sys.stdin,], [], [], 0)[0]:
thisChar = sys.stdin.read(1)
else:
if (debug):
print "nothing to read"
@davstott
davstott / controlLoop.py
Last active December 14, 2015 12:29
Basic control loop
init()
#target number of updates per second. hopefully The Loop completes in less time than that.
targetSpeed = 10
# for each trip around This Seems Obvious to Dav AI Loop
# sleep for a bit / find out if it's time to do anything yet.
# read next input from sensors
# make any decisions
# set any output