This will be part of a larger project (remotely controlled camera rig). I post this 'snippet' her in case somebody would need something similar. It can be adapted easily to send any IR code once you know what it looks like
PS: this is my first attempt at Python...
Code: Select all
"""
Trigger a Sony Nex camera every 5 seconds
The IR LED (in series with a 100 ohm resistance) is connected
from pin X2 to ground
Good Sony IR description: http://www.cypress.com/?docID=46755
"""
import pyb
import micropython
micropython.alloc_emergency_exception_buf(100)
class IRState:
def __init__(self):
self.cnt=-1
self.data=0
self.cells=""
def nextVal(self):
if self.cnt<0 or self.cnt>=len(self.cells):
self.cnt = -1
val = 'x'
else:
val = self.cells[self.cnt]
self.cnt = self.cnt+1
return val
def send(self, s):
if self.cnt >=0:
print ('send: busy')
self.cells = s
self.cnt = 0
timer = pyb.Timer(2, freq=40000)
ch2 = timer.channel(2, pyb.Timer.PWM, pin=pyb.Pin.board.X2, pulse_width_percent=50)
irs = IRState()
def cb(filter):
global irs
val = irs.nextVal()
if val=='m':
ch2.pulse_width_percent(20)
else:
ch2.pulse_width_percent(0)
modulator = pyb.Timer(1, freq=1666) # 1666 Hz -> 600 us cells
modulator.callback(cb)
S = 'mmmms' # start: 2400 us on 600 us off
I = 'mms' # 1 bit: 1200 us on 600 us off
O = 'ms' # 0 bit: 600 us on 600 us off
P='ssssssssssssssssssss' #pause
code = S+ I+O+I+I +O+I+O+O + I+O+I+I + I+O+O+O + I+I+I+I
all = code + P + code + P + code + P + code # 4 transmissions
while True:
irs.send(all)
pyb.delay(5000)