def motor_stop(self): """ 停止编码电机转动 """ self.i2c.writeto(self.address, b'\x00\x00') sleep_ms(10)
def _effect(self, mode): """ 生效 """ write_buf = b'\x00' + mode self.i2c.writeto(self.address, write_buf) sleep_ms(10)
def humidity(self): """ 获取湿度 :return: 湿度,单位% """ self.i2c.writeto(0x40, b'\xf5') sleep_ms(25) t = i2c.readfrom(0x40, 2) return -6 + 125 * (t[0] * 256 + t[1]) / 65535
def temperature(self): """ 获取温度 :return: 温度,单位摄氏度 """ self.i2c.writeto(0x40, b'\xf3') sleep_ms(70) t = i2c.readfrom(0x40, 2) return -46.86 + 175.72 * (t[0] * 256 + t[1]) / 65535
def getLight(self): """ 获取光线值 :return: 返回光线值,单位lux """ self.i2c.writeto(0x23, bytearray([0x10])) sleep_ms(120) t = self.i2c.readfrom(0x23, 2) sleep_ms(10) return int((t[0] * 256 + t[1]) / 1.2)
def Init(self): """ 1602初始化函数 """ self._setRowOffsets(0x00, 0x40, (0x00 + 16), (0x40 + 16)) sleep_ms(50) self.display(True) self.Clear() self._sendCommand(_LCD_ENTRYMODESET | self._displaymode) self.Cursor(1) self.Blink(1)
def distance(self): """ 获取超声波测距 :return: 返回测距,单位cm """ self.i2c.writeto(0x0b, bytearray([1])) sleep_ms(2) temp = self.i2c.readfrom(0x0b, 2) distanceCM = (temp[0] + temp[1] * 256) / 10 return distanceCM
def common_measure(self): """ 获取实验探究类传感器测量值的通用函数 :return int: 返回传感器测量值,单位:电压(V)、电流(A)、磁场(mT)、电导率(uS/cm)、PH(pH)、光电门(s)、气压(kPa)、力传感器(N) """ self.i2c.scan() temp = self.i2c.readfrom(self.address, 2) data = ustruct.unpack(">h", temp) sleep_ms(20) return round(data[0] / 100, 2)
def note(self, note, on_off): """ 播放音符 :param note: MIDI音符编码 :param on_off: 音符播放或停止 """ if on_off == 1: self.uart.write(bytearray([0x90, note, 0x7f])) elif on_off == 0: self.uart.write(bytearray([0x80, note, 0x00])) sleep_ms(5)
def set_position(self, turn1, turn2): """ 定位模式,可设置编码编码电机定点位置,范围-1023~1023。 :param turn1: 设置M1通道编码电机定位,-1023~1023 :param turn2: 设置M2通道编码电机定位,-1023~1023 """ if turn1 > 1023 or turn1 < -1023 or turn2 > 1023 or turn2 < -1023: raise ValueError("Position out of range:0~1023") write_buf = b'\x10' + ustruct.pack('>ll', turn1, turn2) self.i2c.writeto(self.address, write_buf) sleep_ms(10) self._effect(self.POSITION_MODE)
def set_cruise(self, speed1, speed2): """ Cruise巡航模式,设定速度后,当编码电机受阻时,会根据反馈,自动调整扭力,稳定在恒定的速度。 :param speed1: 设置M1通道编码电机速度,范围-1023~1023 :param speed2: 设置M2通道编码电机速度,范围-1023~1023 """ if speed1 > 1023 or speed1 < -1023 or speed2 > 1023 or speed2 < -1023: raise ValueError("Speed out of range:-1023~1023") write_buf = b'\x0a' + ustruct.pack('>HH', speed1, speed2) self.i2c.writeto(self.address, write_buf) sleep_ms(10) self._effect(self.CRUISE_MODE)
def set_pwm(self, speed1, speed2): """ pwm模式 :param speed1: 设置M1通道编码电机速度,范围-1023~1023 :param speed2: 设置M2通道编码电机速度,范围-1023~1023 """ if speed1 > 1023 or speed1 < -1023 or speed2 > 1023 or speed2 < -1023: raise ValueError("Speed out of range:-1023~1023") write_buf = b'\x06' + ustruct.pack('>HH', speed1, speed2) self.i2c.writeto(self.address, write_buf) sleep_ms(10) self._effect(self.PWM_MODE)
def _cmdWrite(self, cmd): sum = 0 for i in range(0, 6): sum += cmd[i] sum1 = ((0xFFFF - sum) + 1) sum_l = sum1 & 0xff sum_h = sum1 >> 8 self.uart.write(bytearray([0x7E])) self.uart.write(cmd) self.uart.write(bytearray([sum_h])) self.uart.write(bytearray([sum_l])) self.uart.write(bytearray([0xEF])) sleep_ms(20)
def _cmdWrite(self, cmd): sum = 0 len = 0 for i in cmd: sum += i len += 1 len += 2 sum += len sum = sum & 0xff pakage = [0x7E, len] pakage += cmd pakage += ([sum, 0xEF]) self.uart.write(bytearray(pakage)) # print(len) # print(pakage) sleep_ms(100)
def __init__(self, tx): self.uart = UART(1, 31250, tx=tx) sleep_ms(30) self.uart.write(bytearray([0xb0, 0x78, 0x00])) sleep_ms(5) self.uart.write(bytearray([0xb0, 0x79, 0x7f])) sleep_ms(15) self.volume = 100
def getRGB(self): """ 获取RGB值 :return: 返回RGB三元组,(r,g,b) """ color = [0, 0, 0] self.i2c.writeto(0x0a, bytearray([1])) sleep_ms(100) self.i2c.writeto(0x0a, bytearray([2])) state = self.i2c.readfrom(0x0a, 1) if state[0] == 3: self.i2c.writeto(0x0a, bytearray([3])) c = self.i2c.readfrom(0x0a, 6) color[0] = c[5] * 256 + c[4] # color R color[1] = c[1] * 256 + c[0] # color G color[2] = c[3] * 256 + c[2] # color B maxColor = max(color[0], color[1], color[2]) if maxColor > 255: scale = 255 / maxColor color[0] = int(color[0] * scale) color[1] = int(color[1] * scale) color[2] = int(color[2] * scale) return color
def instrument(self, ins): self._ins = ins self.uart.write(bytearray([0xc0, self._ins])) sleep_ms(10)
def volume(self, vol): # range 0~127 volume self._vol = vol self.uart.write(bytearray([0xb0, 0x07, self._vol])) sleep_ms(10)
def _sendCharacter(self, c): character = bytearray([0x02, c]) self.i2c.writeto(24, character) sleep_ms(1)
def _sendCommand(self, cmd): command = bytearray([0x01, cmd]) self.i2c.writeto(24, command) sleep_ms(1)
def Home(self): """光标返回屏幕原点""" self._sendCommand(_LCD_RETURNHOME) sleep_ms(2)
def Clear(self): """清屏""" self._sendCommand(_LCD_CLEARDISPLAY) sleep_ms(2)