def run(n): lcd = pyb.LCD('X') lcd.light(1) m = mpr121.MPR121(pyb.I2C(1, pyb.I2C.MASTER)) def blob(x, y, w, h, fill): for i in range(w): for j in range(h): if pyb.rng() & 0xff < fill: lcd.pixel(x + i, y + j, 1) for i in range(n): t = m.touch_status() lcd.fill(0) for y in range(32): lcd.pixel(64, y, 1) for x in range(128): lcd.pixel(x, 16, 1) if t & 1: blob(90, 20, 10, 10, 316 - m.elec_voltage(0)) if t & 2: blob(30, 20, 10, 10, 316 - m.elec_voltage(1)) if t & 4: blob(90, 5, 10, 10, 316 - m.elec_voltage(2)) if t & 8: blob(30, 5, 10, 10, 316 - m.elec_voltage(3)) lcd.show() pyb.delay(50)
def main(): (port, pid) = (None, None) midiCheckTime = time.monotonic() try: while True: midi_tasks(port) for i in range(len(cap_addrs)): if caps[i] is None: cap = mpr121.MPR121(cap_addrs[i][0]) try: cap.begin(cap_addrs[i][1]) caps[i] = cap except OSError: pass if port is None: checkInterval = MIDI_DISCONNECTED_CHECK_INTERVAL else: checkInterval = MIDI_CONNECTED_CHECK_INTERVAL now = time.monotonic() if (now - midiCheckTime >= checkInterval): (old_port, old_pid) = (port, pid) (port, pid) = replace_port(port, pid) if old_port is None and port is not None: print("Connected to PID", pid) for chan in range(len(channel_instrument_map)): midi_change_instrument(port, chan, channel_instrument_map[chan][0]) elif old_port is not None and port is None: print("Disconnected from PID", old_pid) midiCheckTime = now time.sleep(0.001) except KeyboardInterrupt: if port is not None: for chan in range(16): port.send_message([CONTROL_CHANGE + chan, ALL_NOTES_OFF, 0])
import pyb # Touch keypad import mpr121 keybd = mpr121.MPR121(pyb.I2C(1, pyb.I2C.MASTER)) keybd.debounce(3,3) for electr in range(4): keybd.threshold(electr, 50, 30) # LCD lcd = pyb.LCD('X') lcd.light(True) # Maths required for calculations from math import cos, sin, pi, sqrt, atan2, asin, acos d2r = pi/180 def circle_intersection(circle1, circle2): ''' @summary: calculates intersection points of two circles @param circle1: tuple(x,y,radius) @param circle2: tuple(x,y,radius) @result: tuple of intersection points (which are (x,y) tuple) ''' # return self.circle_intersection_sympy(circle1,circle2) x1,y1,r1 = circle1 x2,y2,r2 = circle2 # http://stackoverflow.com/a/3349134/798588 dx,dy = x2-x1,y2-y1 d = sqrt(dx*dx+dy*dy) if d > r1+r2:
""" To solve this puzzle, you must press and hold keys 3, 7 and 11. """ import mpr121 from machine import Pin i2c = machine.I2C(3) mpr = mpr121.MPR121(i2c) # the winning combination is 3, 7 and 11 combination = (1 << 3) | (1 << 7) | (1 << 11) # check all keys def check(pin): t = mpr.touched() print(t) if t & combination == combination: print("You found the winning combination!") d3 = Pin('D3', Pin.IN, Pin.PULL_UP) d3.irq(check, Pin.IRQ_FALLING)
import machine import servo import mpr121 import time i2c = machine.I2C(1, scl=machine.Pin(22), sda=machine.Pin(21), freq=400000) pca = servo.PCA9685(i2c) touch = mpr121.MPR121(i2c) s1 = servo.Servo(pca, 15) s2 = servo.Servo(pca, 14) s3 = servo.Servo(pca, 13) s4 = servo.Servo(pca, 12) while True: t1 = touch.is_touched(0) print(t1) if t1: s1.position(180) else: s1.position(0) t2 = touch.is_touched(11) if t2: s2.position(180) else: s2.position(0) t3 = touch.is_touched(2) if t3: s3.position(180)