File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/python
2
+
3
+ import time
4
+ from Adafruit_I2C import Adafruit_I2C
5
+
6
+ # ===========================================================================
7
+ # HMC6352 Class
8
+ # ===========================================================================
9
+
10
+ class HMC6352 :
11
+ i2c = None
12
+
13
+ # HMC6352 Address
14
+ address = 0x42 >> 1
15
+
16
+ # Commands
17
+ CMD_READ_DATA = 0x41
18
+ CMD_ENTER_USER_CALIBRATION = 0x45
19
+ CMD_EXIT_USER_CALIBRATION = 0x4C
20
+
21
+ # Constructor
22
+ def __init__ (self ):
23
+ self .i2c = Adafruit_I2C (self .address )
24
+
25
+ def userCalibration (self ):
26
+ "Write 0x45 for calibration and write 0x4C for leave after 20s"
27
+ self .i2c .write8 (0x0 , self .CMD_ENTER_USER_CALIBRATION )
28
+ time .sleep (20 )
29
+ self .i2c .write8 (0x0 , self .CMD_EXIT_USER_CALIBRATION )
30
+
31
+ def readData (self ):
32
+ "Read the heading data by write a 0x41 first"
33
+ self .i2c .write8 (0x0 , self .CMD_READ_DATA )
34
+ # Wait 6 ms
35
+ time .sleep (0.006 )
36
+ # Read 2 bytes
37
+ value = self .i2c .readU16 (0x0 )
38
+ # Reverse the data byte order
39
+ value = self .i2c .reverseByteOrder (value )
40
+ # Convert to 360.0 range from the raw integer value
41
+ value = float ('%0.1f' % value )/ 10
42
+
43
+ return value
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/python
2
+
3
+ import time
4
+ from Adafruit_HMC6352 import HMC6352
5
+
6
+ # ===========================================================================
7
+ # Example Code
8
+ # ===========================================================================
9
+
10
+ # Initialise the HMC6352
11
+ hmc = HMC6352 ()
12
+
13
+ heading = hmc .readData ()
14
+ print "Compass Heading: %.1f" % heading
15
+
16
+ time .sleep (0.1 )
Original file line number Diff line number Diff line change
1
+ ../ Adafruit_I2C / Adafruit_I2C .py
You can’t perform that action at this time.
0 commit comments