class Cooler: def __init__(self): self.state = "off" self.pin = Pin(13, Pin.OUT) self.intensity = self.pin.read() def set_state(self, state): self.state = state if state == "on": self.pin.value(1) elif state == "off": self.pin.value(0) else: print("invalid value") def get_state(self): return self.state def sub_cb(self, topic, msg): # sub_cb means "callback subroutine" print((topic, msg)) # Outputs the message that was received. Debugging use. if msg == b"ON": # If message says "ON" ... self.pin.value(1) # ... turns on 5V elif msg == b"OFF": # If message says "OFF" ... self.pin.value(0) # ... turns on 12V else: # If any other message is received ... print("Unknown message" ) # ... do nothing but output that it happened. # Subscribed messages will be delivered to this callback client.set_callback(self.sub_cb()) client.subscribe(topic="abho/feeds/cool") client.check_msg()
def read_extension(self, port, direction, pull_mode, level): if port not in self.io_list: _gpio_log.info("Port {} does not exist".format(port)) return 0 pin = Pin(port, direction, pull_mode, level) level = pin.read() _gpio_log.info("Port {}'s level is {}".format(port, level)) return level
def read(self, port): if port not in self.io_list: _gpio_log.info("Port {} does not exist".format(port)) return 0 pin = Pin(port, Pin.IN, Pin.PULL_PD, 0) level = pin.read() _gpio_log.info("Port {}'s level is {}".format(port, level)) return level
class lis2dh12(): i2c_dev = None address = None int_pin = None dev_log = None def init(self, slave_address): self.dev_log = log.getLogger("I2C") self.address = slave_address self.i2c_dev = I2C(I2C.I2C1, I2C.STANDARD_MODE) self.int_pin = Pin(Pin.GPIO3, Pin.IN, Pin.PULL_PU, 0) self.sensor_init() # 配置单击检测 self.Single_tap_enable() self.start_sensor() pass def read_data(self, regaddr, datalen, debug=True): r_data = [0x00 for i in range(datalen)] r_data = bytearray(r_data) reg_addres = bytearray([regaddr]) self.i2c_dev.read(self.address, reg_addres, 1, r_data, datalen, 1) ret_data = list(r_data) if debug is True: self.dev_log.debug(" read 0x{0:02x} from 0x{1:02x}".format( ret_data[0], regaddr)) return ret_data def write_data(self, regaddr, data, debug=True): w_data = bytearray([regaddr, data]) # 临时将需要传送的地址放在数据位 self.i2c_dev.write(self.address, bytearray(0x00), 0, bytearray(w_data), len(w_data)) if debug is True: self.dev_log.debug(" write 0x{0:02x} to 0x{1:02x}".format( data, regaddr)) def sensor_reset(self): self.dev_log.debug("Sensor restart Ing") # 重置chip self.write_data(LIS2DH12_CTRL_REG5, 0x80) time.sleep_ms(100) r_data = self.read_data(LIS2DH12_WHO_AM_I, 1) # 确定重启成功 while r_data[0] != 0x33: r_data = self.read_data(LIS2DH12_WHO_AM_I, 1) time.sleep_ms(5) self.dev_log.debug("Sensor restart successful") pass def sensor_init(self): # 1. 重置设备 self.sensor_reset() # 2. 初始化传感器 self.write_data(LIS2DH12_CTRL_REG2, 0x04) # 使能高分辨率 self.write_data(LIS2DH12_CTRL_REG3, 0x80) # 将中断引到INT1 引脚上面, 默认高电平有效 self.write_data(LIS2DH12_CTRL_REG4, 0x08) # ±2g, High-resolution mode def Single_tap_enable(self): # self.write_data(LIS2DH12_CLICK_CFG, 0x15) # 使能 XYZ 三轴单击中断, self.write_data(LIS2DH12_CLICK_CFG, 0x10) # 使能 Z 轴单击中断, self.write_data(LIS2DH12_CLICK_THS, 0x30) # 设置阈值 self.write_data(LIS2DH12_TIME_LIMIT, 0x18) # 设置时间窗口限制 self.write_data(LIS2DH12_TIME_LATENCY, 0x02) # 设置延时 def start_sensor(self): self.write_data(LIS2DH12_CTRL_REG1, 0x77) # 设置ODR 400HZ ,enable XYZ. # self.write_data(LIS2DH12_CTRL_REG1, 0x74) # 设置ODR ,enable Z轴. time.sleep_ms(20) # (7/ODR) = 18ms # Xl,Xh,Yl,Yh,Zl,Zh def ReadXYZ(self): data = [] for i in range(6): r_data = self.read_data(LIS2DH12_OUT_X_L + i, 1) data.append(r_data[0]) return data # 检测到上升沿了 def ProcessingData(self): self.dev_log.debug("An interrupt has been detected") data = self.ReadXYZ() self.dev_log.info("xL:{0},xH:{1},yL:{2},yH:{3},zL:{4},zH:{5}".format( data[0], data[1], data[2], data[3], data[4], data[5])) self.dev_log.info("X:{0} Y:{1} Z:{2}".format(data[0] & data[1], data[2] & data[3], data[4] & data[5])) pass def run(self): # 提示上下抬起开发板, 给传感器一个分量 self.dev_log.info("Lift the development board up and down") while True: value = self.int_pin.read() # 检测到中断信号了 if value == 1: self.ProcessingData()
GPIO3 – 引脚号178 GPIO4 – 引脚号199 GPIO5 – 引脚号204 EC600SCN平台引脚对应关系如下: GPIO1 – 引脚号10 GPIO2 – 引脚号11 GPIO3 – 引脚号12 GPIO4 – 引脚号13 GPIO5 – 引脚号14 GPIO6 – 引脚号15 GPIO7 – 引脚号16 GPIO8 – 引脚号39 GPIO9 – 引脚号40 GPIO10 – 引脚号48 * 参数2:direction IN – 输入模式 OUT – 输出模式 * 参数3:pull PULL_DISABLE – 禁用模式 PULL_PU – 上拉模式 PULL_PD – 下拉模式 * 参数4:level 0 设置引脚为低电平 1 设置引脚为高电平 ''' gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0) gpio1.write(1) # 设置gpio1 输出 gpio1.read() # 获取gpio的当前高低状态 # >>> 1
from machine import Pin gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0) ret = gpio1.write(1) print(ret) ret = gpio1.read() print(ret) ret = gpio1.write(0) print(ret) ret = gpio1.read() print(ret)
* 参数2:direction IN – 输入模式 OUT – 输出模式 * 参数3:pull PULL_DISABLE – 禁用模式 PULL_PU – 上拉模式 PULL_PD – 下拉模式 * 参数4:level 0 设置引脚为低电平 1 设置引脚为高电平 ''' gpio1 = Pin(Pin.GPIO1, Pin.OUT, Pin.PULL_DISABLE, 0) if __name__ == '__main__': ''' 手动运行本例程时,可以去掉该延时,如果将例程文件名改为main.py,希望开机自动运行时,需要加上该延时, 否则无法从CDC口看到下面的 poweron_print_once() 中打印的信息 ''' utime.sleep(5) checknet.poweron_print_once() ''' 如果用户程序包含网络相关代码,必须执行 wait_network_connected() 等待网络就绪(拨号成功); 如果是网络无关代码,可以屏蔽 wait_network_connected() 【本例程可以屏蔽下面这一行!】 ''' # checknet.wait_network_connected() gpio1.write(1) # 设置 gpio1 输出高电平 val = gpio1.read() # 获取 gpio1 的当前高低状态 print('val = {}'.format(val))
routercon.active() routercon.active(True) routercon.connect(ssid, password) #SIGNAL FROM RC SHUTTER PIXHAWK btn = Pin(4, Pin.IN) #GPIO4 (D2) as Input GPIO trim_threshold = 1500 while 1 < 2: #eternal loop #print('btn value') #print(btn.value()) print('time pulse value / trim values') print(time_pulse_us(btn, 1)) #1000 value is camera untriggered and 1800 is trigger camera if time_pulse_us(btn, 1) > trim_threshold: #pix.irq(shutter_camera,Pin.IRQ_RISING) print('cheese!!') try: requests.get('http://10.5.5.9/gp/gpControl/command/shutter?p=1') print(btn.read()) time.sleep(2) except OSError: print('query failed, sleeping 2 seconds...') time.sleep(2) print('request sent to GoPro API') else: print('waiting') time.sleep(0.25) #locate = requests.get('http://10.5.5.9/gp/gpControl/command/system/locate?p=1') #locate = requests.get('http://10.5.5.9/gp/gpControl/command/system/locate?p=1') #defaultcameramode = requests.get('http://10.5.5.9/gp/gpControl/setting/53/1') #defaultvideomode = requests.get('http://10.5.5.9/gp/gpControl/setting/53/1')