-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmi8658.py
93 lines (85 loc) · 2.88 KB
/
qmi8658.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from machine import Pin,I2C,SPI,PWM,Timer,ADC
import framebuf
import time
Vbat_Pin = 29
#Pin definition
I2C_SDA = 6
I2C_SDL = 7
I2C_INT = 17
I2C_RST = 16
DC = 8
CS = 9
SCK = 10
MOSI = 11
MISO = 12
RST = 13
BL = 25
class QMI8658(object):
def __init__(self,address=0X6B):
self._address = address
self._bus = I2C(id=1,scl=Pin(I2C_SDL),sda=Pin(I2C_SDA),freq=100_000)
bRet=self.WhoAmI()
if bRet :
self.Read_Revision()
else :
return NULL
self.Config_apply()
def _read_byte(self,cmd):
rec=self._bus.readfrom_mem(int(self._address),int(cmd),1)
return rec[0]
def _read_block(self, reg, length=1):
rec=self._bus.readfrom_mem(int(self._address),int(reg),length)
return rec
def _read_u16(self,cmd):
LSB = self._bus.readfrom_mem(int(self._address),int(cmd),1)
MSB = self._bus.readfrom_mem(int(self._address),int(cmd)+1,1)
return (MSB[0] << 8) + LSB[0]
def _write_byte(self,cmd,val):
self._bus.writeto_mem(int(self._address),int(cmd),bytes([int(val)]))
def WhoAmI(self):
bRet=False
if (0x05) == self._read_byte(0x00):
bRet = True
return bRet
def Read_Revision(self):
return self._read_byte(0x01)
def Config_apply(self):
# REG CTRL1
self._write_byte(0x02,0x60)
# REG CTRL2 : QMI8658AccRange_8g and QMI8658AccOdr_1000Hz
self._write_byte(0x03,0x23)
# REG CTRL3 : QMI8658GyrRange_512dps and QMI8658GyrOdr_1000Hz
self._write_byte(0x04,0x53)
# REG CTRL4 : No
self._write_byte(0x05,0x00)
# REG CTRL5 : Enable Gyroscope And Accelerometer Low-Pass Filter
self._write_byte(0x06,0x11)
# REG CTRL6 : Disables Motion on Demand.
self._write_byte(0x07,0x00)
# REG CTRL7 : Enable Gyroscope And Accelerometer
self._write_byte(0x08,0x03)
def Read_Raw_XYZ(self):
xyz=[0,0,0,0,0,0]
raw_timestamp = self._read_block(0x30,3)
raw_acc_xyz=self._read_block(0x35,6)
raw_gyro_xyz=self._read_block(0x3b,6)
raw_xyz=self._read_block(0x35,12)
timestamp = (raw_timestamp[2]<<16)|(raw_timestamp[1]<<8)|(raw_timestamp[0])
for i in range(6):
# xyz[i]=(raw_acc_xyz[(i*2)+1]<<8)|(raw_acc_xyz[i*2])
# xyz[i+3]=(raw_gyro_xyz[((i+3)*2)+1]<<8)|(raw_gyro_xyz[(i+3)*2])
xyz[i] = (raw_xyz[(i*2)+1]<<8)|(raw_xyz[i*2])
if xyz[i] >= 32767:
xyz[i] = xyz[i]-65535
return xyz
def Read_XYZ(self):
xyz=[0,0,0,0,0,0]
raw_xyz=self.Read_Raw_XYZ()
#QMI8658AccRange_8g
acc_lsb_div=(1<<12)
#QMI8658GyrRange_512dps
gyro_lsb_div = 64
for i in range(3):
xyz[i]=raw_xyz[i]/acc_lsb_div#(acc_lsb_div/1000.0)
xyz[i+3]=raw_xyz[i+3]*1.0/gyro_lsb_div
return xyz