- Notifications
You must be signed in to change notification settings - Fork7
todbot/qtpy-tricks
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Some things to do on a stockAdafruit QT PyrunningCircuitPython 6.This list is mostly to help me remmeber how to do things in CircuitPython.Fore more generalCircuitPython tricks, check out myCircuitPython Tricks page.
These code snippets will also work on a Trinket M0 and really just aboutCircuitPython-compatible board, but you may need to adjust some of theboard
pins.
Notes:
- You will need to copy needed libraries from theCircuitPython libraries bundle to your CIRCUITPY drive
- When copying files to QT Py or Trinket M0, and you're on a Mac,be sure touse the
xattr
trick described here to save flash space - Or, just use
circup
to install libraries. It's very nice! (on Mac, dopip3 install circup
)
- Print "time" on OLED display
- Disco Party on built-in Neopixel
- Output Farty Noises to DAC
- Capsense Touch to Colors on Built-in LED
- Rotary Encoder to Built-in LED
- Fire Simulation on External Neopixel Strip
- Two servos with Python Class for Easing / Sequencing
- Spooky Eyes with Dual SSD1306 OLED displays
- Use Capsense as Proximity Detector to Make Spooopy Ghost
- Get Size of Device's Flash Disk
- Capsense Touch Sensor to USB keyboard
[Note: as of CircuitPython 7, this only works on QTPy RP2040, not QTPy M0 or QTPY M0 Haxpress]
importtimeimportboardimportadafruit_ssd1306# requires: adafruit_bus_device and adafruit_framebufi2c=board.I2C()oled=adafruit_ssd1306.SSD1306_I2C(width=128,height=32,i2c=i2c)whileTrue:oled.fill(0)oled.text("hello world",0,0,1)# requires 'font5x8.bin'oled.text("time:"+str(time.monotonic()),0,8,1)oled.show()time.sleep(1.0)
importtimeimportboardimportneopixelfromrandomimportrandintpixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=0.2)whileTrue:pixel.fill( (randint(0,255),randint(0,255),randint(0,255) ))time.sleep(0.1)
importtimeimportboardimportanalogioimportrandomdac=analogio.AnalogOut(board.A0)i=0di=40000lasttime=0whileTrue:dac.value=ii= (i+di)&0xffffiftime.monotonic()-lasttime>1:lasttime=time.monotonic()di=random.randrange(40000,80000)
importboardfromtouchioimportTouchInimportneopixelpixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=0.2)touchA=TouchIn(board.A1)touchB=TouchIn(board.A2)print("hello")whileTrue:pixel[0]= (int(touchA.value*255),0,int(touchB.value*255))
importboardimporttimeimportneopixelimportrotaryiopixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=1.0)encoder=rotaryio.IncrementalEncoder(board.MOSI,board.MISO )# any two pinswhileTrue:b= (encoder.position%32)*8print(encoder.position,b)pixel.fill((0,0,b))time.sleep(0.1)
Uses Python array operations and list comprehensions for conciseness.
importboardimporttimeimportneopixelfromrandomimportrandint# External Neopixel strip, can be on any pinleds=neopixel.NeoPixel(board.RX,8,brightness=0.2,auto_write=False)whileTrue:# reduce brightness of all pixels by (30,30,30)leds[:]= [[max(i-30,0)foriinl]forlinleds]# shift LED values up by one (0->1, 1->2, etc)leds[1:]=leds[0:-1]# '-1' means len-1 here# pick new random fire color for LED 0leds[0]= (randint(150,255),randint(50,100),0)leds.show()time.sleep(0.1)
If you want it "flipped", so the fire goes from the top LED down to LED 0:
importtime,board,neopixelfromrandomimportrandintleds=neopixel.NeoPixel(board.RX,8,brightness=0.2,auto_write=False)whileTrue:leds[:]= [[max(i-30,0)foriinl]forlinleds]# reduce brightness of all pixels by (30,30,30)leds[0:-1]=leds[1:]# shift LED values down by oneleds[-1]= (randint(150,255),randint(50,100),0)# pick new random fire color for LED Nleds.show()time.sleep(0.1)
importboardimporttimefrompulseioimportPWMOutfromadafruit_motorimportservofromrandomimportrandomservoA=servo.Servo(PWMOut(board.RX,duty_cycle=2**15,frequency=50))servoB=servo.Servo(PWMOut(board.TX,duty_cycle=2**15,frequency=50))classAngleSequence:def__init__(self,angles,speed):self.angles=anglesself.aindex=0self.angle=angles[0]self.speed=speeddefnext_angle(self):self.aindex= (self.aindex+1)%len(self.angles)defupdate(self):new_angle=self.angles[self.aindex]self.angle+= (new_angle-self.angle)*self.speed# simple easingreturnself.angleseqA=AngleSequence( [90,70,90,90,90,90,60,80,100],0.1)# set speed to tasteseqB=AngleSequence( [90,90,90,80,70,90,120],0.1)# set angles for desired animationlasttime=time.monotonic()whileTrue:iftime.monotonic()-lasttime> (0.2+random()*4.0 ):# creepy random timinglasttime=time.monotonic()seqA.next_angle()# go to next angle in listseqB.next_angle()servoA.angle=seqA.update()# get updated (eased) servo angleservoB.angle=seqB.update()time.sleep(0.02)# wait a bit, note this affects 'speed'
Displays are at same address, so code can be much simpler
[Note: as of CircuitPython 7, this only works on QTPy RP2040, not QTPy M0 or QTPY M0 Haxpress]
importtimeimportboardimportrandomimportadafruit_ssd1306# requires: adafruit_bus_device, adafruit_framebufi2c=board.I2C()oled=adafruit_ssd1306.SSD1306_I2C(width=128,height=64,i2c=i2c)i=0;inc=30whileTrue:oled.fill(0)fordin (31,30,14,12,10,8):# various diametersoled.circle(64+i,32,d,1)i=random.randint(-30,30)oled.show()time.sleep(0.1+random.random() )
Computing the difference between current touch raw value anda baseline minimumprovides a kind of proximity detector, if your antenna is big enough.
importtimeimportboardimportdigitalioimporttouchiofrompulseioimportPWMOutfromadafruit_motorimportservotouchA=touchio.TouchIn(board.A2)servoA=servo.Servo(PWMOut(board.RX,duty_cycle=2**15,frequency=50))touch_min=touchA.raw_value# baseline for proximityservo_pos_last=160whileTrue:# get proximity value, set within servo bounds (30-160)proximity_val= (touchA.raw_value-touch_min)servo_pos=160-min(160,max(30,proximity_val))servo_pos_last+= (servo_pos-servo_pos_last)*0.01# easing/smoothingservoA.angle=servo_pos_last
importosprint("\nHello World!")fs_stat=os.statvfs('/')print("Disk size in MB",fs_stat[0]*fs_stat[2]/1024/1024)print("Free space in MB",fs_stat[0]*fs_stat[3]/1024/1024)whileTrue:pass
importtimeimportboardimportneopixelimporttouchioimportusb_hidfromadafruit_hid.keyboardimportKeyboardfromadafruit_hid.keycodeimportKeycodetouchA=touchio.TouchIn(board.A0)pixel=neopixel.NeoPixel(board.NEOPIXEL,1,brightness=0.2,auto_write=False)pixel[0]= (0,0,0)time.sleep(1)# Sleep for a bit to avoid a race condition on some systemskbd=Keyboard(usb_hid.devices)whileTrue:iftouchA.value:print("A press")pixel[0]= (255,0,0)kbd.send(Keycode.RIGHT_ARROW)pixel.show()pixel[0]= (0,0,0)time.sleep(0.2)