Skip to content

Instantly share code, notes, and snippets.

@j0hnm4r5
Created April 30, 2014 02:03
Show Gist options
  • Save j0hnm4r5/dbf7d33eb674305abfaa to your computer and use it in GitHub Desktop.
Save j0hnm4r5/dbf7d33eb674305abfaa to your computer and use it in GitHub Desktop.
Working etch-a-sketch for the plotter, controlled by BrainJr.
import pygame
from pygame.locals import *
from pygame import midi
from colors import *
import chiplotle
import sys
import time
import random
import thread
import Queue
# Initialize Pygame --------------------
pygame.init()
windowX = 800
windowY = 800
window = pygame.display.set_mode((windowX, windowY))
pygame.display.set_caption('PenPlotter')
# Initialize Pygame Midi --------------------
midi.init()
count = midi.get_count()
# Prints all midi devices
for i in xrange(0, count):
print i, midi.get_device_info(x)
# Sets midi input to device 0 (should be 'BrainJr Controls')
midi_in = midi.Input(0)
# Initialize the Plotter --------------------
plotter = chiplotle.instantiate_plotters()[0]
# Pygame helper functions --------------------
def clear():
window.fill((255, 255, 255))
def input(events):
for event in events:
if event.type == QUIT:
pygame.quit()
sys.exit()
# Plotter functions --------------------
# Queue of plotter commands
q = Queue.Queue()
def send_to_plotter(m, n):
while True:
if q.qsize() > 0:
print "WRITING TO PLOTTER"
plotter.write(q.get())
# Start plotter thread ((None, None), is needed because args needs a tuple)
thread.start_new_thread(send_to_plotter, (None, None))
# Other helper functions --------------------
def scale(val, in_min, in_max, out_min, out_max):
in_span = in_max - in_min
out_span = out_max - out_min
unitized = float(val - in_min) / float(in_span)
return out_min + (unitized * out_span)
# Initialize variables--------------------
left_pot = 0
right_pot = 0
last_pt = (0, 0)
pointlist = ""
# Pick up pen
plotter.write('SP6;')
# Start main thread --------------------
clear()
while True:
# Read 4 blocks of midi data at a time
# Below, we'll read msg[0], essentially taking every fourth message. This helps reduce latency.
msg = midi_in.read(4)
if msg != []:
# Button
if msg[0][0][0] == 144:
# Left
if msg[0][0][1] == 1:
# Off
if msg[0][0][2] == 0:
pass
# On
if msg[0][0][2] == 64:
print "LEFT!"
clear()
# Right
if msg[0][0][1] == 16:
# Off
if msg[0][0][2] == 0:
pass
# On
if msg[0][0][2] == 64:
print "RIGHT!"
clear()
# Potentiometer
if msg[0][0][0] == 176:
# Left
if msg[0][0][1] == 1:
left_pot = msg[0][0][2]
# Right
if msg[0][0][1] == 16:
right_pot = 127 - msg[0][0][2]
# Changes deltaX by value of left pot
x += int(scale(left_pot, 0, 127, -5, 5))
print x
# x = int(scale(left_pot, 0, 127, 0, windowX))
y = int(scale(right_pot, 0, 127, 0, windowY))
# Add plotter command to string
pointlist += 'PD' + str(y * 20 - 8000) + ',' + str(x * 20 - 8000) + ';'
pygame.draw.line(window, BLACK, last_pt, (x, y), 2)
last_pt = (x, y)
# When the pointlist has more than n characters, put the string in the queue, then reset the string.
if len(pointlist) > 500:
print "sending points"
q.put_nowait(pointlist)
pointlist = ""
pygame.display.flip()
# clear()
input(pygame.event.get())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment