|
59 | 59 | def b2i(x, y):
|
60 | 60 | return (x << 8 | y) if not x & 0x80 else (-(((x ^ 255) << 8) | (y ^ 255) + 1))
|
61 | 61 |
|
| 62 | +def i2b(n): |
| 63 | + if n < 0: |
| 64 | + n = (-n ^ 0xFFFF) + 1 |
| 65 | + return bytes([ (n >> 8) & 0xFF, n & 0xFF ]) |
62 | 66 |
|
63 | 67 | def update():
|
64 | 68 | global acc, gyro, mag, temp
|
@@ -155,10 +159,13 @@ def heading():
|
155 | 159 |
|
156 | 160 |
|
157 | 161 | def calibrate_compass():
|
158 |
| - global calibrateMagFlag |
| 162 | + global calibrateMagFlag, mag_min, mag_max |
159 | 163 | import display
|
160 | 164 | from time import sleep
|
161 | 165 |
|
| 166 | + mag_min = [ 99999, 99999, 99999 ] |
| 167 | + mag_max = [ -99999, -99999, -99999 ] |
| 168 | + |
162 | 169 | display.scroll("TILT TO FILL SCREEN")
|
163 | 170 |
|
164 | 171 | buffer = bytearray(16)
|
@@ -226,17 +233,39 @@ def calibrate_compass():
|
226 | 233 | calibrateMagFlag = True
|
227 | 234 |
|
228 | 235 | def loadCalibrateFromSRAM():
|
229 |
| - return False |
| 236 | + buff = i2c1.readfrom_mem(0x6F, 0x20 + 0, 13) |
| 237 | + crc = 0 |
| 238 | + for i in range(12): |
| 239 | + crc = crc + buff[i] |
| 240 | + crc = crc & 0xFF |
| 241 | + |
| 242 | + if crc != buff[12]: |
| 243 | + return False |
| 244 | + |
| 245 | + for i in range(3): |
| 246 | + mag_min[i] = b2i(buff[(i * 2) + 0], buff[(i * 2) + 1]) / 100.0 |
| 247 | + for i in range(3): |
| 248 | + mag_max[i] = b2i(buff[(i * 2) + 0 + 6], buff[(i * 2) + 1 + 6]) / 100.0 |
230 | 249 |
|
231 |
| -def saveCalibrateIntoSRAM(): |
232 | 250 | return True
|
233 | 251 |
|
234 |
| -def calCRC(data, size): |
235 |
| - sum = b'\0' |
236 |
| - for i in range(size): |
237 |
| - sum += data[i] |
238 |
| - sum = sum ^ 0xFF |
239 |
| - return sum |
| 252 | +def saveCalibrateIntoSRAM(): |
| 253 | + buff = bytearray(13) |
| 254 | + for i in range(3): |
| 255 | + a = i2b(int(mag_min[i] * 100)) |
| 256 | + buff[(i * 2) + 0] = a[0] |
| 257 | + buff[(i * 2) + 1] = a[1] |
| 258 | + for i in range(3): |
| 259 | + a = i2b(int(mag_max[i] * 100)) |
| 260 | + buff[(i * 2) + 0 + 6] = a[0] |
| 261 | + buff[(i * 2) + 1 + 6] = a[1] |
| 262 | + crc = 0 |
| 263 | + for i in range(12): |
| 264 | + crc = crc + buff[i] |
| 265 | + buff[12] = crc & 0xFF |
| 266 | + |
| 267 | + i2c1.writeto_mem(0x6F, 0x20 + 0, buff) |
| 268 | + return True |
240 | 269 |
|
241 | 270 | def is_gesture(event, blocking=True):
|
242 | 271 | global __xStartCalc, __startCalcLowStrengthContinue
|
@@ -268,7 +297,7 @@ def is_gesture(event, blocking=True):
|
268 | 297 | else:
|
269 | 298 | lowStrengthContinue = False
|
270 | 299 | break
|
271 |
| - return lowStrengthContinue; |
| 300 | + return lowStrengthContinue |
272 | 301 | else:
|
273 | 302 | if acc[3] < 500:
|
274 | 303 | if not __startCalcLowStrengthContinue:
|
|
0 commit comments