Skip to content

Instantly share code, notes, and snippets.

+-1
--1
-----1
+++++1
++++++++--------1
1*-+-+-+-+-+-+-+-+-+-+-+-1*-+1/+-+-+-+-+-1
@seth10
seth10 / randomWord.py
Last active May 13, 2017 21:20
Reads the standard system dictionary file and prints a random word
with open("/usr/share/dict/words") as f:
dict = [word.rstrip('\n') for word in f]
import random
print random.choice(dict)
# or, a one-liner
random.choice(open("/usr/share/dict/words").readlines())[:-1]
# in an isolated environment:
@seth10
seth10 / 00-Keyboard.py
Last active May 11, 2017 20:40
TouchScreenInput partial
from PiStorms import PiStorms
from TouchScreenInput import TouchScreenInput
from functools import partial
psm = PiStorms()
textbox = TouchScreenInput(psm.screen)
textbox.bind_led_on_func(partial(psm.led, 1, 255, 0, 0))
textbox.bind_led_off_func(partial(psm.led, 1, *[0]*3))
result = textbox.getInput()
psm.screen.showMessage(['Your answer', result['response']])
@seth10
seth10 / warnings.log
Created May 11, 2017 19:57
Warnings generated by Doxygen for undocumented members in PiStorms/sys
LegoDevices.py:298: Compound LegoDevices::EV3ColorSensor
swarmclient.py:60: Compound swarmclient::SwarmClient
LegoDevices.py:347: Compound LegoDevices::EV3InfraredSensor
LegoDevices.py:311: Compound LegoDevices::EV3GyroSensor
LegoDevices.py:257: Compound LegoDevices::NXTLightSensor
PiStormsCom.py:33: Compound PiStormsCom::PSSensor
MS_ILI9341.py:31: Compound MS_ILI9341::ILI9341
LegoDevices.py:199: Compound LegoDevices::EV3TouchSensor
scratch.py:9: Compound scratch::Scratch
LegoDevices.py:330: Compound LegoDevices::EV3UltrasonicSensor
@seth10
seth10 / 00-TextAlignTest.py
Created May 10, 2017 21:21
Testing drawAutoText and its parameter align
from PiStorms import PiStorms
import time
psm = PiStorms()
s = psm.screen
for _ in range(4):
for align in ["left", "center", "right"]:
s.clearScreen(display = False)
s.drawAutoText(align+", rotation: "+str(s.currentRotation), 10 if align!="center" else 9999, 20, align=align)
@seth10
seth10 / 05-Comprehensive-timeit.py
Created May 4, 2017 21:26
Measure how long each step of drawing the graph takes
while not stop:
start = time.time()
plt.cla() # clear axis, get rid of old lines
print "Clear:\t", time.time()-start
start = time.time()
slicedata = data[:,-1*DATA_SIZE:] # use only the last n data points, where n is DATA_SIZE
print "Slice:\t", time.time()-start
start = time.time()
# plot a spline (smooth line) for each axis
for i in range(3): plt.plot(smooth_x, spline(np.arange(len(slicedata[i])), slicedata[i], smooth_x))
@seth10
seth10 / find_factors.py
Created April 2, 2017 18:28
Just some fun finding factors
# this method will return a pair of prime numbers which, when multiplied, equal the argument
def find_factors(num):
if num == 0: return (0,0)
# if not prime ...
for i in [2]+range(3, num/2+2, 2):
if num % i == 0:
return (i, num/i)
return (1, num)
def sieve_primes(SIEVE_SIZE):
@seth10
seth10 / SystematicLove.cpp
Created March 29, 2017 23:32
Systematic Love
#include <iostream>
#include <math.h>
const int HEART_SIZE = 20;
const int HALF_SIZE = HEART_SIZE / 2;
bool is_in_love(int x, int y);
int main(void)
{
@seth10
seth10 / isPrime.playground
Created February 27, 2017 21:18
Created October 4, 2015 at 12:05 AM
//: Playground - noun: a place where people can play
var n = 61
var isPrime = true
//for var factor = 2; factor < n; factor++ {
for factor in stride(from: 2, to: n/2, by: 2) {
if n % factor == 0 {
isPrime = false
break
@seth10
seth10 / 00-TouchTest.py
Created February 23, 2017 13:55
A simple PiStorms program to print the touchscreen coordinates on screen
from PiStorms import PiStorms
psm = PiStorms()
psm.screen.drawDisplay('Raw touchscreen vals')
psm.screen.termPrintAt(8, 'Press GO to quit.')
while not psm.isKeyPressed():
psm.screen.termPrintAt(0, 'X: %d' % psm.screen.TS_X())
psm.screen.termPrintAt(1, 'Y: %d' % psm.screen.TS_Y())