def __init__(self, scl=_SCL_PIN, sda=_SDA_PIN): """ Initialize the turtleplotbot, optionally passing an i2c object to use. """ self._current_step = [0, 0] # current step indexes self._step_delay = 1000 # us delay between steps self._pen_delay = 250 # ms delay for pen raise or lower self.mcp23017 = mcp23017.MCP23017( machine.I2C(scl=machine.Pin(scl), sda=machine.Pin(sda), freq=100000), 0x20) # pylint: disable=no-member self.mcp23017.porta.mode = 0x00 # porta output self.mcp23017.porta.gpio = 0x00 # all pins low self._pen_servo = Servo(machine.Pin(_SERVO_PIN, machine.Pin.OUT), freq=50, min_us=600, max_us=2400, angle=180) time.sleep_ms(100) self._pen_servo.write_angle(degrees=_PEN_UP_ANGLE) self.rst = machine.Pin(16, machine.Pin.OUT) # power pin for LCD display self.rst.value(1) # power on super().__init__()
def setmode(mode=BCM): GPIO.expanders = [] for i2c_addr in range(0x20, 0x28): try: expander = mcp23017.MCP23017(bus_addr=1, i2c_addr=i2c_addr) expander.read_byte(i2c_addr, 0x00) GPIO.expanders.append(expander) print("found", i2c_addr) except OSError: pass #print("missed", i2c_addr) RGPIO.setmode(mode) for expander in GPIO.expanders: expander.setmode(mode)
from machine import Pin from time import sleep import mcp23017 mcp = mcp23017.MCP23017(0x20) mcp.pullUps(mcp.PORTA, 0xFF) mcp.pullUps(mcp.PORTB, 0xFF) mcp.write8(mcp23017.MCP23017_IPOLA, 0xFF) mcp.write8(mcp23017.MCP23017_IPOLB, 0xFF) # mirror on, int pin high indicates interrupt mcp.configSystemInterrupt(mcp.INTMIRROROFF, mcp.INTPOLACTIVEHIGH) # for the input pin, enable interrupts and compare to previous value rather than default mcp.configPinInterrupts(mcp.PORTA, 0xFF, 0x00, 0x00) mcp.configPinInterrupts(mcp.PORTB, 0xFF, 0x00, 0x00) p_intA = 12 # D6 p_intB = 14 # D5 toggle_intA = Pin(p_intA, Pin.IN) toggle_intB = Pin(p_intB, Pin.IN) def cb(p): if p == toggle_intA: data = mcp.readInterrupt(mcp.PORTA) else: data = mcp.readInterrupt(mcp.PORTB) print("Values: {}".format(data)) # mcp.clearInterrupts() # sleep(0.02) #debounce
mcp_address = i break for i in range(72, 74): if i in found: print('Found ADS1015 at I2C address {} {}'.format(i, hex(i))) ads_address = i break del found # adc import ads1015 ads = ads1015.ADS1015(i2c, ads_address, gain=1) # mcp23017 import mcp23017 mcp = mcp23017.MCP23017(i2c, mcp_address) # sd card import uos as os from machine import SDCard sd = SDCard(slot=2) sd_cd = Pin(33, Pin.IN) # if sd card detect pin is LOW, a card is inserted if sd_cd.value() == 0: try: print('Found SD card {}'.format(sd.info())) os.mount(sd, '/sd') print('Mounted SD card at /sd') except OSError: print('Failed to mount SD card')
def run_switch(params, start_time=None): """Run the switch. Cycle between sources by turning on appropriate Raspberry Pi GPIO pins for user-specified lengths of time. - params : options from parser - start_time : optional starting time stamp for the log file """ # Open log file if start_time is None: start_time = datetime.datetime.utcnow().strftime('%Y%m%d_%H%M%S') # Setup MCP23017 GPIO's as outputs gpios = mcp23017.MCP23017(i2c_bus, 0x20) for switch in params['switch-control']['switches'].keys(): for gpio in params['switch-control']['switches'][switch]['gpios'].keys( ): port = None pin = None if params['switch-control']['switches'][switch]['gpios'][ gpio] != 'None': port = params['switch-control']['switches'][switch]['gpios'][ gpio][0] pin = params['switch-control']['switches'][switch]['gpios'][ gpio][1:] logging.debug('Setting port %s, pin %s to output' % (port, pin)) gpios.set_gpio_direction(port, pin, False) for aux_gpio in params['switch-control']['aux-gpios'].keys(): port = params['switch-control']['aux-gpios'][aux_gpio][0] pin = params['switch-control']['aux-gpios'][aux_gpio][1:] logging.debug('Setting port %s, pin %s to output' % (port, pin)) gpios.set_gpio_direction(port, pin, False) while True: tstart = time.time() tfrag = repr(tstart)[:5] #starttime = datetime.datetime.utcnow()#.strftime('%Y%m%d_%H%M%S') outsubdir = params[ 'data-directory'] + '/switch_data/' + tfrag + '/' + str( nm.int64(tstart)) if not os.path.isdir(outsubdir): os.makedirs(outsubdir) pos_scio_files = {} for pos in params['switch-control']['sequence']: pos_scio_files[pos] = scio.scio( outsubdir + '/%s.scio' % (pos), compress=params['scio-files']['compress']) seq_list = params['switch-control']['sequence'] while time.time() - tstart < params['scio-files']['file_time']: seq = seq_list.pop(0) which_switch = params['switch-control'][seq]['switch'] which_pos = str(params['switch-control'][seq]['position']) ontime = params['switch-control'][seq]['ontime'] port = params['switch-control']['switches'][which_switch]['gpios'][ which_pos][0] pin = params['switch-control']['switches'][which_switch]['gpios'][ which_pos][1:] starttime = datetime.datetime.utcnow().strftime('%Y%m%d_%H%M%S') logging.debug('Reset On') if params['switch-control']['switches'][which_switch]['gpios'][ 'reset'] != 'None': reset_port = params['switch-control']['switches'][ which_switch]['gpios']['reset'][0] reset_pin = params['switch-control']['switches'][which_switch][ 'gpios']['reset'][1:] gpios.set_output_latch(reset_port, reset_pin, True) time.sleep(0.20) gpios.set_output_latch(reset_port, reset_pin, False) time.sleep(0.01) logging.debug('Reset off') # Special case for noise source: need to turn mosfet on as well if params['switch-control'][seq]['aux'] != 'None': aux = params['switch-control'][seq]['aux'] aux_port = params['switch-control']['aux-gpios'][aux][0] aux_pin = params['switch-control']['aux-gpios'][aux][1:] gpios.set_output_latch(aux_port, aux_pin, True) logging.debug('AUX %s on' % (aux)) # Set switch to the appropriate # source. These are latching # switches, so just need to pulse the # pin... gpios.set_output_latch(port, pin, True) logging.debug('%s Source On' % (seq)) time.sleep(0.20) # ...and then immediately turn off again logging.debug('%s Source Off' % (seq)) gpios.set_output_latch(port, pin, False) # Record time stamps for how long # we're onu this switch position. Note # that we're using system time here, # will need to correct with RTC time # in post-processing. t_start = time.time() pos_scio_files[seq].append(nm.array([1, t_start])) time.sleep(ontime) if params['switch-control'][seq]['aux'] != 'None': aux = params['switch-control'][seq]['aux'] aux_port = params['switch-control']['aux-gpios'][aux][0] aux_pin = params['switch-control']['aux-gpios'][aux][1:] gpios.set_output_latch(aux_port, aux_pin, False) logging.debug('AUX %s off' % (aux)) t_stop = time.time() pos_scio_files[seq].append(nm.array([0, t_stop])) seq_list.append(seq) return None