Optimizations and comments.

master
Guy Carver 2018-02-28 18:03:42 -05:00
rodzic 3fb554106f
commit ac5224abdb
10 zmienionych plików z 776 dodań i 937 usunięć

Wyświetl plik

@ -2,545 +2,540 @@
#driver for gy-521 Accelerometer
#Translated by Guy Carver from the MPU6050 sample code.
#todo: use const().
import pyb
ADDRESS_LOW = 0x68 #address pin low (GND), default for InvenSense evaluation board
ADDRESS_HIGH = 0x69 #address pin high (VCC)
def setvalue( aOriginal, aPosBits, aValue ) :
'''Set a specific bit on a variable. aPosBits = 0xpm (p = pos, m = # mask bits).'''
mask = (1 << (aPosBits & 0xf)) - 1
pos = aPosBits >> 4
aOriginal &= ~(mask << pos)
return aOriginal | (aValue & mask) << pos
RA_XG_OFFS_TC = 0x00 //[7] PWR_MODE, [6:1] XG_OFFS_TC, [0] OTP_BNK_VLD
RA_YG_OFFS_TC = 0x01 //[7] PWR_MODE, [6:1] YG_OFFS_TC, [0] OTP_BNK_VLD
RA_ZG_OFFS_TC = 0x02 //[7] PWR_MODE, [6:1] ZG_OFFS_TC, [0] OTP_BNK_VLD
RA_X_FINE_GAIN = 0x03 //[7:0] X_FINE_GAIN
RA_Y_FINE_GAIN = 0x04 //[7:0] Y_FINE_GAIN
RA_Z_FINE_GAIN = 0x05 //[7:0] Z_FINE_GAIN
RA_XA_OFFS_H = 0x06 //[15:0] XA_OFFS
RA_XA_OFFS_L_TC = 0x07
RA_YA_OFFS_H = 0x08 //[15:0] YA_OFFS
RA_YA_OFFS_L_TC = 0x09
RA_ZA_OFFS_H = 0x0A //[15:0] ZA_OFFS
RA_ZA_OFFS_L_TC = 0x0B
RA_XG_OFFS_USRH = 0x13 //[15:0] XG_OFFS_USR
RA_XG_OFFS_USRL = 0x14
RA_YG_OFFS_USRH = 0x15 //[15:0] YG_OFFS_USR
RA_YG_OFFS_USRL = 0x16
RA_ZG_OFFS_USRH = 0x17 //[15:0] ZG_OFFS_USR
RA_ZG_OFFS_USRL = 0x18
RA_SMPLRT_DIV = 0x19
RA_CONFIG = 0x1A
RA_GYRO_CONFIG = 0x1B
RA_ACCEL_CONFIG = 0x1C
RA_FF_THR = 0x1D
RA_FF_DUR = 0x1E
RA_MOT_THR = 0x1F
RA_MOT_DUR = 0x20
RA_ZRMOT_THR = 0x21
RA_ZRMOT_DUR = 0x22
RA_FIFO_EN = 0x23
RA_I2C_MST_CTRL = 0x24
RA_I2C_SLV0_ADDR = 0x25
RA_I2C_SLV0_REG = 0x26
RA_I2C_SLV0_CTRL = 0x27
RA_I2C_SLV1_ADDR = 0x28
RA_I2C_SLV1_REG = 0x29
RA_I2C_SLV1_CTRL = 0x2A
RA_I2C_SLV2_ADDR = 0x2B
RA_I2C_SLV2_REG = 0x2C
RA_I2C_SLV2_CTRL = 0x2D
RA_I2C_SLV3_ADDR = 0x2E
RA_I2C_SLV3_REG = 0x2F
RA_I2C_SLV3_CTRL = 0x30
RA_I2C_SLV4_ADDR = 0x31
RA_I2C_SLV4_REG = 0x32
RA_I2C_SLV4_DO = 0x33
RA_I2C_SLV4_CTRL = 0x34
RA_I2C_SLV4_DI = 0x35
RA_I2C_MST_STATUS = 0x36
RA_INT_PIN_CFG = 0x37
RA_INT_ENABLE = 0x38
RA_DMP_INT_STATUS = 0x39
RA_INT_STATUS = 0x3A
RA_ACCEL_XOUT_H = 0x3B
RA_ACCEL_XOUT_L = 0x3C
RA_ACCEL_YOUT_H = 0x3D
RA_ACCEL_YOUT_L = 0x3E
RA_ACCEL_ZOUT_H = 0x3F
RA_ACCEL_ZOUT_L = 0x40
RA_TEMP_OUT_H = 0x41
RA_TEMP_OUT_L = 0x42
RA_GYRO_XOUT_H = 0x43
RA_GYRO_XOUT_L = 0x44
RA_GYRO_YOUT_H = 0x45
RA_GYRO_YOUT_L = 0x46
RA_GYRO_ZOUT_H = 0x47
RA_GYRO_ZOUT_L = 0x48
RA_EXT_SENS_DATA_00 = 0x49
RA_EXT_SENS_DATA_01 = 0x4A
RA_EXT_SENS_DATA_02 = 0x4B
RA_EXT_SENS_DATA_03 = 0x4C
RA_EXT_SENS_DATA_04 = 0x4D
RA_EXT_SENS_DATA_05 = 0x4E
RA_EXT_SENS_DATA_06 = 0x4F
RA_EXT_SENS_DATA_07 = 0x50
RA_EXT_SENS_DATA_08 = 0x51
RA_EXT_SENS_DATA_09 = 0x52
RA_EXT_SENS_DATA_10 = 0x53
RA_EXT_SENS_DATA_11 = 0x54
RA_EXT_SENS_DATA_12 = 0x55
RA_EXT_SENS_DATA_13 = 0x56
RA_EXT_SENS_DATA_14 = 0x57
RA_EXT_SENS_DATA_15 = 0x58
RA_EXT_SENS_DATA_16 = 0x59
RA_EXT_SENS_DATA_17 = 0x5A
RA_EXT_SENS_DATA_18 = 0x5B
RA_EXT_SENS_DATA_19 = 0x5C
RA_EXT_SENS_DATA_20 = 0x5D
RA_EXT_SENS_DATA_21 = 0x5E
RA_EXT_SENS_DATA_22 = 0x5F
RA_EXT_SENS_DATA_23 = 0x60
RA_MOT_DETECT_STATUS = 0x61
RA_I2C_SLV0_DO = 0x63
RA_I2C_SLV1_DO = 0x64
RA_I2C_SLV2_DO = 0x65
RA_I2C_SLV3_DO = 0x66
RA_I2C_MST_DELAY_CTRL = 0x67
RA_SIGNAL_PATH_RESET = 0x68
RA_MOT_DETECT_CTRL = 0x69
RA_USER_CTRL = 0x6A
RA_PWR_MGMT_1 = 0x6B
RA_PWR_MGMT_2 = 0x6C
RA_BANK_SEL = 0x6D
RA_MEM_START_ADDR = 0x6E
RA_MEM_R_W = 0x6F
RA_DMP_CFG_1 = 0x70
RA_DMP_CFG_2 = 0x71
RA_FIFO_COUNTH = 0x72
RA_FIFO_COUNTL = 0x73
RA_FIFO_R_W = 0x74
RA_WHO_AM_I = 0x75
TC_PWR_MODE_BIT = 7
TC_OFFSET_BIT = 6
TC_OFFSET_LENGTH = 6
TC_OTP_BNK_VLD_BIT = 0
VDDIO_LEVEL_VLOGIC = 0
VDDIO_LEVEL_VDD = 1
CFG_EXT_SYNC_SET_BIT = 5
CFG_EXT_SYNC_SET_LENGTH = 3
CFG_DLPF_CFG_BIT = 2
CFG_DLPF_CFG_LENGTH = 3
EXT_SYNC_DISABLED = 0x0
EXT_SYNC_TEMP_OUT_L = 0x1
EXT_SYNC_GYRO_XOUT_L = 0x2
EXT_SYNC_GYRO_YOUT_L = 0x3
EXT_SYNC_GYRO_ZOUT_L = 0x4
EXT_SYNC_ACCEL_XOUT_L = 0x5
EXT_SYNC_ACCEL_YOUT_L = 0x6
EXT_SYNC_ACCEL_ZOUT_L = 0x7
DLPF_BW_256 = 0x00
DLPF_BW_188 = 0x01
DLPF_BW_98 = 0x02
DLPF_BW_42 = 0x03
DLPF_BW_20 = 0x04
DLPF_BW_10 = 0x05
DLPF_BW_5 = 0x06
GCONFIG_FS_SEL_BIT = 4
GCONFIG_FS_SEL_LENGTH = 2
GYRO_FS_250 = 0x00
GYRO_FS_500 = 0x01
GYRO_FS_1000 = 0x02
GYRO_FS_2000 = 0x03
ACONFIG_XA_ST_BIT = 7
ACONFIG_YA_ST_BIT = 6
ACONFIG_ZA_ST_BIT = 5
ACONFIG_AFS_SEL_BIT = 4
ACONFIG_AFS_SEL_LENGTH = 2
ACONFIG_ACCEL_HPF_BIT = 2
ACONFIG_ACCEL_HPF_LENGTH = 3
ACCEL_FS_2 = 0x00
ACCEL_FS_4 = 0x01
ACCEL_FS_8 = 0x02
ACCEL_FS_16 = 0x03
DHPF_RESET = 0x00
DHPF_5 = 0x01
DHPF_2P5 = 0x02
DHPF_1P25 = 0x03
DHPF_0P63 = 0x04
DHPF_HOLD = 0x07
TEMP_FIFO_EN_BIT = 7
XG_FIFO_EN_BIT = 6
YG_FIFO_EN_BIT = 5
ZG_FIFO_EN_BIT = 4
ACCEL_FIFO_EN_BIT = 3
SLV2_FIFO_EN_BIT = 2
SLV1_FIFO_EN_BIT = 1
SLV0_FIFO_EN_BIT = 0
MULT_MST_EN_BIT = 7
WAIT_FOR_ES_BIT = 6
SLV_3_FIFO_EN_BIT = 5
I2C_MST_P_NSR_BIT = 4
I2C_MST_CLK_BIT = 3
I2C_MST_CLK_LENGTH = 4
CLOCK_DIV_348 = 0x0
CLOCK_DIV_333 = 0x1
CLOCK_DIV_320 = 0x2
CLOCK_DIV_308 = 0x3
CLOCK_DIV_296 = 0x4
CLOCK_DIV_286 = 0x5
CLOCK_DIV_276 = 0x6
CLOCK_DIV_267 = 0x7
CLOCK_DIV_258 = 0x8
CLOCK_DIV_500 = 0x9
CLOCK_DIV_471 = 0xA
CLOCK_DIV_444 = 0xB
CLOCK_DIV_421 = 0xC
CLOCK_DIV_400 = 0xD
CLOCK_DIV_381 = 0xE
CLOCK_DIV_364 = 0xF
I2C_SLV_RW_BIT = 7
I2C_SLV_ADDR_BIT = 6
I2C_SLV_ADDR_LENGTH = 7
I2C_SLV_EN_BIT = 7
I2C_SLV_BYTE_SW_BIT = 6
I2C_SLV_REG_DIS_BIT = 5
I2C_SLV_GRP_BIT = 4
I2C_SLV_LEN_BIT = 3
I2C_SLV_LEN_LENGTH = 4
I2C_SLV4_RW_BIT = 7
I2C_SLV4_ADDR_BIT = 6
I2C_SLV4_ADDR_LENGTH = 7
I2C_SLV4_EN_BIT = 7
I2C_SLV4_INT_EN_BIT = 6
I2C_SLV4_REG_DIS_BIT = 5
I2C_SLV4_MST_DLY_BIT = 4
I2C_SLV4_MST_DLY_LENGTH = 5
MST_PASS_THROUGH_BIT = 7
MST_I2C_SLV4_DONE_BIT = 6
MST_I2C_LOST_ARB_BIT = 5
MST_I2C_SLV4_NACK_BIT = 4
MST_I2C_SLV3_NACK_BIT = 3
MST_I2C_SLV2_NACK_BIT = 2
MST_I2C_SLV1_NACK_BIT = 1
MST_I2C_SLV0_NACK_BIT = 0
INTCFG_INT_LEVEL_BIT = 7
INTCFG_INT_OPEN_BIT = 6
INTCFG_LATCH_INT_EN_BIT = 5
INTCFG_INT_RD_CLEAR_BIT = 4
INTCFG_FSYNC_INT_LEVEL_BIT = 3
INTCFG_FSYNC_INT_EN_BIT = 2
INTCFG_I2C_BYPASS_EN_BIT = 1
INTCFG_CLKOUT_EN_BIT = 0
INTMODE_ACTIVEHIGH = 0x00
INTMODE_ACTIVELOW = 0x01
INTDRV_PUSHPULL = 0x00
INTDRV_OPENDRAIN = 0x01
INTLATCH_50USPULSE = 0x00
INTLATCH_WAITCLEAR = 0x01
INTCLEAR_STATUSREAD = 0x00
INTCLEAR_ANYREAD = 0x01
INTERRUPT_FF_BIT = 7
INTERRUPT_MOT_BIT = 6
INTERRUPT_ZMOT_BIT = 5
INTERRUPT_FIFO_OFLOW_BIT = 4
INTERRUPT_I2C_MST_INT_BIT = 3
INTERRUPT_PLL_RDY_INT_BIT = 2
INTERRUPT_DMP_INT_BIT = 1
INTERRUPT_DATA_RDY_BIT = 0
# TODO: figure out what these actually do
# UMPL source code is not very obivous
DMPINT_5_BIT = 5
DMPINT_4_BIT = 4
DMPINT_3_BIT = 3
DMPINT_2_BIT = 2
DMPINT_1_BIT = 1
DMPINT_0_BIT = 0
MOTION_MOT_XNEG_BIT = 7
MOTION_MOT_XPOS_BIT = 6
MOTION_MOT_YNEG_BIT = 5
MOTION_MOT_YPOS_BIT = 4
MOTION_MOT_ZNEG_BIT = 3
MOTION_MOT_ZPOS_BIT = 2
MOTION_MOT_ZRMOT_BIT = 0
DELAYCTRL_DELAY_ES_SHADOW_BIT = 7
DELAYCTRL_I2C_SLV4_DLY_EN_BIT = 4
DELAYCTRL_I2C_SLV3_DLY_EN_BIT = 3
DELAYCTRL_I2C_SLV2_DLY_EN_BIT = 2
DELAYCTRL_I2C_SLV1_DLY_EN_BIT = 1
DELAYCTRL_I2C_SLV0_DLY_EN_BIT = 0
PATHRESET_GYRO_RESET_BIT = 2
PATHRESET_ACCEL_RESET_BIT = 1
PATHRESET_TEMP_RESET_BIT = 0
DETECT_ACCEL_ON_DELAY_BIT = 5
DETECT_ACCEL_ON_DELAY_LENGTH = 2
DETECT_FF_COUNT_BIT = 3
DETECT_FF_COUNT_LENGTH = 2
DETECT_MOT_COUNT_BIT = 1
DETECT_MOT_COUNT_LENGTH = 2
DETECT_DECREMENT_RESET = 0x0
DETECT_DECREMENT_1 = 0x1
DETECT_DECREMENT_2 = 0x2
DETECT_DECREMENT_4 = 0x3
USERCTRL_DMP_EN_BIT = 7
USERCTRL_FIFO_EN_BIT = 6
USERCTRL_I2C_MST_EN_BIT = 5
USERCTRL_I2C_IF_DIS_BIT = 4
USERCTRL_DMP_RESET_BIT = 3
USERCTRL_FIFO_RESET_BIT = 2
USERCTRL_I2C_MST_RESET_BIT = 1
USERCTRL_SIG_COND_RESET_BIT = 0
PWR1_DEVICE_RESET_BIT = 7
PWR1_SLEEP_BIT = 6
PWR1_CYCLE_BIT = 5
PWR1_TEMP_DIS_BIT = 3
PWR1_CLKSEL_BIT = 2
PWR1_CLKSEL_LENGTH = 3
CLOCK_INTERNAL = 0x00
CLOCK_PLL_XGYRO = 0x01
CLOCK_PLL_YGYRO = 0x02
CLOCK_PLL_ZGYRO = 0x03
CLOCK_PLL_EXT32K = 0x04
CLOCK_PLL_EXT19M = 0x05
CLOCK_KEEP_RESET = 0x07
PWR2_LP_WAKE_CTRL_BIT = 7
PWR2_LP_WAKE_CTRL_LENGTH = 2
PWR2_STBY_XA_BIT = 5
PWR2_STBY_YA_BIT = 4
PWR2_STBY_ZA_BIT = 3
PWR2_STBY_XG_BIT = 2
PWR2_STBY_YG_BIT = 1
PWR2_STBY_ZG_BIT = 0
WAKE_FREQ_1P25 = 0x0
WAKE_FREQ_2P5 = 0x1
WAKE_FREQ_5 = 0x2
WAKE_FREQ_10 = 0x3
BANKSEL_PRFTCH_EN_BIT = 6
BANKSEL_CFG_USER_BANK_BIT = 5
BANKSEL_MEM_SEL_BIT = 4
BANKSEL_MEM_SEL_LENGTH = 5
WHO_AM_I_BIT = 6
WHO_AM_I_LENGTH = 6
DMP_MEMORY_BANKS = 8
DMP_MEMORY_BANK_SIZE = 256
DMP_MEMORY_CHUNK_SIZE = 16
def getvalue( aOriginal, aPosBits ) :
'''Get a specific bit from a variable. aPosBits = 0xpm (p = pos, m = # mask bits).'''
mask = (1 << (aPosBits & 0xf)) - 1
pos = aPosBits >> 4
return (aOriginal >> pos) & mask
class accel(object) :
"""gy-521 Accelerometer."""
@staticmethod
def color( aR, aG, aB ) :
'''Create a 565 rgb TFTColor value'''
return TFTColor(aR, aG, aB)
_ADDRESS_LOW = const(0x68) #address pin low (GND), default for InvenSense evaluation board
_ADDRESS_HIGH = const(0x69) #address pin high (VCC)
def __init__( self, aLoc, aAddress = ADDRESS_LOW ) :
"""aLoc I2C pin location is either 1 for 'X' or 2 for 'Y'.
# _RA_XG_OFFS_TC = const(0x00) #[7] PWR_MODE, [6:1] XG_OFFS_TC, [0] OTP_BNK_VLD
# _RA_YG_OFFS_TC = const(0x01) #[7] PWR_MODE, [6:1] YG_OFFS_TC, [0] OTP_BNK_VLD
# _RA_ZG_OFFS_TC = const(0x02) #[7] PWR_MODE, [6:1] ZG_OFFS_TC, [0] OTP_BNK_VLD
# _RA_X_FINE_GAIN = const(0x03) #[7:0] X_FINE_GAIN
# _RA_Y_FINE_GAIN = const(0x04) #[7:0] Y_FINE_GAIN
# _RA_Z_FINE_GAIN = const(0x05) #[7:0] Z_FINE_GAIN
# _RA_XA_OFFS_H = const(0x06) #[15:0] XA_OFFS
# _RA_XA_OFFS_L_TC = const(0x07)
# _RA_YA_OFFS_H = const(0x08) #[15:0] YA_OFFS
# _RA_YA_OFFS_L_TC = const(0x09)
# _RA_ZA_OFFS_H = const(0x0A) #[15:0] ZA_OFFS
# _RA_ZA_OFFS_L_TC = const(0x0B)
# _RA_XG_OFFS_USRH = const(0x13) #[15:0] XG_OFFS_USR
# _RA_XG_OFFS_USRL = const(0x14)
# _RA_YG_OFFS_USRH = const(0x15) #[15:0] YG_OFFS_USR
# _RA_YG_OFFS_USRL = const(0x16)
# _RA_ZG_OFFS_USRH = const(0x17) #[15:0] ZG_OFFS_USR
# _RA_ZG_OFFS_USRL = const(0x18)
_RA_SMPLRT_DIV = const(0x19)
_RA_CONFIG = const(0x1A)
_RA_GYRO_CONFIG = const(0x1B)
_RA_ACCEL_CONFIG = const(0x1C)
_RA_FF_THR = const(0x1D)
_RA_FF_DUR = const(0x1E)
_RA_MOT_THR = const(0x1F)
_RA_MOT_DUR = const(0x20)
_RA_ZRMOT_THR = const(0x21)
_RA_ZRMOT_DUR = const(0x22)
_RA_FIFO_EN = const(0x23)
_RA_I2C_MST_CTRL = const(0x24)
# _RA_I2C_SLV0_ADDR = const(0x25)
# _RA_I2C_SLV0_REG = const(0x26)
# _RA_I2C_SLV0_CTRL = const(0x27)
# _RA_I2C_SLV1_ADDR = const(0x28)
# _RA_I2C_SLV1_REG = const(0x29)
# _RA_I2C_SLV1_CTRL = const(0x2A)
# _RA_I2C_SLV2_ADDR = const(0x2B)
# _RA_I2C_SLV2_REG = const(0x2C)
# _RA_I2C_SLV2_CTRL = const(0x2D)
# _RA_I2C_SLV3_ADDR = const(0x2E)
# _RA_I2C_SLV3_REG = const(0x2F)
# _RA_I2C_SLV3_CTRL = const(0x30)
# _RA_I2C_SLV4_ADDR = const(0x31)
# _RA_I2C_SLV4_REG = const(0x32)
# _RA_I2C_SLV4_DO = const(0x33)
# _RA_I2C_SLV4_CTRL = const(0x34)
# _RA_I2C_SLV4_DI = const(0x35)
# _RA_I2C_MST_STATUS = const(0x36)
_RA_INT_PIN_CFG = const(0x37)
# _RA_INT_ENABLE = const(0x38)
# _RA_DMP_INT_STATUS = const(0x39)
# _RA_INT_STATUS = const(0x3A)
_RA_ACCEL_XOUT_H = const(0x3B)
# _RA_ACCEL_XOUT_L = const(0x3C)
# _RA_ACCEL_YOUT_H = const(0x3D)
# _RA_ACCEL_YOUT_L = const(0x3E)
# _RA_ACCEL_ZOUT_H = const(0x3F)
# _RA_ACCEL_ZOUT_L = const(0x40)
# _RA_TEMP_OUT_H = const(0x41)
# _RA_TEMP_OUT_L = const(0x42)
_RA_GYRO_XOUT_H = const(0x43)
# _RA_GYRO_XOUT_L = const(0x44)
# _RA_GYRO_YOUT_H = const(0x45)
# _RA_GYRO_YOUT_L = const(0x46)
# _RA_GYRO_ZOUT_H = const(0x47)
# _RA_GYRO_ZOUT_L = const(0x48)
# _RA_EXT_SENS_DATA_00 = const(0x49)
# _RA_EXT_SENS_DATA_01 = const(0x4A)
# _RA_EXT_SENS_DATA_02 = const(0x4B)
# _RA_EXT_SENS_DATA_03 = const(0x4C)
# _RA_EXT_SENS_DATA_04 = const(0x4D)
# _RA_EXT_SENS_DATA_05 = const(0x4E)
# _RA_EXT_SENS_DATA_06 = const(0x4F)
# _RA_EXT_SENS_DATA_07 = const(0x50)
# _RA_EXT_SENS_DATA_08 = const(0x51)
# _RA_EXT_SENS_DATA_09 = const(0x52)
# _RA_EXT_SENS_DATA_10 = const(0x53)
# _RA_EXT_SENS_DATA_11 = const(0x54)
# _RA_EXT_SENS_DATA_12 = const(0x55)
# _RA_EXT_SENS_DATA_13 = const(0x56)
# _RA_EXT_SENS_DATA_14 = const(0x57)
# _RA_EXT_SENS_DATA_15 = const(0x58)
# _RA_EXT_SENS_DATA_16 = const(0x59)
# _RA_EXT_SENS_DATA_17 = const(0x5A)
# _RA_EXT_SENS_DATA_18 = const(0x5B)
# _RA_EXT_SENS_DATA_19 = const(0x5C)
# _RA_EXT_SENS_DATA_20 = const(0x5D)
# _RA_EXT_SENS_DATA_21 = const(0x5E)
# _RA_EXT_SENS_DATA_22 = const(0x5F)
# _RA_EXT_SENS_DATA_23 = const(0x60)
# _RA_MOT_DETECT_STATUS = const(0x61)
# _RA_I2C_SLV0_DO = const(0x63)
# _RA_I2C_SLV1_DO = const(0x64)
# _RA_I2C_SLV2_DO = const(0x65)
# _RA_I2C_SLV3_DO = const(0x66)
# _RA_I2C_MST_DELAY_CTRL = const(0x67)
# _RA_SIGNAL_PATH_RESET = const(0x68)
# _RA_MOT_DETECT_CTRL = const(0x69)
_RA_USER_CTRL = const(0x6A)
_RA_PWR_MGMT_1 = const(0x6B)
# _RA_PWR_MGMT_2 = const(0x6C)
# _RA_BANK_SEL = const(0x6D)
# _RA_MEM_START_ADDR = const(0x6E)
# _RA_MEM_R_W = const(0x6F)
# _RA_DMP_CFG_1 = const(0x70)
# _RA_DMP_CFG_2 = const(0x71)
# _RA_FIFO_COUNTH = const(0x72)
# _RA_FIFO_COUNTL = const(0x73)
# _RA_FIFO_R_W = const(0x74)
# _RA_WHO_AM_I = const(0x75)
#Value Location data.
#Following are the bytes describing the data packed into each 8 bit value on the chip.
#Nibble 1 = position, 0 = bits
#NOTE: Commented out currently unused locations.
# _TC_OTP_BNK_VLD = const(0x01)
# _TC_OFFSET = const(0x16)
# _TC_PWR_MODE = const(0x71)
_CFG_DLPF_CFG = const(0x03)
# _CFG_EXT_SYNC_SET = const(0x33)
_GCONFIG_FS_SEL = const(0x32)
# _ACONFIG_ACCEL_HPF = const(0x03)
_ACONFIG_AFS_SEL = const(0x32)
# _ACONFIG_ZA_ST = const(0x51)
# _ACONFIG_YA_ST = const(0x61)
# _ACONFIG_XA_ST = const(0x71)
# _SLV0_FIFO_EN = const(0X01)
# _SLV1_FIFO_EN = const(0X11)
# _SLV2_FIFO_EN = const(0X21)
_ACCEL_FIFO_EN = const(0X31)
_ZG_FIFO_EN = const(0X41)
_YG_FIFO_EN = const(0X51)
_XG_FIFO_EN = const(0X61)
_TEMP_FIFO_EN = const(0X71)
_I2C_MST_CLK = const(0X04)
# _I2C_MST_P_NSR = const(0X41)
# _SLV_3_FIFO_EN = const(0X51)
# _WAIT_FOR_ES = const(0X61)
# _MULT_MST_EN = const(0X71)
# _I2C_SLV_LEN = const(0X04)
# _I2C_SLV_GRP = const(0X41)
# _I2C_SLV_REG_DIS = const(0X51)
# _I2C_SLV_BYTE_SW = const(0X61)
# _I2C_SLV_EN = const(0X71)
# _I2C_SLV_ADDR = const(0X07)
# _I2C_SLV_RW = const(0X71)
# _I2C_SLV4_MST_DLY = const(0X05)
# _I2C_SLV4_REG_DIS = const(0X51)
# _I2C_SLV4_INT_EN = const(0X61)
# _I2C_SLV4_EN = const(0X71)
# _I2C_SLV4_ADDR = const(0X07)
# _I2C_SLV4_RW = const(0X71)
# _MST_I2C_SLV0_NACK = const(0X01)
# _MST_I2C_SLV1_NACK = const(0X11)
# _MST_I2C_SLV2_NACK = const(0X21)
# _MST_I2C_SLV3_NACK = const(0X31)
# _MST_I2C_SLV4_NACK = const(0X41)
# _MST_I2C_LOST_ARB = const(0X51)
# _MST_I2C_SLV4_DONE = const(0X61)
# _MST_PASS_THROUGH = const(0X71)
# _INTERRUPT_DATA_RDY = const(0X01)
# _INTERRUPT_DMP_INT = const(0X11)
# _INTERRUPT_PLL_RDY_INT = const(0X21)
# _INTERRUPT_I2C_MST_INT = const(0X31)
# _INTERRUPT_FIFO_OFLOW = const(0X41)
# _INTERRUPT_ZMOT = const(0X51)
# _INTERRUPT_MOT = const(0X61)
# _INTERRUPT_FF = const(0X71)
# TODO: figure out what these actually do
# UMPL source code is not very obivous
# _DMPINT_0 = const(0X01)
# _DMPINT_1 = const(0X11)
# _DMPINT_2 = const(0X21)
# _DMPINT_3 = const(0X31)
# _DMPINT_4 = const(0X41)
# _DMPINT_5 = const(0X51)
# _MOTION_MOT_ZRMOT = const(0X01)
# _MOTION_MOT_ZPOS = const(0X21)
# _MOTION_MOT_ZNEG = const(0X31)
# _MOTION_MOT_YPOS = const(0X41)
# _MOTION_MOT_YNEG = const(0X51)
# _MOTION_MOT_XPOS = const(0X61)
# _MOTION_MOT_XNEG = const(0X71)
# _DELAYCTRL_I2C_SLV0_DLY_EN = const(0X01)
# _DELAYCTRL_I2C_SLV1_DLY_EN = const(0X11)
# _DELAYCTRL_I2C_SLV2_DLY_EN = const(0X21)
# _DELAYCTRL_I2C_SLV3_DLY_EN = const(0X31)
# _DELAYCTRL_I2C_SLV4_DLY_EN = const(0X41)
# _DELAYCTRL_DELAY_ES_SHADOW = const(0X71)
# _PATHRESET_TEMP_RESET = const(0X01)
# _PATHRESET_ACCEL_RESET = const(0X11)
# _PATHRESET_GYRO_RESET = const(0X21)
# _DETECT_MOT_COUNT = const(0X02)
# _DETECT_FF_COUNT = const(0X22)
# _DETECT_ACCEL_ON_DELAY = const(0X42)
# _INTCFG_CLKOUT_EN = const(0X01)
# _INTCFG_I2C_BYPASS_EN = const(0X11)
# _INTCFG_FSYNC_INT_EN = const(0X21)
# _INTCFG_FSYNC_INT_LEVEL = const(0X31)
_INTCFG_INT_RD_CLEAR = const(0X41)
_INTCFG_LATCH_INT_EN = const(0X51)
_INTCFG_INT_OPEN = const(0X61)
_INTCFG_INT_LEVEL = const(0X71)
# _USERCTRL_SIG_COND_RESET = const(0X01)
# _USERCTRL_I2C_MST_RESET = const(0X11)
# _USERCTRL_FIFO_RESET = const(0X21)
# _USERCTRL_DMP_RESET = const(0X31)
# _USERCTRL_I2C_IF_DIS = const(0X41)
# _USERCTRL_I2C_MST_EN = const(0X51)
_USERCTRL_FIFO_EN = const(0X61)
# _USERCTRL_DMP_EN = const(0X71)
_PWR1_CLKSEL = const(0X03)
# _PWR1_TEMP_DIS = const(0X31)
# _PWR1_CYCLE = const(0X51)
_PWR1_SLEEP = const(0X61)
_PWR1_DEVICE_RESET = const(0X71)
# _PWR2_STBY_ZG = const(0X01)
# _PWR2_STBY_YG = const(0X11)
# _PWR2_STBY_XG = const(0X21)
# _PWR2_STBY_ZA = const(0X31)
# _PWR2_STBY_YA = const(0X41)
# _PWR2_STBY_XA = const(0X51)
# _PWR2_LP_WAKE_CTRL = const(0X62)
# _BANKSEL_MEM_SEL = const(0X05)
# _BANKSEL_CFG_USER_BANK = const(0X51)
# _BANKSEL_PRFTCH_EN = const(0X61)
# _WHO_AM_I = const(0X06)
#Values, not currently in use.
# _DLPF_BW_256 = const(0)
# _DLPF_BW_188 = const(1)
# _DLPF_BW_98 = const(2)
# _DLPF_BW_42 = const(3)
# _DLPF_BW_20 = const(4)
# _DLPF_BW_10 = const(5)
# _DLPF_BW_5 = const(6)
# _DHPF_RESET = const(0)
# _DHPF_5 = const(1)
# _DHPF_2P5 = const(2)
# _DHPF_1P25 = const(3)
# _DHPF_0P63 = const(4)
# _DHPF_HOLD = const(7)
_GYRO_FS_250 = const(0)
# _GYRO_FS_500 = const(1)
# _GYRO_FS_1000 = const(2)
# _GYRO_FS_2000 = const(3)
#
_ACCEL_FS_2 = const(0)
# _ACCEL_FS_4 = const(1)
# _ACCEL_FS_8 = const(2)
# _ACCEL_FS_16 = const(3)
#
#
# _CLOCK_DIV_348 = const(0x0)
# _CLOCK_DIV_333 = const(0x1)
# _CLOCK_DIV_320 = const(0x2)
# _CLOCK_DIV_308 = const(0x3)
# _CLOCK_DIV_296 = const(0x4)
# _CLOCK_DIV_286 = const(0x5)
# _CLOCK_DIV_276 = const(0x6)
# _CLOCK_DIV_267 = const(0x7)
# _CLOCK_DIV_258 = const(0x8)
# _CLOCK_DIV_500 = const(0x9)
# _CLOCK_DIV_471 = const(0xA)
# _CLOCK_DIV_444 = const(0xB)
# _CLOCK_DIV_421 = const(0xC)
# _CLOCK_DIV_400 = const(0xD)
# _CLOCK_DIV_381 = const(0xE)
# _CLOCK_DIV_364 = const(0xF)
#
# _INTMODE_ACTIVEHIGH = const(0)
# _INTMODE_ACTIVELOW = const(1)
#
# _INTDRV_PUSHPULL = const(0)
# _INTDRV_OPENDRAIN = const(1)
#
# _INTLATCH_50USPULSE = const(0)
# _INTLATCH_WAITCLEAR = const(1)
#
# _INTCLEAR_STATUSREAD = const(0)
# _INTCLEAR_ANYREAD = const(1)
#
# _DETECT_DECREMENT_RESET = const(0)
# _DETECT_DECREMENT_1 = const(1)
# _DETECT_DECREMENT_2 = const(2)
# _DETECT_DECREMENT_4 = const(3)
#
# _WAKE_FREQ_1P25 = const(0)
# _WAKE_FREQ_2P5 = const(1)
# _WAKE_FREQ_5 = const(2)
# _WAKE_FREQ_10 = const(3)
#
# _DMP_MEMORY_BANKS = const(8)
# _DMP_MEMORY_BANK_SIZE = const(256)
# _DMP_MEMORY_CHUNK_SIZE = const(16)
# _VDDIO_LEVEL_VLOGIC = const(0)
# _VDDIO_LEVEL_VDD = const(1)
# _EXT_SYNC_DISABLED = const(0)
# _EXT_SYNC_TEMP_OUT_L = const(1)
# _EXT_SYNC_GYRO_XOUT_L = const(2)
# _EXT_SYNC_GYRO_YOUT_L = const(3)
# _EXT_SYNC_GYRO_ZOUT_L = const(4)
# _EXT_SYNC_ACCEL_XOUT_L = const(5)
# _EXT_SYNC_ACCEL_YOUT_L = const(6)
# _EXT_SYNC_ACCEL_ZOUT_L = const(7)
# _CLOCK_INTERNAL = const(0)
_CLOCK_PLL_XGYRO = const(1)
# _CLOCK_PLL_YGYRO = const(2)
# _CLOCK_PLL_ZGYRO = const(3)
# _CLOCK_PLL_EXT32K = const(4)
# _CLOCK_PLL_EXT19M = const(5)
# _CLOCK_KEEP_RESET = const(7)
def __init__( self, aLoc, aAddress = _ADDRESS_LOW ) :
"""aLoc I2C pin location is either 1, 'X', 2 or 'Y'.
aAddress is either ADDRESS_LOW or ADDRESS_HIGH."""
if 1 > aLoc > 2 :
raise Exception("aLoc must be 1 or 2.")
self._data = bytearray(1)
self._data6 = bytearray(6)
self._data14 = bytearray(14)
self._address = aAddress
self._i2c = pyb.I2C(aLoc, pyb.I2C.MASTER, baudrate = 400000)
self.setclocksource(CLOCK_PLL_XGYRO)
self.setfullscalegyrorange(GYRO_FS_250)
self.setfulscaleaccelrange(ACCEL_FS_2)
self.setclocksource(_CLOCK_PLL_XGYRO)
self.setfullscalegyrorange(_GYRO_FS_250)
self.setfulscaleaccelrange(_ACCEL_FS_2)
self.setsleepenabled(False)
def getrate( self ) :
self._readdata(RA_SMPLRT_DIV, self._data)
self._readdata(_RA_SMPLRT_DIV, self._data)
return self._data[0]
def setrate( self, aRate ) :
self._writedata(RA_SMPLRT_DIV, aRate)
self._writedata(_RA_SMPLRT_DIV, aRate)
def getDLPF( self ) :
return self._readbits(RA_CONFIG, CFG_DLPF_CFG_BIT, CFG_DLPF_CFG_LENGTH)
return self._readbits(_RA_CONFIG, _CFG_DLPF_CFG)
def setDLPF( self, aMode ) :
self._writebits(RA_CONFIG, CFG_DLPF_CFG_BIT, CFG_DLPF_CFG_LENGTH, aMode)
self._writebits(_RA_CONFIG, _CFG_DLPF_CFG, aMode)
def setclocksource( self, aSource ) :
self._writebits(RA_PWR_MGMT_1, PWR1_CLKSEL_BIT, PWR1_CLKSEL_LENGTH, aSource)
self._writebits(_RA_PWR_MGMT_1, _PWR1_CLKSEL, aSource)
def getfullscalegyrorange( self ) :
return self._readbits(RA_GYRO_CONFIG, GCONFIG_FS_SEL_BIT, GCONFIG_FS_SEL_LENGTH)
return self._readbits(_RA_GYRO_CONFIG, _GCONFIG_FS_SEL)
def setfullscalegyrorange( self, aRange ) :
self._writebits(RA_GYRO_CONFIG, GCONFIG_FS_SEL_BIT, GCONFIG_FS_SEL_LENGTH, aRange)
self._writebits(_RA_GYRO_CONFIG, _GCONFIG_FS_SEL, aRange)
def getfullscaleaccelrange( self ) :
self._readbits(RA_ACCEL_CONFIG, ACONFIG_AFS_SEL_BIT, ACONFIG_AFS_SEL_LENGTH)
self._readbits(_RA_ACCEL_CONFIG, _ACONFIG_AFS_SEL)
def setfullscaleaccelrange( self, aRange ) :
self._writebits(RA_ACCEL_CONFIG, ACONFIG_AFS_SEL_BIT, ACONFIG_AFS_SEL_LENGTH, aRange)
self._writebits(_RA_ACCEL_CONFIG, _ACONFIG_AFS_SEL, aRange)
def getsleepenabled( self ) :
self._readbits(RA_PWR_MGMT_1, PWR1_SLEEP_BIT, 1)
self._readbits(_RA_PWR_MGMT_1, _PWR1_SLEEP)
def setsleepenabled( self, aTF ) :
self._writebits(RA_PWR_MGMT_1, PWR1_SLEEP_BIT, 1, aTF)
self._writebits(_RA_PWR_MGMT_1, _PWR1_SLEEP, aTF)
def getfreefalldetectionthreshold( self ) :
self._readdata(RA_FF_THR, self._data)
self._readdata(_RA_FF_THR, self._data)
return self._data[0]
def setfreefalldetectionthreshold( self, aValue ) :
self._writedata(RA_FF_THR, aValue)
self._writedata(_RA_FF_THR, aValue)
def getfreefalldetectionduration( self ) :
self._readdata(RA_FF_DUR, self._data)
self._readdata(_RA_FF_DUR, self._data)
return self._data[0]
def setfreefalldetectionduration( self, aValue ) :
self._writedata(RA_FF_DUR, aValue)
self._writedata(_RA_FF_DUR, aValue)
def getmotiondetectionthreshold( self ) :
self._readdata(RA_MOT_THR, self._data)
self._readdata(_RA_MOT_THR, self._data)
return self._data[0]
def setmotiondetectionthreshold( self, aValue ) :
self._writedata(RA_MOT_THR, aValue)
self._writedata(_RA_MOT_THR, aValue)
def getmotiondetectionduration( self ) :
self._readdata(RA_MOT_DUR, self._data)
self._readdata(_RA_MOT_DUR, self._data)
return self._data[0]
def setmotiondetectionduration( self, aValue ) :
self._writedata(RA_MOT_DUR, aValue)
self._writedata(_RA_MOT_DUR, aValue)
def getzeromotiondetectionthreshold( self ) :
self._readdata(RA_ZRMOT_THR, self._data)
self._readdata(_RA_ZRMOT_THR, self._data)
return self._data[0]
def setzeromotiondetectionthreshold( self, aValue ) :
self._writedata(RA_ZRMOT_THR, aValue)
self._writedata(_RA_ZRMOT_THR, aValue)
def getzeromotiondetectionduration( self ) :
self._readdata(RA_ZRMOT_DUR, self._data)
self._readdata(_RA_ZRMOT_DUR, self._data)
return self._data[0]
def setzeromotiondetectionduration( self, aValue ) :
self._writedata(RA_ZRMOT_DUR, aValue)
self._writedata(_RA_ZRMOT_DUR, aValue)
def getFIFOenabled( self ) :
return self._readbits(RA_USER_CTRL, USERCTRL_FIFO_EN_BIT, 1)
return self._readbits(_RA_USER_CTRL, _USERCTRL_FIFO_EN)
def setFIFFOenabled( self, aTF ) :
self._writebits(RA_USER_CTRL, USERCTRL_FIFO_EN_BIT, 1, aTF)
def setFIFOenabled( self, aTF ) :
self._writebits(_RA_USER_CTRL, _USERCTRL_FIFO_EN, aTF)
def gettempFIFOenabled( self ) :
return self._readbits(RA_FIFO_EN, TEMP_FIFO_EN_BIT, 1)
return self._readbits(_RA_FIFO_EN, _TEMP_FIFO_EN)
def settempFIFFOenabled( self, aTF ) :
self._writebits(RA_FIFO_EN, TEMP_FIFO_EN_BIT, 1, aTF)
self._writebits(_RA_FIFO_EN, _TEMP_FIFO_EN, aTF)
def getxgyroFIFOenabled( self ) :
return self._readbits(RA_FIFO_EN, XG_FIFO_EN_BIT, 1)
return self._readbits(_RA_FIFO_EN, _XG_FIFO_EN)
def setxgyroFIFOenabled( self, aTF ) :
self._writebits(RA_FIFO_EN, XG_FIFO_EN_BIT, 1, aTF)
self._writebits(_RA_FIFO_EN, _XG_FIFO_EN, aTF)
def getygyroFIFOenabled( self ) :
return self._readbits(RA_FIFO_EN, YG_FIFO_EN_BIT, 1)
return self._readbits(_RA_FIFO_EN, _YG_FIFO_EN)
def setygyroFIFOenabled( self, aTF ) :
self._writebits(RA_FIFO_EN, YG_FIFO_EN_BIT, 1, aTF)
self._writebits(_RA_FIFO_EN, _YG_FIFO_EN, aTF)
def getzgyroFIFOenabled( self ) :
return self._readbits(RA_FIFO_EN, ZG_FIFO_EN_BIT, 1)
return self._readbits(_RA_FIFO_EN, _ZG_FIFO_EN)
def setzgyroFIFOenabled( self, aTF ) :
self._writebits(RA_FIFO_EN, ZG_FIFO_EN_BIT, 1, aTF)
self._writebits(_RA_FIFO_EN, _ZG_FIFO_EN, aTF)
def getaccelFIFOenabled( self ) :
return self._readbits(RA_FIFO_EN, ACCEL_FIFO_EN_BIT, 1)
return self._readbits(_RA_FIFO_EN, _ACCEL_FIFO_EN)
def setaccelFIFOenabled( self, aTF ) :
self._writebits(RA_FIFO_EN, ACCEL_FIFO_EN_BIT, 1, aTF)
self._writebits(_RA_FIFO_EN, _ACCEL_FIFO_EN, aTF)
def getmasterclockspeed( self ) :
return self._readbits(RA_I2C_MST_CTRL, I2C_MST_CLK_BIT, I2C_MST_CLK_LENGTH)
return self._readbits(_RA_I2C_MST_CTRL, _I2C_MST_CLK)
def setmasterclockspeed( self, aValue ) :
self._writebits(RA_I2C_MST_CTRL, I2C_MST_CLK_BIT, I2C_MST_CLK_LENGTH, aValue)
self._writebits(_RA_I2C_MST_CTRL, _I2C_MST_CLK, aValue)
def getinterruptmode( self ) :
return self._readbits(RA_INT_PIN_CFG, INTCFG_INT_LEVEL_BIT, 1)
return self._readbits(_RA_INT_PIN_CFG, _INTCFG_INT_LEVEL)
def setinterruptmode( self, aValue ) :
self._writebits(RA_INT_PIN_CFG, INTCFG_INT_LEVEL_BIT, 1, aValue)
self._writebits(_RA_INT_PIN_CFG, _INTCFG_INT_LEVEL, aValue)
def getinterruptdrive( self ) :
return self._readbits(RA_INT_PIN_CFG, INTCFG_INT_OPEN_BIT, 1)
return self._readbits(_RA_INT_PIN_CFG, _INTCFG_INT_OPEN)
def setinterruptdrive( self, aValue ) :
self._writebits(RA_INT_PIN_CFG, INTCFG_INT_OPEN_BIT, 1, aValue)
self._writebits(_RA_INT_PIN_CFG, _INTCFG_INT_OPEN, aValue)
def getinterruptlatch( self ) :
return self._readbits(RA_INT_PIN_CFG, INTCFG_LATCH_INT_EN_BIT, 1)
return self._readbits(_RA_INT_PIN_CFG, _INTCFG_LATCH_INT_EN)
def setinterruptlatch( self, aValue ) :
self._writebits(RA_INT_PIN_CFG, INTCFG_LATCH_INT_EN_BIT, 1, aValue)
self._writebits(_RA_INT_PIN_CFG, _INTCFG_LATCH_INT_EN, aValue)
def getinterruptlatchclear( self ) :
return self._readbits(RA_INT_PIN_CFG, INTCFG_INT_RD_CLEAR_BIT, 1)
return self._readbits(_RA_INT_PIN_CFG, _INTCFG_INT_RD_CLEAR)
def setinterruptlatchclear( self, aValue ) :
self._writebits(RA_INT_PIN_CFG, INTCFG_INT_RD_CLEAR_BIT, 1, aValue)
self._writebits(_RA_INT_PIN_CFG, _INTCFG_INT_RD_CLEAR, aValue)
def getacceltemprot( self ) :
self._readdata(RA_ACCEL_XOUT_H, self._data14)
self._readdata(_RA_ACCEL_XOUT_H, self._data14)
return [(self._data14[i] << 8) | self._data14[i + 1] for i in range(0, len(self._data14), 2)]
def getacceleration( self ) :
self._readdata(RA_ACCEL_XOUT_H, self._data6)
self._readdata(_RA_ACCEL_XOUT_H, self._data6)
return [(self._data6[i] << 8) | self._data6[i + 1] for i in range(0, len(self._data6), 2)]
def getrotation( self ) :
self._readdata(RA_GYRO_XOUT_H, self.data6)
self._readdata(_RA_GYRO_XOUT_H, self.data6)
return [(self._data6[i] << 8) | self._data6[i + 1] for i in range(0, len(self._data6), 2)]
def reset( self ) :
self._writebits(RA_PWR_MGMT_1, PWR1_DEVICE_RESET_BIT, 1, True)
self._writebits(_RA_PWR_MGMT_1, _PWR1_DEVICE_RESET, True)
def _writedata( self, aAddress, aData ) :
self._i2c.mem_write(aData, self._address, aAddress)
@ -549,16 +544,13 @@ class accel(object) :
self._i2c.mem_read(aData, self._address, aAddress)
# @micropython.native
def _readbits( self, aAddress, aStart, aLen ) :
def _readbits( self, aAddress, aPosBits ) :
self._readdata(aAddress, self._data)
b = (self._data[0] >> (aStart - aLen + 1)) & ((1 << aLen) - 1)
return getvalue(self._data[0], aPosBits)
# @micropython.native
def _writebits( self, aAddress, aStart, aLen, aValue ) :
def _writebits( self, aAddress, aPosBits, aValue ) :
self._readdata(aAddress, self._data)
mask = ((1 << aLen) - 1) << (aStart - aLen + 1)
aValue = (buffer[0] << (aStart - aLen + 1)) & mask #shift data into correct position
val &= ~mask
self._data[0] |= aValue
self._data[0] = setvalue(self._data[0], aPosBits, aValue)
self._writedata(aAddress, self._data)

Wyświetl plik

@ -5,9 +5,6 @@ class irdistance(object):
""" Driver for Sharp Gp2y0a IR distance sensor. The distance
range is around 3 to 40 inches. """
maxinches = 31.5 #Maximun range of IR board in inches.
_v2i = -1.02 #Voltage to inches power.
def __init__( self, pin ) :
"""pin may be name or pin object. It must be able to handle ADC input."""
@ -25,8 +22,10 @@ class irdistance(object):
@property
def inches( self ) :
#distance / 204.8? Why?
volts = self.distance * 0.0048828125
return 65.0 * pow(volts, IRDistance._v2i)
#inches = v^-1.02.
return 65.0 * pow(volts, -1.02)
@property
def centimeters( self ) : return self.inches * 2.54

Wyświetl plik

@ -1,12 +1,11 @@
# JY-MCU Bluetooth board ----------------------------------------
from pyb import UART, repl_uart, udelay
# JY-MCU Bluetooth board ----------------------------------------
# This opens connection with Bluetooth module connected to Y1, Y2 (UART 6)
# Then it sets the repl output to this UART.
#COMMANDS AT - does nothing but get an ok.
# The posible baudrates are:
# The possible baudrates are:
# AT+BAUD1-------1200
# AT+BAUD2-------2400
# AT+BAUD3-------4800

Wyświetl plik

@ -3,13 +3,13 @@
import pyb
class pir(object):
"""Passive Infrared Motion Sensor driver. Supports on/off through an output pin
trigger reading as well as an interrupt callback on trigger high/low changes."""
'''Passive Infrared Motion Sensor driver. Supports on/off through an output pin
trigger reading as well as an interrupt callback on trigger high/low changes.'''
def __init__(self, power, trigger, callback = None):
"""Power and trigger pins, optional interrupt callback in the format
'''Power and trigger pins, optional interrupt callback in the format
of fun( bOnOff ). This will be called whenever the trigger state
changes."""
changes.'''
if power != None:
self._power = pyb.Pin(power, pyb.Pin.OUT_PP)
self._power.low()
@ -19,7 +19,7 @@ class pir(object):
self.interrupt = callback
def _onoff( self, aTF ) :
"""Turn device on/off"""
'''Turn device on/off'''
if (self._power != None):
if (aTF):
oldon = self.inton

Wyświetl plik

@ -3,7 +3,7 @@
from pyb import Timer, Pin
class pwm(object):
"""docstring for PWMM"""
"""Class to help with PWM pin and clock initialization."""
#Dict pin name: timer #, channel #.
PinChannels = {
@ -48,15 +48,19 @@ class pwm(object):
raise pwm.PWMException("Pin {} cannot use timer {}".format(pinname, timernum))
def __init__( self, p, timernum, afreq = 100 ) :
isname = type(p) == str
pinname = p if isname else p.name()
def __init__( self, aPin, timernum, afreq = 100 ) :
'''aPin may be a pyb.Pin or a pin name.
timernum must be a number corresponding to the given pin.
afreq is the frequency for the PWM signal.
'''
isname = type(aPin) == str
pinname = aPin if isname else aPin.name()
timernum, channel = pwm.timerandchannel(pinname, timernum)
if isname:
p = Pin(pinname)
aPin = Pin(pinname)
self._timer = Timer(timernum, freq = afreq)
self._channel = self._timer.channel(channel, Timer.PWM, pin = p)
self._channel = self._timer.channel(channel, Timer.PWM, pin = aPin)
@property
def pulse_width( self ) : return self._channel.pulse_width()

Wyświetl plik

@ -1,15 +1,18 @@
from pyb import Pin, Timer, udelay
# WARNING: Do not use PA4-X5 or PA5-X6 as the echo pin without a 1k resistor.
# WARNING: Should place a 1k resistor on the echo pin.
class sr04distance(object):
""" """
"""Driver for SR05 sonic distance sensor. """
maxinches = 20 #maximum range of SR04.
# _MAXINCHES = const(20) #maximum range of SR04.
def __init__( self, tpin, epin, timer=2 ) :
""" """
def __init__( self, tpin, epin, timer = 2 ) :
'''tpin = Timer pin.
epin = Echo pin.
timer = Timer #.
'''
if type(tpin) == str:
self._tpin = Pin(tpin, Pin.OUT_PP, Pin.PULL_NONE)
@ -28,19 +31,22 @@ class sr04distance(object):
raise Exception("echo pin must be pin name or pyb.Pin configured for input.")
# Create a microseconds counter.
self._micros = Timer(timer, prescaler=83, period=0x3fffffff)
self._micros = Timer(timer, prescaler = 83, period = 0x3fffffff)
def __del__( self ) :
self._micros.deinit()
@property
def counter( self ) : return self._micros.counter()
def counter( self ) :
return self._micros.counter()
@counter.setter
def counter( self, value ) : self._micros.counter(value)
def counter( self, value ) :
self._micros.counter(value)
@property
def centimeters( self ) :
'''Get # of centimeters distance sensor is reporting.'''
start = 0
end = 0
@ -64,8 +70,10 @@ class sr04distance(object):
# Calc the duration of the recieved pulse, divide the result by
# 2 (round-trip) and divide it by 29 (the speed of sound is
# 340 m/s and that is 29 us/cm).
return (end - start) / 58
return (end - start) / 58.0
@property
def inches( self ) : return self.centimeters * 0.3937
def inches( self ) :
#Get distance in inches.
return self.centimeters * 0.3937

Wyświetl plik

@ -1,28 +1,12 @@
#driver for Sainsmart 1.8" tft display ST7735
#Driver for Sainsmart 1.8" tft display ST7735
#Translated by Guy Carver from the ST7735 sample code.
#Display uses SPI interface.
#todo: Use const()
import pyb
from math import sqrt
#TFTRotations and TFTRGB are bits to set
# on MADCTL to control display rotation/color layout
#Looking at display with pins on top.
#00 = upper left printing right
#10 = does nothing (MADCTL_ML)
#20 = upper left printing down (backwards) (Vertical flip)
#40 = upper right printing left (backwards) (X Flip)
#80 = lower left printing right (backwards) (Y Flip)
#04 = (MADCTL_MH)
#60 = 90 right rotation
#C0 = 180 right rotation
#A0 = 270 right rotation
TFTRotations = [0x00, 0x60, 0xC0, 0xA0]
TFTBGR = 0x08 #When set color is bgr else rgb.
TFTRGB = 0x00
@micropython.native
def clamp( aValue, aMin, aMax ) :
return max(aMin, min(aMax, aValue))
@ -33,68 +17,85 @@ def TFTColor( aR, aG, aB ) :
This assumes rgb 565 layout and will be incorrect for bgr.'''
return ((aR & 0xF8) << 8) | ((aG & 0xFC) << 3) | (aB >> 3)
ScreenSize = (128, 160)
class tft(object) :
"""Sainsmart tft-7735 display driver."""
'''Sainsmart tft-7735 display driver.'''
NOP = 0x0
SWRESET = 0x01
RDDID = 0x04
RDDST = 0x09
#TFTRotations and TFTRGB are bits to set
# on MADCTL to control display rotation/color layout
#Looking at display with pins on top.
#00 = upper left printing right
#10 = does nothing (MADCTL_ML)
#20 = upper left printing down (backwards) (Vertical flip)
#40 = upper right printing left (backwards) (X Flip)
#80 = lower left printing right (backwards) (Y Flip)
#04 = (MADCTL_MH)
#60 = 90 right rotation
#C0 = 180 right rotation
#A0 = 270 right rotation
_TFTRotations = [0x00, 0x60, 0xC0, 0xA0]
_TFTBGR = const(0x08) #When set color is bgr else rgb.
_TFTRGB = const(0x00)
SLPIN = 0x10
SLPOUT = 0x11
PTLON = 0x12
NORON = 0x13
_NOP = const(0x0)
_SWRESET = const(0x01)
_RDDID = const(0x04)
_RDDST = const(0x09)
INVOFF = 0x20
INVON = 0x21
DISPOFF = 0x28
DISPON = 0x29
CASET = 0x2A
RASET = 0x2B
RAMWR = 0x2C
RAMRD = 0x2E
_SLPIN = const(0x10)
_SLPOUT = const(0x11)
_PTLON = const(0x12)
_NORON = const(0x13)
COLMOD = 0x3A
MADCTL = 0x36
_INVOFF = const(0x20)
_INVON = const(0x21)
_DISPOFF = const(0x28)
_DISPON = const(0x29)
_CASET = const(0x2A)
_RASET = const(0x2B)
_RAMWR = const(0x2C)
_RAMRD = const(0x2E)
FRMCTR1 = 0xB1
FRMCTR2 = 0xB2
FRMCTR3 = 0xB3
INVCTR = 0xB4
DISSET5 = 0xB6
_COLMOD = const(0x3A)
_MADCTL = const(0x36)
PWCTR1 = 0xC0
PWCTR2 = 0xC1
PWCTR3 = 0xC2
PWCTR4 = 0xC3
PWCTR5 = 0xC4
VMCTR1 = 0xC5
_FRMCTR1 = const(0xB1)
_FRMCTR2 = const(0xB2)
_FRMCTR3 = const(0xB3)
_INVCTR = const(0xB4)
_DISSET5 = const(0xB6)
RDID1 = 0xDA
RDID2 = 0xDB
RDID3 = 0xDC
RDID4 = 0xDD
_PWCTR1 = const(0xC0)
_PWCTR2 = const(0xC1)
_PWCTR3 = const(0xC2)
_PWCTR4 = const(0xC3)
_PWCTR5 = const(0xC4)
_VMCTR1 = const(0xC5)
PWCTR6 = 0xFC
_RDID1 = const(0xDA)
_RDID2 = const(0xDB)
_RDID3 = const(0xDC)
_RDID4 = const(0xDD)
GMCTRP1 = 0xE0
GMCTRN1 = 0xE1
_PWCTR6 = const(0xFC)
BLACK = 0
RED = TFTColor(0xFF, 0x00, 0x00)
MAROON = TFTColor(0x80, 0x00, 0x00)
GREEN = TFTColor(0x00, 0xFF, 0x00)
FOREST = TFTColor(0x00, 0x80, 0x80)
BLUE = TFTColor(0x00, 0x00, 0xFF)
NAVY = TFTColor(0x00, 0x00, 0x80)
CYAN = TFTColor(0x00, 0xFF, 0xFF)
YELLOW = TFTColor(0xFF, 0xFF, 0x00)
PURPLE = TFTColor(0xFF, 0x00, 0xFF)
WHITE = TFTColor(0xFF, 0xFF, 0xFF)
GRAY = TFTColor(0x80, 0x80, 0x80)
_GMCTRP1 = const(0xE0)
_GMCTRN1 = const(0xE1)
_BLACK = 0
_RED = TFTColor(0xFF, 0x00, 0x00)
_MAROON = TFTColor(0x80, 0x00, 0x00)
_GREEN = TFTColor(0x00, 0xFF, 0x00)
_FOREST = TFTColor(0x00, 0x80, 0x80)
_BLUE = TFTColor(0x00, 0x00, 0xFF)
_NAVY = TFTColor(0x00, 0x00, 0x80)
_CYAN = TFTColor(0x00, 0xFF, 0xFF)
_YELLOW = TFTColor(0xFF, 0xFF, 0x00)
_PURPLE = TFTColor(0xFF, 0x00, 0xFF)
_WHITE = TFTColor(0xFF, 0xFF, 0xFF)
_GRAY = TFTColor(0x80, 0x80, 0x80)
_SCREENSIZE = (128, 160)
@staticmethod
def color( aR, aG, aB ) :
@ -102,9 +103,9 @@ class tft(object) :
return TFTColor(aR, aG, aB)
def __init__( self, aLoc, aDC, aReset ) :
"""aLoc SPI pin location is either 1 for 'X' or 2 for 'Y'.
aDC is the DC pin and aReset is the reset pin."""
self._size = ScreenSize
'''aLoc SPI pin location is either 1 for 'X' or 2 for 'Y'.
aDC is the DC pin and aReset is the reset pin.'''
self._size = tft._SCREENSIZE
self.rotate = 0 #Vertical with top toward pins.
self._rgb = True #color order of rgb.
self.dc = pyb.Pin(aDC, pyb.Pin.OUT_PP, pyb.Pin.PULL_DOWN)
@ -120,23 +121,19 @@ class tft(object) :
def size( self ) :
return self._size
# @micropython.native
def on( self, aTF = True ) :
'''Turn display on or off.'''
self._writecommand(tft.DISPON if aTF else tft.DISPOFF)
self._writecommand(_DISPON if aTF else _DISPOFF)
# @micropython.native
def invertcolor( self, aBool ) :
'''Invert the color data IE: Black = White.'''
self._writecommand(tft.INVON if aBool else tft.INVOFF)
self._writecommand(_INVON if aBool else _INVOFF)
# @micropython.native
def rgb( self, aTF = True ) :
'''True = rgb else bgr'''
self._rgb = aTF
self._setMADCTL()
# @micropython.native
def rotation( self, aRot ) :
'''0 - 3. Starts vertical with top toward pins and rotates 90 deg
clockwise each step.'''
@ -156,7 +153,6 @@ class tft(object) :
self._setwindowpoint(aPos)
self._pushcolor(aColor)
# @micropython.native
def text( self, aPos, aString, aColor, aFont, aSize = 1 ) :
'''Draw a text at the given position. If the string reaches the end of the
display it is wrapped to aPos[0] on the next line. aSize may be an integer
@ -183,7 +179,6 @@ class tft(object) :
py += aFont["Height"] * wh[1] + 1
px = aPos[0]
# @micropython.native
def char( self, aPos, aChar, aColor, aFont, aSizes ) :
'''Draw a character at the given position using the given font and color.
aSizes is a tuple with x, y as integer scales indicating the
@ -222,7 +217,6 @@ class tft(object) :
c >>= 1
px += aSizes[0]
# @micropython.native
def line( self, aStart, aEnd, aColor ) :
'''Draws a line from aStart to aEnd in the given color. Vertical or horizontal
lines are forwarded to vline and hline.'''
@ -267,7 +261,6 @@ class tft(object) :
e += dx
py += iny
# @micropython.native
def vline( self, aStart, aLen, aColor ) :
'''Draw a vertical line from aStart for aLen. aLen may be negative.'''
start = (clamp(aStart[0], 0, self._size[0]), clamp(aStart[1], 0, self._size[1]))
@ -278,7 +271,6 @@ class tft(object) :
self._setwindowloc(start, stop)
self._draw(aLen, aColor)
# @micropython.native
def hline( self, aStart, aLen, aColor ) :
'''Draw a horizontal line from aStart for aLen. aLen may be negative.'''
start = (clamp(aStart[0], 0, self._size[0]), clamp(aStart[1], 0, self._size[1]))
@ -289,7 +281,6 @@ class tft(object) :
self._setwindowloc(start, stop)
self._draw(aLen, aColor)
# @micropython.native
def rect( self, aStart, aSize, aColor ) :
'''Draw a hollow rectangle. aStart is the smallest coordinate corner
and aSize is a tuple indicating width, height.'''
@ -298,7 +289,6 @@ class tft(object) :
self.vline(aStart, aSize[1], aColor)
self.vline((aStart[0] + aSize[0] - 1, aStart[1]), aSize[1], aColor)
# @micropython.native
def fillrect( self, aStart, aSize, aColor ) :
'''Draw a filled rectangle. aStart is the smallest coordinate corner
and aSize is a tuple indicating width, height.'''
@ -318,7 +308,6 @@ class tft(object) :
numPixels = (end[0] - start[0] + 1) * (end[1] - start[1] + 1)
self._draw(numPixels, aColor)
# @micropython.native
def circle( self, aPos, aRadius, aColor ) :
'''Draw a hollow circle with the given radius and color with aPos as center.'''
self.colorData[0] = aColor >> 8
@ -353,7 +342,6 @@ class tft(object) :
self._setwindowpoint((xyn, yxn))
self._writedata(self.colorData)
# @micropython.native
def fillcircle( self, aPos, aRadius, aColor ) :
'''Draw a filled circle with given radius and color with aPos as center'''
rsq = aRadius * aRadius
@ -371,7 +359,6 @@ class tft(object) :
'''Fill screen with the given color.'''
self.fillrect((0, 0), self._size, aColor)
# @micropython.native
def _draw( self, aPixels, aColor ) :
'''Send given color to the device aPixels times.'''
self.colorData[0] = aColor >> 8
@ -383,40 +370,38 @@ class tft(object) :
self.spi.send(self.colorData)
self.cs.high()
# @micropython.native
def _setwindowpoint( self, aPos ) :
'''Set a single point for drawing a color to.'''
x = int(aPos[0])
y = int(aPos[1])
self._writecommand(tft.CASET) #Column address set.
self._writecommand(_CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = x
self.windowLocData[2] = 0x00
self.windowLocData[3] = x
self._writedata(self.windowLocData)
self._writecommand(tft.RASET) #Row address set.
self._writecommand(_RASET) #Row address set.
self.windowLocData[1] = y
self.windowLocData[3] = y
self._writedata(self.windowLocData)
self._writecommand(tft.RAMWR) #Write to RAM.
self._writecommand(_RAMWR) #Write to RAM.
# @micropython.native
def _setwindowloc( self, aPos0, aPos1 ) :
'''Set a rectangular area for drawing a color to.'''
self._writecommand(tft.CASET) #Column address set.
self._writecommand(_CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = int(aPos0[0])
self.windowLocData[2] = 0x00
self.windowLocData[3] = int(aPos1[0])
self._writedata(self.windowLocData)
self._writecommand(tft.RASET) #Row address set.
self._writecommand(_RASET) #Row address set.
self.windowLocData[1] = int(aPos0[1])
self.windowLocData[3] = int(aPos1[1])
self._writedata(self.windowLocData)
self._writecommand(tft.RAMWR) #Write to RAM.
self._writecommand(_RAMWR) #Write to RAM.
@micropython.native
def _writecommand( self, aCommand ) :
@ -445,9 +430,9 @@ class tft(object) :
@micropython.native
def _setMADCTL( self ) :
'''Set screen rotation and RGB/BGR format.'''
self._writecommand(tft.MADCTL)
rgb = TFTRGB if self._rgb else TFTBGR
self._writedata(TFTRotations[self.rotate] | rgb)
self._writecommand(_MADCTL)
rgb = _TFTRGB if self._rgb else _TFTBGR
self._writedata(tft._TFTRotations[self.rotate] | rgb)
@micropython.native
def _reset( self ) :
@ -460,306 +445,306 @@ class tft(object) :
self.reset.high()
pyb.delay(500)
def initb( self ) :
'''Initialize blue tab version.'''
self._size = (ScreenSize[0] + 2, ScreenSize[1] + 1)
self._reset()
self._writecommand(tft.SWRESET) #Software reset.
pyb.delay(50)
self._writecommand(tft.SLPOUT) #out of sleep mode.
pyb.delay(500)
data1 = bytearray(1)
self._writecommand(tft.COLMOD) #Set color mode.
data1[0] = 0x05 #16 bit color.
self._writedata(data1)
pyb.delay(10)
data3 = bytearray([0x00, 0x06, 0x03]) #fastest refresh, 6 lines front, 3 lines back.
self._writecommand(tft.FRMCTR1) #Frame rate control.
self._writedata(data3)
pyb.delay(10)
self._writecommand(tft.MADCTL)
data1[0] = 0x08 #row address/col address, bottom to top refresh
self._writedata(data1)
data2 = bytearray(2)
self._writecommand(tft.DISSET5) #Display settings
data2[0] = 0x15 #1 clock cycle nonoverlap, 2 cycle gate rise, 3 cycle oscil, equalize
data2[1] = 0x02 #fix on VTL
self._writedata(data2)
self._writecommand(tft.INVCTR) #Display inversion control
data1[0] = 0x00 #Line inversion.
self._writedata(data1)
self._writecommand(tft.PWCTR1) #Power control
data2[0] = 0x02 #GVDD = 4.7V
data2[1] = 0x70 #1.0uA
self._writedata(data2)
pyb.delay(10)
self._writecommand(tft.PWCTR2) #Power control
data1[0] = 0x05 #VGH = 14.7V, VGL = -7.35V
self._writedata(data1)
self._writecommand(tft.PWCTR3) #Power control
data2[0] = 0x01 #Opamp current small
data2[1] = 0x02 #Boost frequency
self._writedata(data2)
self._writecommand(tft.VMCTR1) #Power control
data2[0] = 0x3C #VCOMH = 4V
data2[1] = 0x38 #VCOML = -1.1V
self._writedata(data2)
pyb.delay(10)
self._writecommand(tft.PWCTR6) #Power control
data2[0] = 0x11
data2[1] = 0x15
self._writedata(data2)
#These different values don't seem to make a difference.
# dataGMCTRP = bytearray([0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22, 0x1f,
# 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
dataGMCTRP = bytearray([0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29,
0x25, 0x2b, 0x39, 0x00, 0x01, 0x03, 0x10])
self._writecommand(tft.GMCTRP1)
self._writedata(dataGMCTRP)
# dataGMCTRN = bytearray([0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30,
# 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10])
dataGMCTRN = bytearray([0x03, 0x1d, 0x07, 0x06, 0x2e, 0x2c, 0x29, 0x2d, 0x2e,
0x2e, 0x37, 0x3f, 0x00, 0x00, 0x02, 0x10])
self._writecommand(tft.GMCTRN1)
self._writedata(dataGMCTRN)
pyb.delay(10)
self._writecommand(tft.CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = 2 #Start at column 2
self.windowLocData[2] = 0x00
self.windowLocData[3] = self._size[0] - 1
self._writedata(self.windowLocData)
self._writecommand(tft.RASET) #Row address set.
self.windowLocData[1] = 1 #Start at row 2.
self.windowLocData[3] = self._size[1] - 1
self._writedata(self.windowLocData)
self._writecommand(tft.NORON) #Normal display on.
pyb.delay(10)
self._writecommand(tft.RAMWR)
pyb.delay(500)
self._writecommand(tft.DISPON)
self.cs.high()
pyb.delay(500)
def initr( self ) :
'''Initialize a red tab version.'''
self._reset()
self._writecommand(tft.SWRESET) #Software reset.
pyb.delay(150)
self._writecommand(tft.SLPOUT) #out of sleep mode.
pyb.delay(500)
data3 = bytearray([0x01, 0x2C, 0x2D]) #fastest refresh, 6 lines front, 3 lines back.
self._writecommand(tft.FRMCTR1) #Frame rate control.
self._writedata(data3)
self._writecommand(tft.FRMCTR2) #Frame rate control.
self._writedata(data3)
data6 = bytearray([0x01, 0x2c, 0x2d, 0x01, 0x2c, 0x2d])
self._writecommand(tft.FRMCTR3) #Frame rate control.
self._writedata(data6)
pyb.delay(10)
data1 = bytearray(1)
self._writecommand(tft.INVCTR) #Display inversion control
data1[0] = 0x07 #Line inversion.
self._writedata(data1)
self._writecommand(tft.PWCTR1) #Power control
data3[0] = 0xA2
data3[1] = 0x02
data3[2] = 0x84
self._writedata(data3)
self._writecommand(tft.PWCTR2) #Power control
data1[0] = 0xC5 #VGH = 14.7V, VGL = -7.35V
self._writedata(data1)
data2 = bytearray(2)
self._writecommand(tft.PWCTR3) #Power control
data2[0] = 0x0A #Opamp current small
data2[1] = 0x00 #Boost frequency
self._writedata(data2)
self._writecommand(tft.PWCTR4) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0x2A #Boost frequency
self._writedata(data2)
self._writecommand(tft.PWCTR5) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0xEE #Boost frequency
self._writedata(data2)
self._writecommand(tft.VMCTR1) #Power control
data1[0] = 0x0E
self._writedata(data1)
self._writecommand(tft.INVOFF)
self._writecommand(tft.MADCTL) #Power control
data1[0] = 0xC8
self._writedata(data1)
self._writecommand(tft.COLMOD)
data1[0] = 0x05
self._writedata(data1)
self._writecommand(tft.CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = 0x00
self.windowLocData[2] = 0x00
self.windowLocData[3] = self._size[0] - 1
self._writedata(self.windowLocData)
self._writecommand(tft.RASET) #Row address set.
self.windowLocData[3] = self._size[1] - 1
self._writedata(self.windowLocData)
dataGMCTRP = bytearray([0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22, 0x1f,
0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
self._writecommand(tft.GMCTRP1)
self._writedata(dataGMCTRP)
dataGMCTRN = bytearray([0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30,
0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10])
self._writecommand(tft.GMCTRN1)
self._writedata(dataGMCTRN)
pyb.delay(10)
self._writecommand(tft.DISPON)
pyb.delay(100)
self._writecommand(tft.NORON) #Normal display on.
pyb.delay(10)
self.cs.high()
# def initb( self ) :
# '''Initialize blue tab version.'''
# self._size = (ScreenSize[0] + 2, ScreenSize[1] + 1)
# self._reset()
# self._writecommand(_SWRESET) #Software reset.
# pyb.delay(50)
# self._writecommand(_SLPOUT) #out of sleep mode.
# pyb.delay(500)
#
# data1 = bytearray(1)
# self._writecommand(_COLMOD) #Set color mode.
# data1[0] = 0x05 #16 bit color.
# self._writedata(data1)
# pyb.delay(10)
#
# data3 = bytearray([0x00, 0x06, 0x03]) #fastest refresh, 6 lines front, 3 lines back.
# self._writecommand(_FRMCTR1) #Frame rate control.
# self._writedata(data3)
# pyb.delay(10)
#
# self._writecommand(_MADCTL)
# data1[0] = 0x08 #row address/col address, bottom to top refresh
# self._writedata(data1)
#
# data2 = bytearray(2)
# self._writecommand(_DISSET5) #Display settings
# data2[0] = 0x15 #1 clock cycle nonoverlap, 2 cycle gate rise, 3 cycle oscil, equalize
# data2[1] = 0x02 #fix on VTL
# self._writedata(data2)
#
# self._writecommand(_INVCTR) #Display inversion control
# data1[0] = 0x00 #Line inversion.
# self._writedata(data1)
#
# self._writecommand(_PWCTR1) #Power control
# data2[0] = 0x02 #GVDD = 4.7V
# data2[1] = 0x70 #1.0uA
# self._writedata(data2)
# pyb.delay(10)
#
# self._writecommand(_PWCTR2) #Power control
# data1[0] = 0x05 #VGH = 14.7V, VGL = -7.35V
# self._writedata(data1)
#
# self._writecommand(_PWCTR3) #Power control
# data2[0] = 0x01 #Opamp current small
# data2[1] = 0x02 #Boost frequency
# self._writedata(data2)
#
# self._writecommand(_VMCTR1) #Power control
# data2[0] = 0x3C #VCOMH = 4V
# data2[1] = 0x38 #VCOML = -1.1V
# self._writedata(data2)
# pyb.delay(10)
#
# self._writecommand(_PWCTR6) #Power control
# data2[0] = 0x11
# data2[1] = 0x15
# self._writedata(data2)
#
# #These different values don't seem to make a difference.
## dataGMCTRP = bytearray([0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22, 0x1f,
## 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
# dataGMCTRP = bytearray([0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29,
# 0x25, 0x2b, 0x39, 0x00, 0x01, 0x03, 0x10])
# self._writecommand(_GMCTRP1)
# self._writedata(dataGMCTRP)
#
## dataGMCTRN = bytearray([0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30,
## 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10])
# dataGMCTRN = bytearray([0x03, 0x1d, 0x07, 0x06, 0x2e, 0x2c, 0x29, 0x2d, 0x2e,
# 0x2e, 0x37, 0x3f, 0x00, 0x00, 0x02, 0x10])
# self._writecommand(_GMCTRN1)
# self._writedata(dataGMCTRN)
# pyb.delay(10)
#
# self._writecommand(_CASET) #Column address set.
# self.windowLocData[0] = 0x00
# self.windowLocData[1] = 2 #Start at column 2
# self.windowLocData[2] = 0x00
# self.windowLocData[3] = self._size[0] - 1
# self._writedata(self.windowLocData)
#
# self._writecommand(_RASET) #Row address set.
# self.windowLocData[1] = 1 #Start at row 2.
# self.windowLocData[3] = self._size[1] - 1
# self._writedata(self.windowLocData)
#
# self._writecommand(_NORON) #Normal display on.
# pyb.delay(10)
#
# self._writecommand(_RAMWR)
# pyb.delay(500)
#
# self._writecommand(_DISPON)
# self.cs.high()
# pyb.delay(500)
#
# def initr( self ) :
# '''Initialize a red tab version.'''
# self._reset()
#
# self._writecommand(_SWRESET) #Software reset.
# pyb.delay(150)
# self._writecommand(_SLPOUT) #out of sleep mode.
# pyb.delay(500)
#
# data3 = bytearray([0x01, 0x2C, 0x2D]) #fastest refresh, 6 lines front, 3 lines back.
# self._writecommand(_FRMCTR1) #Frame rate control.
# self._writedata(data3)
#
# self._writecommand(_FRMCTR2) #Frame rate control.
# self._writedata(data3)
#
# data6 = bytearray([0x01, 0x2c, 0x2d, 0x01, 0x2c, 0x2d])
# self._writecommand(_FRMCTR3) #Frame rate control.
# self._writedata(data6)
# pyb.delay(10)
#
# data1 = bytearray(1)
# self._writecommand(_INVCTR) #Display inversion control
# data1[0] = 0x07 #Line inversion.
# self._writedata(data1)
#
# self._writecommand(_PWCTR1) #Power control
# data3[0] = 0xA2
# data3[1] = 0x02
# data3[2] = 0x84
# self._writedata(data3)
#
# self._writecommand(_PWCTR2) #Power control
# data1[0] = 0xC5 #VGH = 14.7V, VGL = -7.35V
# self._writedata(data1)
#
# data2 = bytearray(2)
# self._writecommand(_PWCTR3) #Power control
# data2[0] = 0x0A #Opamp current small
# data2[1] = 0x00 #Boost frequency
# self._writedata(data2)
#
# self._writecommand(_PWCTR4) #Power control
# data2[0] = 0x8A #Opamp current small
# data2[1] = 0x2A #Boost frequency
# self._writedata(data2)
#
# self._writecommand(_PWCTR5) #Power control
# data2[0] = 0x8A #Opamp current small
# data2[1] = 0xEE #Boost frequency
# self._writedata(data2)
#
# self._writecommand(_VMCTR1) #Power control
# data1[0] = 0x0E
# self._writedata(data1)
#
# self._writecommand(_INVOFF)
#
# self._writecommand(_MADCTL) #Power control
# data1[0] = 0xC8
# self._writedata(data1)
#
# self._writecommand(_COLMOD)
# data1[0] = 0x05
# self._writedata(data1)
#
# self._writecommand(_CASET) #Column address set.
# self.windowLocData[0] = 0x00
# self.windowLocData[1] = 0x00
# self.windowLocData[2] = 0x00
# self.windowLocData[3] = self._size[0] - 1
# self._writedata(self.windowLocData)
#
# self._writecommand(_RASET) #Row address set.
# self.windowLocData[3] = self._size[1] - 1
# self._writedata(self.windowLocData)
#
# dataGMCTRP = bytearray([0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22, 0x1f,
# 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
# self._writecommand(_GMCTRP1)
# self._writedata(dataGMCTRP)
#
# dataGMCTRN = bytearray([0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30,
# 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10])
# self._writecommand(_GMCTRN1)
# self._writedata(dataGMCTRN)
# pyb.delay(10)
#
# self._writecommand(_DISPON)
# pyb.delay(100)
#
# self._writecommand(_NORON) #Normal display on.
# pyb.delay(10)
#
# self.cs.high()
@micropython.native
def initg( self ) :
'''Initialize a green tab version.'''
self._reset()
self._writecommand(tft.SWRESET) #Software reset.
self._writecommand(_SWRESET) #Software reset.
pyb.delay(150)
self._writecommand(tft.SLPOUT) #out of sleep mode.
self._writecommand(_SLPOUT) #out of sleep mode.
pyb.delay(255)
data3 = bytearray([0x01, 0x2C, 0x2D]) #fastest refresh, 6 lines front, 3 lines back.
self._writecommand(tft.FRMCTR1) #Frame rate control.
self._writecommand(_FRMCTR1) #Frame rate control.
self._writedata(data3)
self._writecommand(tft.FRMCTR2) #Frame rate control.
self._writecommand(_FRMCTR2) #Frame rate control.
self._writedata(data3)
data6 = bytearray([0x01, 0x2c, 0x2d, 0x01, 0x2c, 0x2d])
self._writecommand(tft.FRMCTR3) #Frame rate control.
self._writecommand(_FRMCTR3) #Frame rate control.
self._writedata(data6)
pyb.delay(10)
self._writecommand(tft.INVCTR) #Display inversion control
self._writecommand(_INVCTR) #Display inversion control
self._writedata(0x07)
self._writecommand(tft.PWCTR1) #Power control
self._writecommand(_PWCTR1) #Power control
data3[0] = 0xA2
data3[1] = 0x02
data3[2] = 0x84
self._writedata(data3)
self._writecommand(tft.PWCTR2) #Power control
self._writecommand(_PWCTR2) #Power control
self._writedata(0xC5)
data2 = bytearray(2)
self._writecommand(tft.PWCTR3) #Power control
self._writecommand(_PWCTR3) #Power control
data2[0] = 0x0A #Opamp current small
data2[1] = 0x00 #Boost frequency
self._writedata(data2)
self._writecommand(tft.PWCTR4) #Power control
self._writecommand(_PWCTR4) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0x2A #Boost frequency
self._writedata(data2)
self._writecommand(tft.PWCTR5) #Power control
self._writecommand(_PWCTR5) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0xEE #Boost frequency
self._writedata(data2)
self._writecommand(tft.VMCTR1) #Power control
self._writecommand(_VMCTR1) #Power control
self._writedata(0x0E)
self._writecommand(tft.INVOFF)
self._writecommand(_INVOFF)
self._setMADCTL()
self._writecommand(tft.COLMOD)
self._writecommand(_COLMOD)
self._writedata(0x05)
self._writecommand(tft.CASET) #Column address set.
self._writecommand(_CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = 0x01 #Start at row/column 1.
self.windowLocData[2] = 0x00
self.windowLocData[3] = self._size[0] - 1
self._writedata(self.windowLocData)
self._writecommand(tft.RASET) #Row address set.
self._writecommand(_RASET) #Row address set.
self.windowLocData[3] = self._size[1] - 1
self._writedata(self.windowLocData)
dataGMCTRP = bytearray([0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29,
0x25, 0x2b, 0x39, 0x00, 0x01, 0x03, 0x10])
self._writecommand(tft.GMCTRP1)
self._writecommand(_GMCTRP1)
self._writedata(dataGMCTRP)
dataGMCTRN = bytearray([0x03, 0x1d, 0x07, 0x06, 0x2e, 0x2c, 0x29, 0x2d, 0x2e,
0x2e, 0x37, 0x3f, 0x00, 0x00, 0x02, 0x10])
self._writecommand(tft.GMCTRN1)
self._writecommand(_GMCTRN1)
self._writedata(dataGMCTRN)
self._writecommand(tft.NORON) #Normal display on.
self._writecommand(_NORON) #Normal display on.
pyb.delay(10)
self._writecommand(tft.DISPON)
self._writecommand(_DISPON)
pyb.delay(100)
self.cs.high()
def maker( ) :
t = tft(1, "X1", "X2")
print("Initializing")
t.initr()
t.fill(0)
return t
def makeb( ) :
t = tft(1, "X1", "X2")
print("Initializing")
t.initb()
t.fill(0)
return t
def makeg( ) :
t = tft(1, "X1", "X2")
print("Initializing")
t.initg()
#def maker( ) :
# t = tft(1, "X1", "X2")
# print("Initializing")
# t.initr()
# t.fill(0)
return t
# return t
#
#def makeb( ) :
# t = tft(1, "X1", "X2")
# print("Initializing")
# t.initb()
# t.fill(0)
# return t
#
#def makeg( ) :
# t = tft(1, "X1", "X2")
# print("Initializing")
# t.initg()
## t.fill(0)
# return t

Wyświetl plik

@ -1,10 +1,10 @@
import pyb
class joystick(object) :
_x_center = 2084.0
_y_center = 2114.0
_pos_x = 4095.0 - _x_center
_pos_y = 4095.0 - _y_center
_X_CENTER = 2084.0
_Y_CENTER = 2114.0
_POS_X = 4095.0 - _X_CENTER
_POS_Y = 4095.0 - _Y_CENTER
def __init__( self, aX, aY, aB ) :
self._jx = pyb.ADC(aX)
@ -43,10 +43,10 @@ class joystick(object) :
if self._index >= 3 :
self._index = 0
rx = float(sum(self._xA)) / 3.0 - joystick._x_center
ry = float(sum(self._yA)) / 3.0 - joystick._y_center
dx = joystick._pos_x if rx >= 0 else joystick._x_center
dy = joystick._pos_y if ry >= 0 else joystick._y_center
rx = float(sum(self._xA)) / 3.0 - joystick._X_CENTER
ry = float(sum(self._yA)) / 3.0 - joystick._Y_CENTER
dx = joystick._POS_X if rx >= 0 else joystick._X_CENTER
dy = joystick._POS_Y if ry >= 0 else joystick._Y_CENTER
self._x = rx / dx
self._y = ry / dy

Wyświetl plik

@ -48,7 +48,7 @@ class pca9865(object):
return self._b1[0]
def write( self, aVal, aLoc ) :
"""Write 8 bit value to given address. aVal may be an int buffer."""
'''Write 8 bit value to given address. aVal may be an int buffer.'''
self.i2c.mem_write(aVal, _ADDRESS, aLoc)
def reset( self ):

Wyświetl plik

@ -1,148 +0,0 @@
# MicroPython tm1637 quad 7-segment LED display driver
from machine import Pin
from time import sleep_us
_CMD_SET1 = const(64) # 0x40 data set
_CMD_SET2 = const(192) # 0xC0 address command set
_CMD_SET3 = const(128) # 0x80 data control command set
# 0-9, a-f, blank, dash
_SEGMENTS = [63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113,0,64]
class tm1637(object):
"""Library for the quad 7-segment LED display modules based on the tm1637
LED driver."""
def __init__(self, clk, dio, brightness=7):
self.clk = clk
self.dio = dio
if not 0 <= brightness <= 7:
raise ValueError("Brightness out of range")
self._brightness = brightness
self.clk.init(Pin.IN)
self.dio.init(Pin.IN)
self.clk(0)
self.dio(0)
def _start(self):
self.dio.init(Pin.OUT)
sleep_us(50)
def _stop(self):
self.dio.init(Pin.OUT)
sleep_us(50)
self.clk.init(Pin.IN)
sleep_us(50)
self.dio.init(Pin.IN)
sleep_us(50)
def _write_comm1(self):
self._start()
self._write_byte(_CMD_SET1)
self._stop()
def _write_comm3(self):
self._start()
self._write_byte(_CMD_SET3 + self._brightness + 7)
self._stop()
def _write_byte(self, b):
# send each bit
for i in range(8):
self.clk.init(Pin.OUT)
sleep_us(50)
self.dio.init(Pin.IN if b & 1 else Pin.OUT)
sleep_us(50)
self.clk.init(Pin.IN)
sleep_us(50)
b >>= 1
self.clk.init(Pin.OUT)
sleep_us(50)
self.clk.init(Pin.IN)
sleep_us(50)
self.clk.init(Pin.OUT)
sleep_us(50)
def brightness(self, val=None):
"""Set the display brightness 0-7."""
if val is None:
return self._brightness
if not 0 <= val <= 7:
raise ValueError("Brightness out of range")
self._brightness = val
self._write_comm1()
self._write_comm3()
def write(self, segments, pos=0):
"""Display up to 4 segments moving right from a given position.
The MSB in the 2nd segment controls the colon between the 2nd
and 3rd segments."""
if not 0 <= pos <= 3:
raise ValueError("Position out of range")
self._write_comm1()
# write COMM2 + first digit address
self._start()
self._write_byte(_CMD_SET2 + pos)
for seg in segments:
self._write_byte(seg)
self._stop()
self._write_comm3()
def encode_digit(self, digit):
"""Convert a character 0-9, a-f to a segment."""
return _SEGMENTS[digit & 0x0f]
def encode_string(self, string):
"""Convert an up to 4 character length string containing 0-9, a-f,
space, dash to an array of segments, matching the length of the
source string."""
segments = bytearray(4)
for i in range(0, min(4, len(string))):
segments[i] = self.encode_char(string[i])
return segments
def encode_char(self, char):
"""Convert a character 0-9, a-f, space or dash to a segment."""
o = ord(char)
# space
if o == 32:
return _SEGMENTS[16]
# dash
if o == 45:
return _SEGMENTS[17]
# uppercase A-F
if o >= 65 and o <= 70:
return _SEGMENTS[o-55]
# lowercase a-f
if o >= 97 and o <= 102:
return _SEGMENTS[o-87]
# 0-9
if o >= 48 and o <= 57:
return _SEGMENTS[o-48]
raise ValueError("Character out of range")
def hex(self, val):
"""Display a hex value 0x0000 through 0xffff, right aligned."""
string = '{:04x}'.format(val & 0xffff)
self.write(self.encode_string(string))
def number(self, num):
"""Display a numeric value -999 through 9999, right aligned."""
# limit to range -999 to 9999
num = max(-999, min(num, 9999))
string = '{0: >4d}'.format(num)
self.write(self.encode_string(string))
def numbers(self, num1, num2, colon=True):
"""Display two numeric values -9 through 99, with leading zeros
and separated by a colon."""
num1 = max(-9, min(num1, 99))
num2 = max(-9, min(num2, 99))
segments = self.encode_string('{0:0>2d}{1:0>2d}'.format(num1, num2))
# colon on
if colon:
segments[1] |= 0x80
self.write(segments)