Advertisement
dragonn5678

Untitled

Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 57.79 KB | None | 0 0
  1. # ablib.py
  2. #
  3. # Python functions collection to easily manage the I/O lines and
  4. # Daisy modules with the following Acme Systems boards:
  5. # ARIETTA G25 SoM (http://www.acmesystems.it/arietta)
  6. # ARIAG25-EK Board (http://www.acmesystems.it/ariag25ek)
  7. # TERRA Board (http://www.acmesystems.it/terra)
  8. # ARIA G25 SoM (http://www.acmesystems.it/aria)
  9. # ACQUA A5 SoM (http://www.acmesystems.it/acqua)
  10. # FOX Board G20 (http://www.acmesystems.it/FOXG20)
  11. #
  12. # (C) 2014 Sergio Tanzilli <tanzilli@acmesystems.it>
  13. #
  14. # This program is free software; you can redistribute it and/or modify
  15. # it under the terms of the GNU General Public License as published by
  16. # the Free Software Foundation; either version 2 of the License, or
  17. # (at your option) any later version.
  18.  
  19. __version__ = 'v1.0.0'
  20.  
  21. import os.path
  22. import platform
  23. import smbus
  24. import time
  25. import serial
  26. import fcntl
  27. import struct
  28. import _thread
  29. import threading
  30. import select
  31. import math
  32.  
  33. if platform.platform().find("Linux-2")!=-1:
  34. legacy_id=True
  35. else:
  36. legacy_id=False
  37.  
  38. serial_ports = {
  39. 'D1' : '/dev/ttyS2',
  40. 'D2' : '/dev/ttyS5',
  41. 'D3' : '/dev/ttyS1',
  42. 'D5' : '/dev/ttyS6',
  43. 'D6' : '/dev/ttyS4',
  44. 'D8' : '/dev/ttyS3',
  45. 'D10': '/dev/ttyS4',
  46. 'D13': '/dev/ttyS2',
  47. 'D17': '/dev/ttyS1'
  48. }
  49.  
  50. #Pin to Kernel ID table
  51. pin2kid = {
  52.  
  53.  
  54. #Arietta G25
  55. 'J4.7' : 55, #PA23
  56. 'J4.8' : 54, #PA22
  57. 'J4.10' : 53, #PA21
  58. 'J4.11' : 56, #PA24
  59. 'J4.12' : 63, #PA31
  60. 'J4.13' : 57, #PA25
  61. 'J4.14' : 62, #PA30
  62. 'J4.15' : 58, #PA26
  63. 'J4.17' : 59, #PA27
  64. 'J4.19' : 60, #PA28
  65. 'J4.21' : 61, #PA29
  66. 'J4.23' : 32, #PA0
  67. 'J4.24' : 33, #PA1
  68. 'J4.25' : 40, #PA8
  69. 'J4.26' : 39, #PA7
  70. 'J4.27' : 38, #PA6
  71. 'J4.28' : 37, #PA5
  72. 'J4.29' : 124, #PC28
  73. 'J4.30' : 123, #PC27
  74. 'J4.31' : 100, #PC4
  75. 'J4.32' : 127, #PC31
  76. 'J4.33' : 99, #PC3
  77. 'J4.34' : 75, #PB11
  78. 'J4.35' : 98, #PC2
  79. 'J4.36' : 76, #PB12
  80. 'J4.37' : 97, #PC1
  81. 'J4.38' : 77, #PB13
  82. 'J4.39' : 96, #PC0
  83. 'J4.40' : 78, #PB14
  84.  
  85. #Aria G25
  86. 'N2' : 96,
  87. 'N3' : 97,
  88. 'N4' : 98,
  89. 'N5' : 99,
  90. 'N6' : 100,
  91. 'N7' : 101,
  92. 'N8' : 102,
  93. 'N9' : 103,
  94. 'N10' : 104,
  95. 'N11' : 105,
  96. 'N12' : 106,
  97. 'N13' : 107,
  98. 'N14' : 108,
  99. 'N15' : 109,
  100. 'N16' : 110,
  101. 'N17' : 111,
  102. 'N18' : 112,
  103. 'N19' : 113,
  104. 'N20' : 114,
  105. 'N21' : 115,
  106. 'N22' : 116,
  107. 'N23' : 117,
  108. 'E2' : 118,
  109. 'E3' : 119,
  110. 'E4' : 120,
  111. 'E5' : 121,
  112. 'E6' : 122,
  113. 'E7' : 123,
  114. 'E8' : 124,
  115. 'E9' : 125,
  116. 'E10' : 126,
  117. 'E11' : 127,
  118. 'S2' : 53,
  119. 'S3' : 52,
  120. 'S4' : 51,
  121. 'S5' : 50,
  122. 'S6' : 49,
  123. 'S7' : 48,
  124. 'S8' : 47,
  125. 'S9' : 46,
  126. 'S10' : 45,
  127. 'S11' : 44,
  128. 'S12' : 43,
  129. 'S13' : 42,
  130. 'S14' : 41,
  131. 'S15' : 40,
  132. 'S16' : 39,
  133. 'S17' : 38,
  134. 'S18' : 37,
  135. 'S19' : 36,
  136. 'S20' : 35,
  137. 'S21' : 34,
  138. 'S22' : 33,
  139. 'S23' : 32,
  140. 'W9' : 54,
  141. 'W10' : 55,
  142. 'W11' : 56,
  143. 'W12' : 57,
  144. 'W13' : 58,
  145. 'W14' : 59,
  146. 'W15' : 60,
  147. 'W16' : 61,
  148. 'W17' : 62,
  149. 'W18' : 63,
  150. 'W20' : 75,
  151. 'W21' : 76,
  152. 'W22' : 77,
  153. 'W23' : 78,
  154.  
  155. #FOX Board G20
  156.  
  157. 'J7.3' : 82,
  158. 'J7.4' : 83,
  159. 'J7.5' : 80,
  160. 'J7.6' : 81,
  161. 'J7.7' : 66,
  162. 'J7.8' : 67,
  163. 'J7.9' : 64,
  164. 'J7.10' : 65,
  165. 'J7.11' : 110,
  166. 'J7.12' : 111,
  167. 'J7.13' : 108,
  168. 'J7.14' : 109,
  169. 'J7.15' : 105,
  170. 'J7.16' : 106,
  171. 'J7.17' : 103,
  172. 'J7.18' : 104,
  173. 'J7.19' : 101,
  174. 'J7.20' : 102,
  175. 'J7.21' : 73,
  176. 'J7.22' : 72,
  177. 'J7.31' : 87,
  178. 'J7.32' : 86,
  179. 'J7.33' : 89,
  180. 'J7.34' : 88,
  181. 'J7.35' : 60,
  182. 'J7.36' : 59,
  183. 'J7.37' : 58,
  184. 'J7.38' : 57,
  185. 'J6.3' : 92,
  186. 'J6.4' : 71,
  187. 'J6.5' : 70,
  188. 'J6.6' : 93,
  189. 'J6.7' : 90,
  190. 'J6.8' : 69,
  191. 'J6.9' : 68,
  192. 'J6.10' : 91,
  193. 'J6.13' : 75,
  194. 'J6.14' : 74,
  195. 'J6.15' : 77,
  196. 'J6.16' : 76,
  197. 'J6.17' : 85,
  198. 'J6.18' : 84,
  199. 'J6.19' : 95,
  200. 'J6.20' : 94,
  201. 'J6.21' : 63,
  202. 'J6.22' : 62,
  203. 'J6.24' : 38,
  204. 'J6.25' : 39,
  205. 'J6.26' : 41,
  206. 'J6.27' : 99,
  207. 'J6.28' : 98,
  208. 'J6.29' : 97,
  209. 'J6.30' : 96,
  210. 'J6.31' : 56,
  211. 'J6.32' : 55,
  212. 'J6.36' : 42,
  213. 'J6.37' : 54,
  214. 'J6.38' : 43,
  215.  
  216. #Daisy modules
  217.  
  218. 'D1.1' : 0, #3V3
  219. 'D1.2' : 70, #PB6
  220. 'D1.3' : 71, #PB7
  221. 'D1.4' : 92, #PB28
  222. 'D1.5' : 93, #PB29
  223. 'D1.6' : 0, #N.C.
  224. 'D1.7' : 55, #PA23
  225. 'D1.8' : 56, #PA24
  226. 'D1.9' : 0, #5V0
  227. 'D1.10': 0, #GND
  228. 'D2.1' : 0, #3V3
  229. 'D2.2' : 63, #PA31
  230. 'D2.3' : 62, #PA30
  231. 'D2.4' : 61, #PA29
  232. 'D2.5' : 60, #PA28
  233. 'D2.6' : 59, #PA27
  234. 'D2.7' : 58, #PA26
  235. 'D2.8' : 57, #PA25
  236. 'D2.9' : 94, #PB30
  237. 'D2.10': 0, #GND
  238. 'D3.1' : 0, #3V3
  239. 'D3.2' : 68, #PB4
  240. 'D3.3' : 69, #PB5
  241. 'D3.4' : 90, #PB26
  242. 'D3.5' : 91, #PB27
  243. 'D3.6' : 86, #PB22
  244. 'D3.7' : 88, #PB24
  245. 'D3.8' : 89, #PB25
  246. 'D3.9' : 87, #PB23
  247. 'D3.10': 0, #GND
  248. 'D4.1' : 0, #3V3
  249. 'D4.2' : 0, #AVDD
  250. 'D4.3' : 0, #VREF
  251. 'D4.4' : 0, #AGND
  252. 'D4.5' : 96, #PC0
  253. 'D4.6' : 97, #PC1
  254. 'D4.7' : 98, #PC2
  255. 'D4.8' : 99, #PC3
  256. 'D4.9' : 0, #5V0
  257. 'D4.10': 0, #GND
  258. 'D5.1' : 0, #3V3
  259. 'D5.2' : 76, #PB12
  260. 'D5.3' : 77, #PB13
  261. 'D5.4' : 80, #PB16
  262. 'D5.5' : 81, #PB17
  263. 'D5.6' : 82, #PB18
  264. 'D5.7' : 83, #PB19
  265. 'D5.8' : 84, #PB20
  266. 'D5.9' : 85, #PB21
  267. 'D5.10': 0, #GND
  268. 'D6.1' : 0, #3V3
  269. 'D6.2' : 74, #PB10
  270. 'D6.3' : 75, #PB11
  271. 'D6.4' : 104, #PC8
  272. 'D6.5' : 106, #PC10
  273. 'D6.6' : 95, #PB31
  274. 'D6.7' : 55, #PA23
  275. 'D6.8' : 56, #PA24
  276. 'D6.9' : 0, #5V0
  277. 'D6.10': 0, #GND
  278. 'D7.1' : 0, #3V3
  279. 'D7.2' : 65, #PB1
  280. 'D7.3' : 64, #PB0
  281. 'D7.4' : 66, #PB2
  282. 'D7.5' : 67, #PB3
  283. 'D7.6' : 101, #PC5
  284. 'D7.7' : 100, #PC4
  285. 'D7.8' : 99, #PC3
  286. 'D7.9' : 0, #5V0
  287. 'D7.10': 0, #GND
  288. 'D8.1' : 0, #3V3
  289. 'D8.2' : 72, #PB8
  290. 'D8.3' : 73, #PB9
  291. 'D8.4' : 0, #N.C.
  292. 'D8.5' : 0, #N.C.
  293. 'D8.6' : 0, #N.C.
  294. 'D8.7' : 55, #PA23
  295. 'D8.8' : 56, #PA24
  296. 'D8.9' : 0, #5V0
  297. 'D8.10': 0, #GND
  298. 'D10.1' : 0, #3V3
  299. 'D10.2' : 118, #PC22
  300. 'D10.3' : 119, #PC23
  301. 'D10.4' : 120, #PC24
  302. 'D10.5' : 121, #PC25
  303. 'D10.6' : 122, #PC26
  304. 'D10.7' : 62, #PA30
  305. 'D10.8' : 63, #PA31
  306. 'D10.9' : 0, #5V0
  307. 'D10.10': 0, #GND
  308. 'D11.1' : 0, #3V3
  309. 'D11.2' : 112, #PC16
  310. 'D11.3' : 113, #PC17
  311. 'D11.4' : 114, #PC18
  312. 'D11.5' : 115, #PC19
  313. 'D11.6' : 116, #PC20
  314. 'D11.7' : 117, #PC21
  315. 'D11.8' : 98, #PC2
  316. 'D11.9' : 99, #PC3
  317. 'D11.10': 0, #GND
  318. 'D12.1' : 0, #3V3
  319. 'D12.2' : 104, #PC8
  320. 'D12.3' : 105, #PC9
  321. 'D12.4' : 106, #PC10
  322. 'D12.5' : 107, #PC11
  323. 'D12.6' : 108, #PC12
  324. 'D12.7' : 109, #PC13
  325. 'D12.8' : 110, #PC14
  326. 'D12.9' : 111, #PC15
  327. 'D12.10': 0, #GND
  328. 'D13.1' : 0, #3V3
  329. 'D13.2' : 37, #PA5
  330. 'D13.3' : 38, #PA6
  331. 'D13.4' : 123, #PC27
  332. 'D13.5' : 124, #PC28
  333. 'D13.6' : 125, #PC29
  334. 'D13.7' : 96, #PC0
  335. 'D13.8' : 97, #PC1
  336. 'D13.9' : 0, #5V0
  337. 'D13.10': 0, #GND
  338. 'D14.1' : 0, #3V3
  339. 'D14.2' : 0, #3V3
  340. 'D14.3' : 0, #VREF
  341. 'D14.4' : 0, #GND
  342. 'D14.5' : 75, #PB11
  343. 'D14.6' : 76, #PB12
  344. 'D14.7' : 77, #PB13
  345. 'D14.8' : 78, #PB14
  346. 'D14.9' : 0, #5V0
  347. 'D14.10': 0, #GND
  348. 'D15.1' : 0, #3V3
  349. 'D15.2' : 44, #PA12
  350. 'D15.3' : 43, #PA11
  351. 'D15.4' : 45, #PA13
  352. 'D15.5' : 46, #PA14
  353. 'D15.6' : 39, #PA7
  354. 'D15.7' : 33, #PA1
  355. 'D15.8' : 0, #N.C.
  356. 'D15.9' : 0, #5V0
  357. 'D15.10': 0, #GND
  358. 'D16.1' : 0, #3V3
  359. 'D16.2' : 61, #PA29
  360. 'D16.3' : 59, #PA27
  361. 'D16.4' : 56, #PA24
  362. 'D16.5' : 57, #PA25
  363. 'D16.6' : 58, #PA26
  364. 'D16.7' : 62, #PA30
  365. 'D16.8' : 63, #PA31.
  366. 'D16.9' : 60, #PA28
  367. 'D16.10': 0, #GND
  368. 'D17.1' : 0, #3V3
  369. 'D17.2' : 32, #PA0
  370. 'D17.3' : 33, #PA1
  371. 'D17.4' : 34, #PA2
  372. 'D17.5' : 35, #PA3
  373. 'D17.6' : 36, #PA4
  374. 'D17.7' : 96, #PC0
  375. 'D17.8' : 97, #PC1
  376. 'D17.9' : 0, #5V0
  377. 'D10.10': 0, #GND
  378.  
  379. #Acqua A5
  380.  
  381. 'J1.9' : 1+32,
  382. 'J1.10' : 0+32,
  383. 'J1.11' : 3+32,
  384. 'J1.12' : 2+32,
  385. 'J1.13' : 5+32,
  386. 'J1.14' : 4+32,
  387. 'J1.15' : 7+32,
  388. 'J1.16' : 6+32,
  389. 'J1.17' : 9+32,
  390. 'J1.18' : 8+32,
  391. 'J1.19' : 11+32,
  392. 'J1.20' : 10+32,
  393. 'J1.21' : 13+32,
  394. 'J1.22' : 12+32,
  395. 'J1.23' : 15+32,
  396. 'J1.24' : 14+32,
  397. 'J1.25' : 77+32,
  398. 'J1.26' : 78+32,
  399. 'J1.27' : 75+32,
  400. 'J1.28' : 76+32,
  401. 'J1.29' : 79+32,
  402. 'J1.30' : 74+32,
  403. 'J1.31' : 156+32,
  404. 'J1.32' : 155+32,
  405. 'J1.33' : 25+32,
  406. 'J1.35' : 27+32,
  407. 'J1.36' : 28+32,
  408. 'J1.37' : 29+32,
  409. 'J1.38' : 26+32,
  410. 'J1.39' : 24+32,
  411. 'J1.40' : 116+32,
  412. 'J1.41' : 117+32,
  413. 'J1.42' : 118+32,
  414. 'J1.43' : 119+32,
  415. 'J1.44' : 120+32,
  416. 'J1.45' : 121+32,
  417. 'J1.46' : 122+32,
  418. 'J1.47' : 123+32,
  419. 'J1.48' : 124+32,
  420. 'J1.49' : 125+32,
  421.  
  422. 'J2.1' : 127+32,
  423. 'J2.2' : 126+32,
  424. 'J2.3' : 115+32,
  425. 'J2.5' : 109+32,
  426. 'J2.6' : 108+32,
  427. 'J2.7' : 107+32,
  428. 'J2.8' : 106+32,
  429. 'J2.9' : 111+32,
  430. 'J2.10' : 110+32,
  431. 'J2.11' : 113+32,
  432. 'J2.12' : 112+32,
  433. 'J2.13' : 34+32,
  434. 'J2.14' : 114+32,
  435. 'J2.15' : 38+32,
  436. 'J2.16' : 35+32,
  437. 'J2.17' : 39+32,
  438. 'J2.18' : 43+32,
  439. 'J2.19' : 42+32,
  440. 'J2.23' : 36+32,
  441. 'J2.25' : 37+32,
  442. 'J2.29' : 32+32,
  443. 'J2.31' : 33+32,
  444. 'J2.32' : 46+32,
  445. 'J2.33' : 40+32,
  446. 'J2.34' : 47+32,
  447. 'J2.35' : 41+32,
  448. 'J2.36' : 48+32,
  449. 'J2.37' : 44+32,
  450. 'J2.38' : 49+32,
  451. 'J2.39' : 45+32,
  452. 'J2.40' : 50+32,
  453. 'J2.42' : 59+32,
  454. 'J2.43' : 58+32,
  455. 'J2.44' : 57+32,
  456. 'J2.45' : 60+32,
  457. 'J2.46' : 61+32,
  458.  
  459. 'J3.5' : 145+32, #PE17
  460. 'J3.6' : 144+32, #PE16
  461. 'J3.7' : 147+32, #PE19
  462. 'J3.8' : 146+32, #PE18
  463. 'J3.9' : 143+32, #PE15
  464. 'J3.10' : 151+32, #PE23
  465. 'J3.11' : 152+32, #PE24
  466. 'J3.12' : 153+32, #PE25
  467. 'J3.13' : 154+32, #PE26
  468. 'J3.14' : 148+32, #PE20
  469. 'J3.15' : 54+32, #PB22
  470. 'J3.16' : 55+32, #PB23
  471. 'J3.17' : 51+32, #PB19
  472. 'J3.18' : 53+32, #PB21
  473. 'J3.19' : 56+32, #PB24
  474. 'J3.20' : 52+32, #PB20
  475. 'J3.22' : 87+32, #PC23
  476. 'J3.23' : 89+32, #PC25
  477. 'J3.24' : 86+32, #PC22
  478. 'J3.25' : 88+32, #PC24
  479. 'J3.26' : 90+32, #PC26
  480. 'J3.28' : 91+32, #PC27
  481. 'J3.29' : 92+32, #PC28
  482. 'J3.30' : 94+32, #PC30
  483. 'J3.31' : 93+32, #PC29
  484. 'J3.32' : 95+32, #PC31
  485. 'J3.33' : 17+32, #PA17
  486. 'J3.34' : 16+32, #PA16
  487. 'J3.35' : 19+32, #PA19
  488. 'J3.36' : 18+32, #PA18
  489. 'J3.37' : 21+32, #PA21
  490. 'J3.38' : 20+32, #PA20
  491. 'J3.39' : 23+32, #PA23
  492. 'J3.40' : 22+32, #PA22
  493. 'J3.41' : 31+32, #PA31
  494. 'J3.42' : 30+32, #PA30
  495. 'J3.43' : 159+32, #PE31
  496. 'J3.44' : 157+32, #PE29
  497. 'J3.45' : 80+32, #PC16
  498. 'J3.46' : 81+32, #PC17
  499. 'J3.47' : 82+32, #PC18
  500. 'J3.48' : 83+32, #PC19
  501. 'J3.49' : 84+32, #PC20
  502. 'J3.50' : 85+32 #PC21
  503. }
  504.  
  505. pinmode = {
  506. "OUTPUT" : "low",
  507. "LOW" : "low",
  508. "HIGH" : "high",
  509. "INPUT" : "in"
  510. }
  511.  
  512. pinlevel = {
  513. "HIGH" : 1,
  514. "LOW" : 0
  515. }
  516.  
  517. mcuName2pinname = {
  518. #Arietta G25
  519. 'Arietta_G25' : {
  520. 'PA23' : 'J4.7',
  521. 'PA22' : 'J4.8',
  522. 'PA21' : 'J4.10',
  523. 'PA24' : 'J4.11',
  524. 'PA31' : 'J4.12',
  525. 'PA25' : 'J4.13',
  526. 'PA30' : 'J4.14',
  527. 'PA26' : 'J4.15',
  528. 'PA27' : 'J4.17',
  529. 'PA28' : 'J4.19',
  530. 'PA29' : 'J4.21',
  531. 'PA0' : 'J4.23',
  532. 'PA1' : 'J4.24',
  533. 'PA8' : 'J4.25',
  534. 'PA7' : 'J4.26',
  535. 'PA6' : 'J4.27',
  536. 'PA5' : 'J4.28',
  537. 'PC28' : 'J4.29',
  538. 'PC27' : 'J4.30',
  539. 'PC4' : 'J4.31',
  540. 'PC31' : 'J4.32',
  541. 'PC3' : 'J4.33',
  542. 'PB11' : 'J4.34',
  543. 'PC2' : 'J4.35',
  544. 'PB12' : 'J4.36',
  545. 'PC1' : 'J4.37',
  546. 'PB13' : 'J4.38',
  547. 'PC0' : 'J4.39',
  548. 'PB14' : 'J4.40'
  549. },
  550.  
  551. #Daisy modules
  552. 'Daisy' : {
  553. 'PB6' : 'D1.2',
  554. 'PB7' : 'D1.3',
  555. 'PB28' : 'D1.4',
  556. 'PB29' : 'D1.5',
  557. 'PA23' : 'D1.7',
  558. 'PA24' : 'D1.8',
  559.  
  560. 'PA31' : 'D2.2',
  561. 'PA30' : 'D2.3',
  562. 'PA29' : 'D2.4',
  563. 'PA28' : 'D2.5',
  564. 'PA27' : 'D2.6',
  565. 'PA26' : 'D2.7',
  566. 'PA25' : 'D2.8',
  567. 'PB30' : 'D2.9',
  568.  
  569. 'PB4' : 'D3.2',
  570. 'PB5' : 'D3.3',
  571. 'PB26' : 'D3.4',
  572. 'PB27' : 'D3.5',
  573. 'PB22' : 'D3.6',
  574. 'PB24' : 'D3.7',
  575. 'PB25' : 'D3.8',
  576. 'PB23' : 'D3.9',
  577.  
  578. 'PC0' : 'D4.5',
  579. 'PC1' : 'D4.6',
  580. 'PC2' : 'D4.7',
  581. 'PC3' : 'D4.8',
  582.  
  583. 'PB12' : 'D5.2',
  584. 'PB13' : 'D5.3',
  585. 'PB16' : 'D5.4',
  586. 'PB17' : 'D5.5',
  587. 'PB18' : 'D5.6',
  588. 'PB19' : 'D5.7',
  589. 'PB20' : 'D5.8',
  590. 'PB21' : 'D5.9',
  591.  
  592. 'PB10' : 'D6.2',
  593. 'PB11' : 'D6.3',
  594. 'PC8' : 'D6.4',
  595. 'PC10' : 'D6.5',
  596. 'PB31' : 'D6.6',
  597. 'PA23' : 'D6.7',
  598. 'PA24' : 'D6.8',
  599.  
  600. 'PB1' : 'D7.2',
  601. 'PB0' : 'D7.3',
  602. 'PB2' : 'D7.4',
  603. 'PB3' : 'D7.5',
  604. 'PC5' : 'D7.6',
  605. 'PC4' : 'D7.7',
  606. 'PC3' : 'D7.8',
  607.  
  608. 'PB8' : 'D8.2',
  609. 'PB9' : 'D8.3',
  610. 'PA23' : 'D8.7',
  611. 'PA24' : 'D8.8',
  612.  
  613. 'PC22' : 'D10.2',
  614. 'PC23' : 'D10.3',
  615. 'PC24' : 'D10.4',
  616. 'PC25' : 'D10.5',
  617. 'PC26' : 'D10.6',
  618. 'PA30' : 'D10.7',
  619. 'PA31' : 'D10.8',
  620.  
  621. 'PC16' : 'D11.2',
  622. 'PC17' : 'D11.3',
  623. 'PC18' : 'D11.4',
  624. 'PC19' : 'D11.5',
  625. 'PC20' : 'D11.6',
  626. 'PC21' : 'D11.7',
  627. 'PC2' : 'D11.8',
  628. 'PC3' : 'D11.9',
  629.  
  630. 'PC8' : 'D12.2',
  631. 'PC9' : 'D12.3',
  632. 'PC10' : 'D12.4',
  633. 'PC11' : 'D12.5',
  634. 'PC12' : 'D12.6',
  635. 'PC13' : 'D12.7',
  636. 'PC14' : 'D12.8',
  637. 'PC15' : 'D12.9',
  638.  
  639. 'PA5' : 'D13.2',
  640. 'PA6' : 'D13.3',
  641. 'PC27' : 'D13.4',
  642. 'PC28' : 'D13.5',
  643. 'PC29' : 'D13.6',
  644. 'PC0' : 'D13.7',
  645. 'PC1' : 'D13.8',
  646.  
  647. 'PB11' : 'D14.5',
  648. 'PB12' : 'D14.6',
  649. 'PB13' : 'D14.7',
  650. 'PB14' : 'D14.8',
  651.  
  652. 'PA12' : 'D15.2',
  653. 'PA11' : 'D15.3',
  654. 'PA13' : 'D15.4',
  655. 'PA14' : 'D15.5',
  656. 'PA7' : 'D15.6',
  657. 'PA1' : 'D15.7',
  658.  
  659. 'PA29' : 'D16.2',
  660. 'PA27' : 'D16.3',
  661. 'PA24' : 'D16.4',
  662. 'PA25' : 'D16.5',
  663. 'PA26' : 'D16.6',
  664. 'PA30' : 'D16.7',
  665. 'PA31' : 'D16.8',
  666. 'PA28' : 'D16.9',
  667.  
  668. 'PA0' : 'D17.2',
  669. 'PA1' : 'D17.3',
  670. 'PA2' : 'D17.4',
  671. 'PA3' : 'D17.5',
  672. 'PA4' : 'D17.6',
  673. 'PC0' : 'D17.7',
  674. 'PC1' : 'D17.8'
  675. },
  676.  
  677. #Acqua A5
  678. 'Acqua_A5' : {
  679. 'PA1' : 'J1.9',
  680. 'PA0' : 'J1.10',
  681. 'PA3' : 'J1.11',
  682. 'PA2' : 'J1.12',
  683. 'PA5' : 'J1.13',
  684. 'PA4' : 'J1.14',
  685. 'PA7' : 'J1.15',
  686. 'PA6' : 'J1.16',
  687. 'PA9' : 'J1.17',
  688. 'PA8' : 'J1.18',
  689. 'PA11' : 'J1.19',
  690. 'PA10' : 'J1.20',
  691. 'PA13' : 'J1.21',
  692. 'PA12' : 'J1.22',
  693. 'PA15' : 'J1.23',
  694. 'PA14' : 'J1.24',
  695. 'PC13' : 'J1.25',
  696. 'PC14' : 'J1.26',
  697. 'PC11' : 'J1.27',
  698. 'PC12' : 'J1.28',
  699. 'PC15' : 'J1.29',
  700. 'PC10' : 'J1.30',
  701. 'PE28' : 'J1.31',
  702. 'PE27' : 'J1.32',
  703. 'PA25' : 'J1.33',
  704. 'PA27' : 'J1.35',
  705. 'PA28' : 'J1.36',
  706. 'PA29' : 'J1.37',
  707. 'PA26' : 'J1.38',
  708. 'PA24' : 'J1.39',
  709. 'PD20' : 'J1.40',
  710. 'PD21' : 'J1.41',
  711. 'PD22' : 'J1.42',
  712. 'PD23' : 'J1.43',
  713. 'PD24' : 'J1.44',
  714. 'PD25' : 'J1.45',
  715. 'PD26' : 'J1.46',
  716. 'PD27' : 'J1.47',
  717. 'PD28' : 'J1.48',
  718. 'PD29' : 'J1.49',
  719.  
  720. 'PD31' : 'J2.1',
  721. 'PD30' : 'J2.2',
  722. 'PD19' : 'J2.3',
  723. 'PD13' : 'J2.5',
  724. 'PD12' : 'J2.6',
  725. 'PD11' : 'J2.7',
  726. 'PD10' : 'J2.8',
  727. 'PD15' : 'J2.9',
  728. 'PD14' : 'J2.10',
  729. 'PD17' : 'J2.11',
  730. 'PD16' : 'J2.12',
  731. 'PB2' : 'J2.13',
  732. 'PD18' : 'J2.14',
  733. 'PB6' : 'J2.15',
  734. 'PB3' : 'J2.16',
  735. 'PB7' : 'J2.17',
  736. 'PB11' : 'J2.18',
  737. 'PB10' : 'J2.19',
  738. 'PB4' : 'J2.23',
  739. 'PB5' : 'J2.25',
  740. 'PB0' : 'J2.29',
  741. 'PB1' : 'J2.31',
  742. 'PB14' : 'J2.32',
  743. 'PB8' : 'J2.33',
  744. 'PB15' : 'J2.34',
  745. 'PB9' : 'J2.35',
  746. 'PB16' : 'J2.36',
  747. 'PB12' : 'J2.37',
  748. 'PB17' : 'J2.38',
  749. 'PB13' : 'J2.39',
  750. 'PB18' : 'J2.40',
  751. 'PB27' : 'J2.42',
  752. 'PB26' : 'J2.43',
  753. 'PB25' : 'J2.44',
  754. 'PB28' : 'J2.45',
  755. 'PB29' : 'J2.46',
  756.  
  757. 'PE17' : 'J3.5',
  758. 'PE16' : 'J3.6',
  759. 'PE19' : 'J3.7',
  760. 'PE18' : 'J3.8',
  761. 'PE15' : 'J3.9',
  762. 'PE23' : 'J3.10',
  763. 'PE24' : 'J3.11',
  764. 'PE25' : 'J3.12',
  765. 'PE26' : 'J3.13',
  766. 'PE20' : 'J3.14',
  767. 'PB22' : 'J3.15',
  768. 'PB23' : 'J3.16',
  769. 'PB19' : 'J3.17',
  770. 'PB21' : 'J3.18',
  771. 'PB24' : 'J3.19',
  772. 'PB20' : 'J3.20',
  773. 'PC23' : 'J3.22',
  774. 'PC25' : 'J3.23',
  775. 'PC22' : 'J3.24',
  776. 'PC24' : 'J3.25',
  777. 'PC26' : 'J3.26',
  778. 'PC27' : 'J3.28',
  779. 'PC28' : 'J3.29',
  780. 'PC30' : 'J3.30',
  781. 'PC29' : 'J3.31',
  782. 'PC31' : 'J3.32',
  783. 'PA17' : 'J3.33',
  784. 'PA16' : 'J3.34',
  785. 'PA19' : 'J3.35',
  786. 'PA18' : 'J3.36',
  787. 'PA21' : 'J3.37',
  788. 'PA20' : 'J3.38',
  789. 'PA23' : 'J3.39',
  790. 'PA22' : 'J3.40',
  791. 'PA31' : 'J3.41',
  792. 'PA30' : 'J3.42',
  793. 'PE31' : 'J3.43',
  794. 'PE29' : 'J3.44',
  795. 'PC16' : 'J3.45',
  796. 'PC17' : 'J3.46',
  797. 'PC18' : 'J3.47',
  798. 'PC19' : 'J3.48',
  799. 'PC20' : 'J3.49',
  800. 'PC21' : 'J3.50'
  801. }
  802. }
  803.  
  804. def getVersion ():
  805. return __version__
  806.  
  807. def get_gpio_path(kernel_id):
  808. global legacy_id
  809. kernel_id=kernel_id-32
  810.  
  811. if (legacy_id==True):
  812. iopath="/sys/class/gpio/gpio%d" % (kernel_id+32)
  813.  
  814. if (legacy_id==False):
  815. iopath="/sys/class/gpio/pio"
  816. if kernel_id>=0 and kernel_id<=31:
  817. iopath="%sA%d" % (iopath,kernel_id-0)
  818. if kernel_id>=32 and kernel_id<=63:
  819. iopath="%sB%d" % (iopath,kernel_id-32)
  820. if kernel_id>=64 and kernel_id<=95:
  821. iopath="%sC%d" % (iopath,kernel_id-64)
  822. if kernel_id>=96 and kernel_id<=127:
  823. iopath="%sD%d" % (iopath,kernel_id-96)
  824. if kernel_id>=128 and kernel_id<=159:
  825. iopath="%sE%d" % (iopath,kernel_id-128)
  826.  
  827.  
  828. return iopath
  829.  
  830.  
  831. def get_kernel_id(connector_name,pin_number):
  832. return pinname2kernelid(connector_name + "." +pin_number)
  833.  
  834.  
  835. def export(kernel_id):
  836. global legacy_id
  837.  
  838. iopath=get_gpio_path(kernel_id)
  839. if not os.path.exists(iopath):
  840. f = open('/sys/class/gpio/export','w')
  841. if (legacy_id==True):
  842. f.write(str(kernel_id))
  843. else:
  844. f.write(str(kernel_id-32))
  845. f.close()
  846. os.system("sudo /bin/gpiochmod " + iopath + "")
  847.  
  848.  
  849.  
  850. def unexport(kernel_id):
  851. global legacy_id
  852.  
  853. iopath=get_gpio_path(kernel_id)
  854. if os.path.exists(iopath):
  855. f = open('/sys/class/gpio/unexport','w')
  856. if (legacy_id==True):
  857. f.write(str(kernel_id))
  858. else:
  859. f.write(str(kernel_id-32))
  860. f.close()
  861.  
  862. def direction(kernel_id,direct):
  863. iopath=get_gpio_path(kernel_id)
  864. if os.path.exists(iopath):
  865. f = open(iopath + '/direction','w')
  866. f.write(direct)
  867. f.close()
  868.  
  869. def set_value(kernel_id,value):
  870. iopath=get_gpio_path(kernel_id)
  871. if os.path.exists(iopath):
  872. f = open(iopath + '/value','w')
  873. f.write(str(value))
  874. f.close()
  875.  
  876. def get_value(kernel_id):
  877. if kernel_id!=-1:
  878. iopath=get_gpio_path(kernel_id)
  879. if os.path.exists(iopath):
  880. f = open(iopath + '/value','r')
  881. a=f.read()
  882. f.close()
  883. return int(a)
  884.  
  885. def set_edge(kernel_id,value):
  886. iopath=get_gpio_path(kernel_id)
  887. if os.path.exists(iopath):
  888. if value in ('none', 'rising', 'falling', 'both'):
  889. f = open(iopath + '/edge','w')
  890. f.write(value)
  891. f.close()
  892.  
  893. def soft_pwm_export(kernel_id):
  894. iopath='/sys/class/soft_pwm/pwm' + str(kernel_id)
  895. if not os.path.exists(iopath):
  896. f = open('/sys/class/soft_pwm/export','w')
  897. f.write(str(kernel_id))
  898. f.close()
  899.  
  900. def soft_pwm_period(kernel_id,value):
  901. iopath='/sys/class/soft_pwm/pwm' + str(kernel_id)
  902. if os.path.exists(iopath):
  903. f = open(iopath + '/period','w')
  904. f.write(str(value))
  905. f.close()
  906.  
  907. def soft_pwm_pulse(kernel_id,value):
  908. iopath='/sys/class/soft_pwm/pwm' + str(kernel_id)
  909. if os.path.exists(iopath):
  910. f = open(iopath + '/pulse','w')
  911. f.write(str(value))
  912. f.close()
  913.  
  914. def soft_pwm_steps(kernel_id,value):
  915. iopath='/sys/class/soft_pwm/pwm' + str(kernel_id)
  916. if os.path.exists(iopath):
  917. f = open(iopath + '/pulses','w')
  918. f.write(str(value))
  919. f.close()
  920.  
  921. def existI2Cdevice(bus_id,i2c_address):
  922. i2c_bus = smbus.SMBus(bus_id)
  923. try:
  924. i2c_bus.write_byte(i2c_address,0x00)
  925. return True
  926. except:
  927. return False
  928.  
  929. def pinname2kernelid(pinname):
  930. """
  931. Return the Kernel ID of any Pin using the MCU name
  932. or the board name
  933. """
  934.  
  935. offset=-1
  936. if pinname[0:2]=="PA":
  937. offset=32+0
  938. if pinname[0:2]=="PB":
  939. offset=32+32
  940. if pinname[0:2]=="PC":
  941. offset=32+64
  942. if pinname[0:2]=="PD":
  943. offset=32+96
  944. if pinname[0:2]=="PE":
  945. offset=32+128
  946.  
  947. if offset!=-1:
  948. return offset+int(pinname[2:4])
  949. else:
  950. return pin2kid[pinname]
  951.  
  952. def readU8(bus,address,reg):
  953. result = bus.read_byte_data(address, reg)
  954. return result
  955.  
  956. def readS8(bus,address,reg):
  957. result = bus.read_byte_data(address, reg)
  958. if result > 127:
  959. result -= 256
  960. return result
  961.  
  962. def readS16(bus,address,register):
  963. hi = readS8(bus,address,register)
  964. lo = readU8(bus,address,register+1)
  965. return (hi << 8) + lo
  966.  
  967. def readU16(bus,address,register):
  968. hi = readU8(bus,address,register)
  969. lo = readU8(bus,address,register+1)
  970. return (hi << 8) + lo
  971.  
  972. def write8(bus,address,reg,value):
  973. bus.write_byte_data(address,reg,value)
  974.  
  975. class Pin():
  976. """
  977. FOX and AriaG25 pins related class
  978. """
  979. kernel_id=None
  980. fd=None
  981.  
  982.  
  983. def __init__(self,pin,mode):
  984. self.kernel_id=pinname2kernelid(pin)
  985. export(self.kernel_id)
  986. direction(self.kernel_id,pinmode[mode])
  987.  
  988. iopath=get_gpio_path(self.kernel_id)
  989. if os.path.exists(iopath):
  990. self.fd = open(iopath + '/value','r')
  991.  
  992. def high(self):
  993. set_value(self.kernel_id,1)
  994.  
  995. def low(self):
  996. set_value(self.kernel_id,0)
  997.  
  998. def on(self):
  999. set_value(self.kernel_id,1)
  1000.  
  1001. def off(self):
  1002. set_value(self.kernel_id,0)
  1003.  
  1004. def digitalWrite(self,level):
  1005. set_value(self.kernel_id,pinlevel[level])
  1006.  
  1007. def set_value(self,value):
  1008. return set_value(self.kernel_id,value)
  1009.  
  1010. def digitalRead(self):
  1011. return get_value(self.kernel_id)
  1012.  
  1013. def get_value(self):
  1014. return get_value(self.kernel_id)
  1015.  
  1016. get = get_value
  1017.  
  1018. def wait_edge(self,fd,callback,debouncingtime):
  1019. debouncingtime=debouncingtime/1000.0 # converto in millisecondi
  1020. timestampprec=time.time()
  1021. counter=0
  1022. po = select.epoll()
  1023. po.register(fd,select.EPOLLET)
  1024. while True:
  1025. events = po.poll()
  1026. timestamp=time.time()
  1027. if (timestamp-timestampprec>debouncingtime) and counter>0:
  1028. callback()
  1029. counter=counter+1
  1030. timestampprec=timestamp
  1031.  
  1032. def set_edge(self,value,callback,debouncingtime=0):
  1033. if self.fd!=None:
  1034. set_edge(self.kernel_id,value)
  1035. thread.start_new_thread(self.wait_edge,(self.fd,callback,debouncingtime))
  1036. return
  1037. else:
  1038. thread.exit()
  1039.  
  1040. ## DAISY-4 #############################################################
  1041.  
  1042. class Daisy4():
  1043.  
  1044. """
  1045. DAISY-4 (Relay module) related class
  1046. http://www.acmesystems.it/DAISY-4
  1047. """
  1048. kernel_id=-1
  1049.  
  1050. dips = {
  1051. 'DIP1' : '2',
  1052. 'DIP2' : '3',
  1053. 'DIP3' : '4',
  1054. 'DIP4' : '5',
  1055. 'DIP5' : '6',
  1056. 'DIP6' : '7',
  1057. 'DIP7' : '8',
  1058. 'DIP8' : '9',
  1059. }
  1060.  
  1061. def __init__(self,connector_id,dip_id):
  1062. pin=self.dips[dip_id]
  1063. self.kernel_id = pinname2kernelid(connector_id + "." + pin)
  1064.  
  1065. if (self.kernel_id!=0):
  1066. export(self.kernel_id)
  1067. direction(self.kernel_id,'low')
  1068.  
  1069. def on(self):
  1070. if (self.kernel_id!=0):
  1071. set_value(self.kernel_id,1)
  1072. else:
  1073. pass
  1074.  
  1075.  
  1076. def off(self):
  1077. if (self.kernel_id!=0):
  1078. set_value(self.kernel_id,0)
  1079. else:
  1080. pass
  1081.  
  1082. ## DAISY-5 #############################################################
  1083.  
  1084.  
  1085. class Daisy5():
  1086.  
  1087. """
  1088. DAISY-5 (8 pushbuttons) related class
  1089. http://www.acmesystems.it/DAISY-5
  1090. """
  1091. kernel_id=None
  1092. fd=None
  1093.  
  1094. buttons = {
  1095. 'P1' : '2',
  1096. 'P2' : '3',
  1097. 'P3' : '4',
  1098. 'P4' : '5',
  1099. 'P5' : '6',
  1100. 'P6' : '7',
  1101. 'P7' : '8',
  1102. 'P8' : '9',
  1103. }
  1104.  
  1105. def __init__(self,connector_id,button_id):
  1106. pin=self.buttons[button_id]
  1107. self.kernel_id = pinname2kernelid(connector_id + "." + pin)
  1108.  
  1109. if (self.kernel_id!=None):
  1110. export(self.kernel_id)
  1111. direction(self.kernel_id,'in')
  1112.  
  1113. iopath=get_gpio_path(self.kernel_id)
  1114. if os.path.exists(iopath):
  1115. self.fd = open(iopath + '/value','r')
  1116.  
  1117. def pressed(self):
  1118. return self.get()
  1119.  
  1120. def get(self):
  1121. if self.fd!=None:
  1122. self.fd.seek(0)
  1123. a=self.fd.read()
  1124. if int(a)==0:
  1125. return False
  1126. else:
  1127. return True
  1128. return False
  1129.  
  1130. def wait_edge(self,fd,callback):
  1131. counter=0
  1132. po = select.epoll()
  1133. po.register(fd,select.EPOLLET)
  1134. while True:
  1135. events = po.poll()
  1136. if counter>0:
  1137. callback()
  1138. counter=counter+1
  1139.  
  1140. def set_edge(self,value,callback):
  1141. if self.fd!=None:
  1142. set_edge(self.kernel_id,value)
  1143. thread.start_new_thread(self.wait_edge,(self.fd,callback))
  1144. return
  1145. else:
  1146. thread.exit()
  1147.  
  1148. ## DAISY-7 #############################################################
  1149.  
  1150. class Daisy7():
  1151.  
  1152. """
  1153. DAISY-7 (GPR/MEMS) related class
  1154. http://www.acmesystems.it/DAISY-7
  1155. """
  1156.  
  1157. # Linear accellerometer registers LIS331DLH
  1158. acc_registers = {
  1159. 'WHO_AM_I' : 0x0F,
  1160. 'CTRL_REG1' : 0x20,
  1161. 'CTRL_REG2' : 0x21,
  1162. 'CTRL_REG3' : 0x22,
  1163. 'CTRL_REG4' : 0x23,
  1164. 'CTRL_REG5' : 0x24,
  1165. 'HP_FILTER_RESET' : 0x25,
  1166. 'REFERENCE' : 0x26,
  1167. 'STATUS_REG' : 0x27,
  1168. 'OUT_X_L' : 0x28,
  1169. 'OUT_X_H' : 0x29,
  1170. 'OUT_Y_L' : 0x2A,
  1171. 'OUT_Y_H' : 0x2B,
  1172. 'OUT_Z_L' : 0x2C,
  1173. 'OUT_Z_H' : 0x2D,
  1174. 'INT1_CFG' : 0x30,
  1175. 'INT1_SRC' : 0x31,
  1176. 'INT1_THS' : 0x32,
  1177. 'INT1_DURATION' : 0x33,
  1178. 'INT2_CFG' : 0x34,
  1179. 'INT2_SRC' : 0x35,
  1180. 'INT1_THS' : 0x36,
  1181. 'INT2_DURATION' : 0x37,
  1182.  
  1183. #I2C Address
  1184. 'I2C_ADDR' : 0x18,
  1185. }
  1186.  
  1187. # Gyroscope registers L3G4200D
  1188. gyro_registers = {
  1189. 'WHO_AM_I' : 0x0F,
  1190. 'CTRL_REG1' : 0x20,
  1191. 'CTRL_REG2' : 0x21,
  1192. 'CTRL_REG3' : 0x22,
  1193. 'CTRL_REG4' : 0x23,
  1194. 'CTRL_REG5' : 0x24,
  1195. 'REFERENCE' : 0x25,
  1196. 'OUT_TEMP' : 0x26,
  1197. 'STATUS_REG' : 0x27,
  1198. 'OUT_X_L' : 0x28,
  1199. 'OUT_X_H' : 0x29,
  1200. 'OUT_Y_L' : 0x2A,
  1201. 'OUT_Y_H' : 0x2B,
  1202. 'OUT_Z_L' : 0x2C,
  1203. 'OUT_Z_H' : 0x2D,
  1204. 'FIFO_CTRL_REG' : 0x2E,
  1205. 'FIFO_SRC_REG' : 0x2F,
  1206. 'INT1_CFG' : 0x30,
  1207. 'INT1_SRC' : 0x31,
  1208. 'INT1_THS_XH' : 0x32,
  1209. 'INT1_THS_XL' : 0x33,
  1210. 'INT1_THS_YH' : 0x34,
  1211. 'INT1_THS_YL' : 0x35,
  1212. 'INT1_THS_ZH' : 0x36,
  1213. 'INT1_THS_ZL' : 0x37,
  1214. 'INT1_DURATION' : 0x38,
  1215.  
  1216. #I2C Address
  1217. 'I2C_ADDR' : 0x68,
  1218. }
  1219.  
  1220. # Compass registers HMC5883L
  1221. compass_registers = {
  1222. 'CONF_REG_A' : 0x00,
  1223. 'CONF_REG_B' : 0x01,
  1224. 'MODE_REG' : 0x02,
  1225. 'OUT_X_H' : 0x03,
  1226. 'OUT_X_L' : 0x04,
  1227. 'OUT_Z_H' : 0x05,
  1228. 'OUT_Z_L' : 0x06,
  1229. 'OUT_Y_H' : 0x07,
  1230. 'OUT_Y_L' : 0x08,
  1231. 'STATUS_REG' : 0x09,
  1232. 'ID_REG_A' : 0x0A,
  1233. 'ID_REG_B' : 0x0B,
  1234. 'ID_REG_C' : 0x0C,
  1235.  
  1236. #I2C Address
  1237. 'I2C_ADDR' : 0x1E,
  1238. }
  1239.  
  1240. MeasurementContinuous = 0x00
  1241. MeasurementSingleShot = 0x01
  1242. MeasurementIdle = 0x03
  1243.  
  1244. # Barometer registers BMP085
  1245. baro_registers = {
  1246.  
  1247. #Registers address
  1248. 'CAL_AC1' : 0xAA,
  1249. 'CAL_AC2' : 0xAC,
  1250. 'CAL_AC3' : 0xAE,
  1251. 'CAL_AC4' : 0xB0,
  1252. 'CAL_AC5' : 0xB2,
  1253. 'CAL_AC6' : 0xB4,
  1254. 'CAL_B1' : 0xB6,
  1255. 'CAL_B2' : 0xB8,
  1256. 'CAL_MB' : 0xBA,
  1257. 'CAL_MC' : 0xBC,
  1258. 'CAL_MD' : 0xBE,
  1259. 'CONTROL' : 0xF4,
  1260. 'TEMPDATA' : 0xF6,
  1261. 'PRESSUREDATA' : 0xF6,
  1262. 'READTEMPCMD' : 0x2E,
  1263. 'READPRESSURECMD' : 0x34,
  1264.  
  1265. #Buffer for calibration data
  1266. 'BUF_AC1' : 0,
  1267. 'BUF_AC2' : 0,
  1268. 'BUF_AC3' : 0,
  1269. 'BUF_AC4' : 0,
  1270. 'BUF_AC5' : 0,
  1271. 'BUF_AC6' : 0,
  1272. 'BUF_B1' : 0,
  1273. 'BUF_B2' : 0,
  1274. 'BUF_MB' : 0,
  1275. 'BUF_MC' : 0,
  1276. 'BUF_MD' : 0,
  1277.  
  1278. #Operating mode
  1279. 'ULTRALOWPOWER' : 0,
  1280. 'STANDARD' : 1,
  1281. 'HIGHRES' : 2,
  1282. 'ULTRAHIGHRES' : 3,
  1283.  
  1284. #I2C Address
  1285. 'I2C_ADDR' : 0x77,
  1286. }
  1287.  
  1288. i2c_bus=-1
  1289. acc_address=0x18
  1290. gyro_address=0x68
  1291. compass_address=0x1E
  1292. ser=-1
  1293. mode = baro_registers["STANDARD"]
  1294.  
  1295. def __init__(self,connector_id):
  1296. self.ser = serial.Serial(
  1297. port=serial_ports[connector_id],
  1298. baudrate=115200,
  1299. timeout=1,
  1300. parity=serial.PARITY_NONE,
  1301. stopbits=serial.STOPBITS_ONE,
  1302. bytesize=serial.EIGHTBITS
  1303. )
  1304. self.ser.flushInput()
  1305.  
  1306. self.i2c_bus = smbus.SMBus(0)
  1307.  
  1308. if self.checkChipAdresses()==False:
  1309. raise IOError("I2C chip not found")
  1310.  
  1311. #Accellerometer setup
  1312. #Chip in Normal mode. Turn on all axis
  1313. self.i2c_bus.write_byte_data(self.acc_address,self.acc_registers['CTRL_REG1'],0x27)
  1314.  
  1315. #Gyroscope setup
  1316. #Chip in Normal mode. Turn on all axis
  1317. self.i2c_bus.write_byte_data(self.gyro_address,self.gyro_registers['CTRL_REG1'],0x0F)
  1318. #Full 2000dps to control REG4
  1319. self.i2c_bus.write_byte_data(self.gyro_address,self.gyro_registers['CTRL_REG4'],0x20)
  1320.  
  1321. #Compass setup
  1322. self.compass_setScale(1.3)
  1323. self.compass_setContinuousMode()
  1324. self.compass_setDeclination(9,54)
  1325.  
  1326. #Read the calibration data
  1327. self.baro_getCalibrationData()
  1328.  
  1329. return
  1330.  
  1331. def __str__(self):
  1332. ret_str = ""
  1333. (x, y, z) = self.acc_getAxes()
  1334. ret_str += "Accellerator: "+"\n"
  1335. ret_str += " Axis X: "+str(x)+"\n"
  1336. ret_str += " Axis Y: "+str(y)+"\n"
  1337. ret_str += " Axis Z: "+str(z)+"\n\n"
  1338.  
  1339. (x, y, z) = self.gyro_getAxes()
  1340. ret_str += "Gyroscope: "+"\n"
  1341. ret_str += " Axis X: "+str(x)+"\n"
  1342. ret_str += " Axis Y: "+str(y)+"\n"
  1343. ret_str += " Axis Z: "+str(z)+"\n\n"
  1344.  
  1345. (x, y, z) = self.compass_getAxes()
  1346. ret_str += "Compass: "+"\n"
  1347. ret_str += " Axis X: "+str(x)+"\n"
  1348. ret_str += " Axis Y: "+str(y)+"\n"
  1349. ret_str += " Axis Z: "+str(z)+"\n\n"
  1350.  
  1351. ret_str += "Barometer: "+"\n"
  1352. ret_str += " Temperature: "+str(self.baro_getTemperature())+"\n"
  1353. ret_str += " Pressure: "+str(self.baro_getPressure())+"\n"
  1354. ret_str += " Altitude: "+str(self.baro_getAltitude())+"\n\n"
  1355.  
  1356. (latitude,longitude) = self.gps_getGoogleCoordinates()
  1357. ret_str += "GPS: "+"\n"
  1358. ret_str += " Latidute: "+str(latitude)+"\n"
  1359. ret_str += " Longitude: "+str(longitude)+"\n\n"
  1360.  
  1361. return ret_str
  1362.  
  1363. def readNMEAmsg(self):
  1364. self.ser.flushInput()
  1365. return self.ser.readline().replace("\r\n","")
  1366.  
  1367.  
  1368. def checkChipAdresses(self):
  1369. rtc=True
  1370.  
  1371. try:
  1372. self.i2c_bus.write_byte(self.acc_registers['I2C_ADDR'],self.acc_registers['WHO_AM_I'])
  1373. except IOError(err):
  1374. print ("Accellerometer not found" )
  1375. rtc=False
  1376.  
  1377. try:
  1378. self.i2c_bus.write_byte(self.gyro_registers['I2C_ADDR'],self.gyro_registers['WHO_AM_I'])
  1379. except IOError(err):
  1380. print ("Gyroscope not found" )
  1381. rtc=False
  1382.  
  1383. return rtc
  1384.  
  1385.  
  1386. #converts 16 bit two's compliment reading to signed int
  1387. def getSignedNumber(self,number):
  1388. if number & (1 << 15):
  1389. return number | ~65535
  1390. else:
  1391. return number & 65535
  1392.  
  1393. # GPS functions
  1394.  
  1395. def gps_getGoogleCoordinates(self):
  1396. #Read a line from the GPS chip
  1397. NMEA_line = self.ser.readline()
  1398.  
  1399. #Split the fields in NMEA line
  1400. values=NMEA_line.split(",")
  1401.  
  1402. #Select just the GGA message line with GPS quality indicator = 1
  1403. #(see the GPS datasheet)
  1404. if values[0]=="$GPGGA" and values[6]=="1":
  1405. H=float(values[2][0:2])
  1406. M=float(values[2][2:4])+(float(values[2][5:9])/10000)
  1407. latitude=H+M/60
  1408.  
  1409. H=float(values[4][0:3])
  1410. M=float(values[4][3:5])+(float(values[4][6:10])/10000)
  1411. longitude=H+M/60
  1412.  
  1413. return (latitude,longitude)
  1414. else:
  1415. return (-1,-1)
  1416.  
  1417. # ACCELLEROMETER functions
  1418.  
  1419. def acc_getAxes(self):
  1420. while True:
  1421. self.i2c_bus.write_byte(self.acc_address,self.acc_registers['STATUS_REG'])
  1422. status_reg=self.i2c_bus.read_byte(self.acc_address)
  1423. if (status_reg&0x08)!=0:
  1424. break
  1425.  
  1426. #Read X axis value
  1427. self.i2c_bus.write_byte(self.acc_address,self.acc_registers['OUT_X_L'])
  1428. OUT_X_L=self.i2c_bus.read_byte(self.acc_address)
  1429. self.i2c_bus.write_byte(self.acc_address,self.acc_registers['OUT_X_H'])
  1430. OUT_X_H=self.i2c_bus.read_byte(self.acc_address)
  1431.  
  1432. #Read Y axis value
  1433. self.i2c_bus.write_byte(self.acc_address,self.acc_registers['OUT_Y_L'])
  1434. OUT_Y_L=self.i2c_bus.read_byte(self.acc_address)
  1435. self.i2c_bus.write_byte(self.acc_address,self.acc_registers['OUT_Y_H'])
  1436. OUT_Y_H=self.i2c_bus.read_byte(self.acc_address)
  1437.  
  1438. #Read Z axis value
  1439. self.i2c_bus.write_byte(self.acc_address,self.acc_registers['OUT_Z_L'])
  1440. OUT_Z_L=self.i2c_bus.read_byte(self.acc_address)
  1441. self.i2c_bus.write_byte(self.acc_address,self.acc_registers['OUT_Z_H'])
  1442. OUT_Z_H=self.i2c_bus.read_byte(self.acc_address)
  1443.  
  1444. xValue=self.getSignedNumber(OUT_X_H<<8|OUT_X_L)
  1445. yValue=self.getSignedNumber(OUT_Y_H<<8|OUT_Y_L)
  1446. zValue=self.getSignedNumber(OUT_Z_H<<8|OUT_Z_L)
  1447.  
  1448. return (xValue,yValue,zValue)
  1449.  
  1450. # GYRO functions
  1451.  
  1452. def gyro_getAxes(self):
  1453. while True:
  1454. self.i2c_bus.write_byte(self.gyro_address,self.gyro_registers['STATUS_REG'])
  1455. status_reg=self.i2c_bus.read_byte(self.gyro_address)
  1456. if (status_reg&0x08)!=0:
  1457. break
  1458.  
  1459. #Read X axis value
  1460. self.i2c_bus.write_byte(self.gyro_address,self.gyro_registers['OUT_X_L'])
  1461. OUT_X_L=self.i2c_bus.read_byte(self.gyro_address)
  1462. self.i2c_bus.write_byte(self.gyro_address,self.gyro_registers['OUT_X_H'])
  1463. OUT_X_H=self.i2c_bus.read_byte(self.gyro_address)
  1464. xValue=self.getSignedNumber(OUT_X_H<<8 | OUT_X_L)
  1465.  
  1466. #Read Y axis value
  1467. self.i2c_bus.write_byte(self.gyro_address,self.gyro_registers['OUT_Y_L'])
  1468. OUT_Y_L=self.i2c_bus.read_byte(self.gyro_address)
  1469. self.i2c_bus.write_byte(self.gyro_address,self.gyro_registers['OUT_Y_H'])
  1470. OUT_Y_H=self.i2c_bus.read_byte(self.gyro_address)
  1471. yValue=self.getSignedNumber(OUT_Y_H<<8 | OUT_Y_L)
  1472.  
  1473. #Read Z axis value
  1474. self.i2c_bus.write_byte(self.gyro_address,self.gyro_registers['OUT_Z_L'])
  1475. OUT_Z_L=self.i2c_bus.read_byte(self.gyro_address)
  1476. self.i2c_bus.write_byte(self.gyro_address,self.gyro_registers['OUT_Z_H'])
  1477. OUT_Z_H=self.i2c_bus.read_byte(self.gyro_address)
  1478. zValue=self.getSignedNumber(OUT_Z_H<<8 | OUT_Z_L)
  1479.  
  1480. xValue=self.getSignedNumber(OUT_X_H<<8|OUT_X_L)
  1481. yValue=self.getSignedNumber(OUT_Y_H<<8|OUT_Y_L)
  1482. zValue=self.getSignedNumber(OUT_Z_H<<8|OUT_Z_L)
  1483.  
  1484. return (xValue,yValue,zValue)
  1485.  
  1486. # BAROMETER functions
  1487. # Some parts of this code become from the Adafruit I2C libraries
  1488. # https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_BMP085/Adafruit_BMP085.py
  1489.  
  1490. def baro_getRawTemperature(self):
  1491. self.i2c_bus.write_byte_data(self.baro_registers['I2C_ADDR'],self.baro_registers['CONTROL'],self.baro_registers['READTEMPCMD'])
  1492. time.sleep(0.005)
  1493. temperature=readU16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['TEMPDATA'])
  1494. return temperature
  1495.  
  1496. def baro_getTemperature(self):
  1497. UT = 0
  1498. X1 = 0
  1499. X2 = 0
  1500. B5 = 0
  1501. temp = 0.0
  1502.  
  1503. # Read raw temp before aligning it with the calibration values
  1504. UT = self.baro_getRawTemperature()
  1505. X1 = ((UT - self.baro_registers["BUF_AC6"]) * self.baro_registers["BUF_AC5"]) >> 15
  1506. X2 = (self.baro_registers["BUF_MC"] << 11) / (X1 + self.baro_registers["BUF_MD"])
  1507. B5 = X1 + X2
  1508. temp = ((B5 + 8) >> 4) / 10.0
  1509. return temp
  1510.  
  1511. def baro_getCalibrationData(self):
  1512. self.baro_registers["BUF_AC1"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_AC1'])
  1513. self.baro_registers["BUF_AC2"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_AC2'])
  1514. self.baro_registers["BUF_AC3"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_AC3'])
  1515. self.baro_registers["BUF_AC4"]=readU16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_AC4'])
  1516. self.baro_registers["BUF_AC5"]=readU16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_AC5'])
  1517. self.baro_registers["BUF_AC6"]=readU16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_AC6'])
  1518. self.baro_registers["BUF_B1"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_B1'])
  1519. self.baro_registers["BUF_B2"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_B2'])
  1520. self.baro_registers["BUF_MB"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_MB'])
  1521. self.baro_registers["BUF_MC"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_MC'])
  1522. self.baro_registers["BUF_MD"]=readS16(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers['CAL_MD'])
  1523.  
  1524. def baro_showCalibrationData(self):
  1525. print ( "CAL_AC1 = %6d" % (self.baro_registers["BUF_AC1"]))
  1526. print ( "CAL_AC2 = %6d" % (self.baro_registers["BUF_AC2"]))
  1527. print ( "CAL_AC3 = %6d" % (self.baro_registers["BUF_AC3"]))
  1528. print ( "CAL_AC4 = %6d" % (self.baro_registers["BUF_AC4"]))
  1529. print ( "CAL_AC5 = %6d" % (self.baro_registers["BUF_AC5"]))
  1530. print ( "CAL_AC6 = %6d" % (self.baro_registers["BUF_AC6"]))
  1531. print ( " CAL_B1 = %6d" % (self.baro_registers["BUF_B1"]))
  1532. print ( " CAL_B2 = %6d" % (self.baro_registers["BUF_B2"]))
  1533. print ( " CAL_MB = %6d" % (self.baro_registers["BUF_MB"]))
  1534. print ( " CAL_MC = %6d" % (self.baro_registers["BUF_MC"]))
  1535. print ( " CAL_MD = %6d" % (self.baro_registers["BUF_MD"]))
  1536.  
  1537. def baro_getRawPressure(self):
  1538. write8(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers["CONTROL"], self.baro_registers["READPRESSURECMD"] + (self.mode << 6))
  1539.  
  1540. if (self.mode == self.baro_registers["ULTRALOWPOWER"]):
  1541. time.sleep(0.005)
  1542. elif (self.mode == self.baro_registers["HIGHRES"]):
  1543. time.sleep(0.014)
  1544. elif (self.mode == self.baro_registers["ULTRAHIGHRES"]):
  1545. time.sleep(0.026)
  1546. else:
  1547. time.sleep(0.008)
  1548.  
  1549. msb = readU8(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers["PRESSUREDATA"])
  1550. lsb = readU8(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers["PRESSUREDATA"]+1)
  1551. xlsb = readU8(self.i2c_bus,self.baro_registers['I2C_ADDR'],self.baro_registers["PRESSUREDATA"]+2)
  1552.  
  1553. raw = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - self.mode)
  1554. return raw
  1555.  
  1556. def baro_getPressure(self):
  1557. "Gets the compensated pressure in pascal"
  1558. UT = 0
  1559. UP = 0
  1560. B3 = 0
  1561. B5 = 0
  1562. B6 = 0
  1563. X1 = 0
  1564. X2 = 0
  1565. X3 = 0
  1566. p = 0
  1567. B4 = 0
  1568. B7 = 0
  1569.  
  1570. UT = self.baro_getRawTemperature()
  1571. UP = self.baro_getRawPressure()
  1572.  
  1573. # True Temperature Calculations
  1574. X1 = ((UT - self.baro_registers["CAL_AC6"]) * self.baro_registers["CAL_AC5"]) >> 15
  1575. X2 = (self.baro_registers["CAL_MC"] << 11) / (X1 + self.baro_registers["CAL_MD"])
  1576. B5 = X1 + X2
  1577.  
  1578. # Pressure Calculations
  1579. B6 = B5 - 4000
  1580. X1 = (self.baro_registers["CAL_B2"] * (B6 * B6) >> 12) >> 11
  1581. X2 = (self.baro_registers["CAL_AC2"] * B6) >> 11
  1582. X3 = X1 + X2
  1583. B3 = (((self.baro_registers["CAL_AC1"] * 4 + X3) << self.mode) + 2) / 4
  1584.  
  1585. X1 = (self.baro_registers["CAL_AC3"] * B6) >> 13
  1586. X2 = (self.baro_registers["CAL_B1"] * ((B6 * B6) >> 12)) >> 16
  1587. X3 = ((X1 + X2) + 2) >> 2
  1588. B4 = (self.baro_registers["CAL_AC4"] * (X3 + 32768)) >> 15
  1589. B7 = (UP - B3) * (50000 >> self.mode)
  1590.  
  1591.  
  1592. if (B7 < 0x80000000):
  1593. p = (B7 * 2) / B4
  1594. else:
  1595. p = (B7 / B4) * 2
  1596.  
  1597. X1 = (p >> 8) * (p >> 8)
  1598. X1 = (X1 * 3038) >> 16
  1599. X2 = (-7357 * p) >> 16
  1600.  
  1601. p = p + ((X1 + X2 + 3791) >> 4)
  1602.  
  1603. return p
  1604.  
  1605. def baro_getAltitude(self, seaLevelPressure=101325):
  1606. "Calculates the altitude in meters"
  1607. altitude = 0.0
  1608. pressure = float(self.baro_getPressure())
  1609. altitude = 44330.0 * (1.0 - pow(pressure / seaLevelPressure, 0.1903))
  1610. return altitude
  1611.  
  1612. # COMPASS functions
  1613.  
  1614. #Part of this code become from Think Bowl I2C Libraries
  1615. #http://think-bowl.com/raspberry-pi/installing-the-think-bowl-i2c-libraries-for-python/
  1616.  
  1617. def compass_setContinuousMode(self):
  1618. self.setOption(self.compass_registers["MODE_REG"], self.MeasurementContinuous)
  1619.  
  1620. def compass_setScale(self, gauss):
  1621. if gauss == 0.88:
  1622. self.scale_reg = 0x00
  1623. self.scale = 0.73
  1624. elif gauss == 1.3:
  1625. self.scale_reg = 0x01
  1626. self.scale = 0.92
  1627. elif gauss == 1.9:
  1628. self.scale_reg = 0x02
  1629. self.scale = 1.22
  1630. elif gauss == 2.5:
  1631. self.scale_reg = 0x03
  1632. self.scale = 1.52
  1633. elif gauss == 4.0:
  1634. self.scale_reg = 0x04
  1635. self.scale = 2.27
  1636. elif gauss == 4.7:
  1637. self.scale_reg = 0x05
  1638. self.scale = 2.56
  1639. elif gauss == 5.6:
  1640. self.scale_reg = 0x06
  1641. self.scale = 3.03
  1642. elif gauss == 8.1:
  1643. self.scale_reg = 0x07
  1644. self.scale = 4.35
  1645.  
  1646. self.scale_reg = self.scale_reg << 5
  1647. self.setOption(self.compass_registers["CONF_REG_B"], self.scale_reg)
  1648.  
  1649. def compass_setDeclination(self, degree, min = 0):
  1650. self.declinationDeg = degree
  1651. self.declinationMin = min
  1652. self.declination = (degree+min/60) * (math.pi/180)
  1653.  
  1654. def setOption(self, register, *function_set):
  1655. options = 0x00
  1656. for function in function_set:
  1657. options = options | function
  1658. self.i2c_bus.write_byte_data(self.compass_address,register, options)
  1659.  
  1660. # Adds to existing options of register
  1661. def addOption(self, register, *function_set):
  1662. options = self.i2c_bus.read_byte_data(self.compass_address,register)
  1663. for function in function_set:
  1664. options = options | function
  1665. self.i2c_bus.write_byte_data(self.compass_address,register, options)
  1666.  
  1667. # Removes options of register
  1668. def removeOption(self, register, *function_set):
  1669. options = self.i2c_bus.read_byte_data(self.compass_address,register)
  1670. for function in function_set:
  1671. options = options & (function ^ 0b11111111)
  1672. self.i2c_bus.write_byte_data(self.compass_address,register, options)
  1673.  
  1674. def compass_getDeclination(self):
  1675. return (self.declinationDeg, self.declinationMin)
  1676.  
  1677. def compass_getDeclinationString(self):
  1678. return str(self.declinationDeg)+"\u00b0 "+str(self.declinationMin)+"'"
  1679.  
  1680. # Returns heading in degrees and minutes
  1681. def compass_getHeading(self):
  1682. (scaled_x, scaled_y, scaled_z) = self.compass_getAxes()
  1683.  
  1684. headingRad = math.atan2(scaled_y, scaled_x)
  1685. headingRad += self.declination
  1686.  
  1687. # Correct for reversed heading
  1688. if(headingRad < 0):
  1689. headingRad += 2*math.pi
  1690.  
  1691. # Check for wrap and compensate
  1692. if(headingRad > 2*math.pi):
  1693. headingRad -= 2*math.pi
  1694.  
  1695. # Convert to degrees from radians
  1696. headingDeg = headingRad * 180/math.pi
  1697. degrees = math.floor(headingDeg)
  1698. minutes = round(((headingDeg - degrees) * 60))
  1699. return (degrees, minutes)
  1700.  
  1701. def compass_getHeadingString(self):
  1702. (degrees, minutes) = self.compass_getHeading()
  1703. return str(degrees)+"\u00b0 "+str(minutes)+"'"
  1704.  
  1705. def compass_getAxes(self):
  1706. #Read X axis value
  1707. self.i2c_bus.write_byte(self.compass_address,self.compass_registers['OUT_X_L'])
  1708. OUT_X_L=self.i2c_bus.read_byte(self.compass_address)
  1709. self.i2c_bus.write_byte(self.compass_address,self.compass_registers['OUT_X_H'])
  1710. OUT_X_H=self.i2c_bus.read_byte(self.compass_address)
  1711. xValue=self.getSignedNumber(OUT_X_H<<8 | OUT_X_L)
  1712.  
  1713. #Read Y axis value
  1714. self.i2c_bus.write_byte(self.compass_address,self.compass_registers['OUT_Y_L'])
  1715. OUT_Y_L=self.i2c_bus.read_byte(self.compass_address)
  1716. self.i2c_bus.write_byte(self.compass_address,self.compass_registers['OUT_Y_H'])
  1717. OUT_Y_H=self.i2c_bus.read_byte(self.compass_address)
  1718. yValue=self.getSignedNumber(OUT_Y_H<<8 | OUT_Y_L)
  1719.  
  1720. #Read Z axis value
  1721. self.i2c_bus.write_byte(self.compass_address,self.compass_registers['OUT_Z_L'])
  1722. OUT_Z_L=self.i2c_bus.read_byte(self.compass_address)
  1723. self.i2c_bus.write_byte(self.compass_address,self.compass_registers['OUT_Z_H'])
  1724. OUT_Z_H=self.i2c_bus.read_byte(self.compass_address)
  1725. zValue=self.getSignedNumber(OUT_Z_H<<8 | OUT_Z_L)
  1726.  
  1727. if (xValue == -4096):
  1728. xValue = None
  1729. else:
  1730. xValue = round(xValue * self.scale, 4)
  1731.  
  1732. if (yValue == -4096):
  1733. yValue = None
  1734. else:
  1735. yValue = round(yValue * self.scale, 4)
  1736.  
  1737. if (zValue == -4096):
  1738. zValue = None
  1739. else:
  1740. zValue = round(zValue * self.scale, 4)
  1741.  
  1742. return (xValue, yValue, zValue)
  1743.  
  1744. ## DAISY-8 #############################################################
  1745.  
  1746. class Daisy8():
  1747.  
  1748. """
  1749. DAISY-8 (2 Relay - 2 input module) related class
  1750. http://www.acmesystems.it/DAISY-8
  1751. """
  1752. kernel_id=-1
  1753. fd=None
  1754.  
  1755.  
  1756. line_first = {
  1757. 'RL0' : '2',
  1758. 'RL1' : '3',
  1759. 'IN0' : '4',
  1760. 'IN1' : '5',
  1761. }
  1762.  
  1763. line_second = {
  1764. 'RL0' : '6',
  1765. 'RL1' : '7',
  1766. 'IN0' : '8',
  1767. 'IN1' : '9',
  1768. }
  1769.  
  1770. def __init__(self,connector="D11",position="first",id="RL0"):
  1771. if (position=="first"):
  1772. pin=self.line_first[id]
  1773. else:
  1774. pin=self.line_second[id]
  1775.  
  1776. self.kernel_id = pinname2kernelid(connector + "." + pin)
  1777.  
  1778. if (self.kernel_id!=0 and id[0:2]=="RL"):
  1779. export(self.kernel_id)
  1780. direction(self.kernel_id,'low')
  1781.  
  1782. if (self.kernel_id!=0 and id[0:2]=="IN"):
  1783. export(self.kernel_id)
  1784. direction(self.kernel_id,'in')
  1785.  
  1786. iopath=get_gpio_path(self.kernel_id)
  1787. if os.path.exists(iopath):
  1788. self.fd = open(iopath + '/value','r')
  1789.  
  1790. def on(self):
  1791. if (self.kernel_id!=0):
  1792. set_value(self.kernel_id,1)
  1793. else:
  1794. pass
  1795.  
  1796. def off(self):
  1797. if (self.kernel_id!=0):
  1798. set_value(self.kernel_id,0)
  1799. else:
  1800. pass
  1801.  
  1802. def get(self):
  1803. if self.fd!=None:
  1804. self.fd.seek(0)
  1805. a=self.fd.read()
  1806. if int(a)==0:
  1807. return False
  1808. else:
  1809. return True
  1810. return False
  1811.  
  1812. def wait_edge(self,fd,callback):
  1813. counter=0
  1814. po = select.epoll()
  1815. po.register(fd,select.EPOLLET)
  1816. while True:
  1817. events = po.poll()
  1818. if counter>0:
  1819. callback()
  1820. counter=counter+1
  1821.  
  1822. def set_edge(self,value,callback):
  1823. if self.fd!=None:
  1824. set_edge(self.kernel_id,value)
  1825. thread.start_new_thread(self.wait_edge,(self.fd,callback))
  1826. return
  1827. else:
  1828. thread.exit()
  1829.  
  1830. ## DAISY-10 ############################################################
  1831.  
  1832. class Daisy10():
  1833.  
  1834. """
  1835. DAISY-10 (RS422/RS485) related class
  1836. http://www.acmesystems.it/DAISY-10
  1837. """
  1838.  
  1839. global serial_ports
  1840. serial = None
  1841. fd=None
  1842. timeout = 0
  1843.  
  1844. def __init__(self, *args, **kwargs):
  1845. #print serial_ports[kwargs.get('port')]
  1846. kwargs['port'] = serial_ports[kwargs.get('port')]
  1847. self.serial = serial.Serial(*args, **kwargs)
  1848. #Serial.__init__(self, *args, **kwargs)
  1849. self.buf = ''
  1850. self.timeout = self.serial.timeout
  1851.  
  1852. def mode(self,mode):
  1853. if mode=="RS485":
  1854. #Read these doc to understand this part
  1855. #http://lxr.free-electrons.com/source/Documentation/serial/serial-rs485.txt
  1856. #http://docs.python.org/2/library/struct.html
  1857. fd=self.serial.fileno()
  1858. serial_rs485 = struct.pack('hhhhhhhh', 1, 0, 0, 0, 0, 0, 0, 0)
  1859. fcntl.ioctl(fd,0x542F,serial_rs485)
  1860. if mode=="RS422":
  1861. fd=self.serial.fileno()
  1862. serial_rs485 = struct.pack('hhhhhhhh', 0, 0, 0, 0, 0, 0, 0, 0)
  1863. fcntl.ioctl(fd,0x542F,serial_rs485)
  1864.  
  1865. def flushInput(self):
  1866. self.serial.flushInput()
  1867.  
  1868. def write(self, msg):
  1869. self.serial.write(msg)
  1870.  
  1871. def read(self, num_bytes):
  1872. self.serial.read(num_bytes)
  1873.  
  1874.  
  1875.  
  1876. ## DAISY-11 ############################################################
  1877.  
  1878. class Daisy11():
  1879.  
  1880. """
  1881. DAISY-11 (8 led) related class
  1882. http://www.acmesystems.it/DAISY-11
  1883. """
  1884.  
  1885. kernel_id=-1
  1886.  
  1887. leds = {
  1888. 'L1' : '2',
  1889. 'L2' : '3',
  1890. 'L3' : '4',
  1891. 'L4' : '5',
  1892. 'L5' : '6',
  1893. 'L6' : '7',
  1894. 'L7' : '8',
  1895. 'L8' : '9',
  1896. }
  1897.  
  1898. def __init__(self,connector_id,led_id):
  1899. pin=self.leds[led_id]
  1900. self.kernel_id = pinname2kernelid(connector_id + "." + pin)
  1901.  
  1902. if (self.kernel_id!=0):
  1903. export(self.kernel_id)
  1904. direction(self.kernel_id,'low')
  1905.  
  1906.  
  1907. def on(self):
  1908. if (self.kernel_id!=0):
  1909. set_value(self.kernel_id,1)
  1910. else:
  1911. pass
  1912.  
  1913.  
  1914. def off(self):
  1915. if (self.kernel_id!=0):
  1916. set_value(self.kernel_id,0)
  1917. else:
  1918. pass
  1919.  
  1920. def get(self):
  1921. if get_value(self.kernel_id):
  1922. return True
  1923. else:
  1924. return False
  1925.  
  1926. ## DAISY-14 ############################################################
  1927.  
  1928. class Daisy14():
  1929.  
  1930. """
  1931. DAISY-14 (I2C LCD adapter)
  1932. http://www.acmesystems.it/DAISY-14
  1933. """
  1934.  
  1935. i2c_bus=-1
  1936. i2c_address = -1
  1937. backled = -1
  1938. rs = -1
  1939. e = -1
  1940.  
  1941. def __init__(self,bus_id=0,i2c_address=0x20):
  1942. self.i2c_address = i2c_address
  1943. self.i2c_bus = smbus.SMBus(bus_id)
  1944. self.rs=Daisy22(bus_id,i2c_address,4)
  1945. self.e=Daisy22(bus_id,i2c_address,5)
  1946. self.rs.off()
  1947. self.e.off()
  1948. time.sleep(0.015)
  1949.  
  1950. #LCD initialization sequence
  1951. #http://web.alfredstate.edu/weimandn/lcd/lcd_initialization/lcd_initialization_index.html
  1952.  
  1953. self.sendnibble(0x03)
  1954. self.sendnibble(0x03)
  1955. self.sendnibble(0x03)
  1956. self.sendnibble(0x02)
  1957.  
  1958. #4 bit interface
  1959. #2 lines display
  1960. self.sendcommand(0x28)
  1961.  
  1962. #Command ENTRY MODE SET
  1963. #Increase 0x02
  1964. #Display is shifted 0x01
  1965. self.sendcommand(0x06+0x02)
  1966.  
  1967. #Command DISPLAY ON/OFF
  1968. #Display ON 0x04
  1969. #Cursor OFF 0x02
  1970. #Blinking OFF 0x01
  1971. self.sendcommand(0x08+0x04)
  1972.  
  1973. #Command DISPLAY CLEAR
  1974. self.sendcommand(0x01)
  1975.  
  1976. self.backled=Daisy22(bus_id,i2c_address,6)
  1977. return
  1978.  
  1979. def e_strobe(self):
  1980. self.e.on()
  1981. self.e.off()
  1982.  
  1983. def sendnibble(self,value):
  1984. currentvalue=self.i2c_bus.read_byte(self.i2c_address)
  1985. self.i2c_bus.write_byte(self.i2c_address,value&0x0F|currentvalue&0xF0)
  1986. self.e_strobe()
  1987. return
  1988.  
  1989. def sendcommand(self,value):
  1990. self.rs.off()
  1991. self.sendnibble((value>>4)&0x0F)
  1992. self.sendnibble(value&0x0F)
  1993. return
  1994.  
  1995. def senddata(self,value):
  1996. self.rs.on()
  1997. self.sendnibble((value>>4)&0x0F)
  1998. self.sendnibble(value&0x0F)
  1999. return
  2000.  
  2001. def clear(self):
  2002. """
  2003. Clear the display content
  2004. """
  2005. self.sendcommand(0x01)
  2006. time.sleep(0.001)
  2007. return
  2008.  
  2009. def home(self):
  2010. """
  2011. Place the curson at home position
  2012. """
  2013. self.sendcommand(0x03)
  2014. time.sleep(0.001)
  2015. return
  2016.  
  2017. def setcurpos(self,x,y):
  2018. if y<0 or y>3:
  2019. return
  2020. if x<0 or x>19:
  2021. return
  2022.  
  2023. if y==0:
  2024. self.sendcommand(0x80+0x00+x)
  2025. if y==1:
  2026. self.sendcommand(0x80+0x40+x)
  2027. if y==2:
  2028. self.sendcommand(0x80+0x14+x)
  2029. if y==3:
  2030. self.sendcommand(0x80+0x54+x)
  2031. return
  2032.  
  2033. def putchar(self,value):
  2034. self.senddata(value)
  2035. return
  2036.  
  2037. def putstring(self,string):
  2038. if len(string)==0:
  2039. return
  2040. if len(string)>20:
  2041. string=string[0:20]
  2042.  
  2043. for char in string:
  2044. self.putchar(ord(char))
  2045. return
  2046.  
  2047. def backlighton(self):
  2048. self.backled.on()
  2049. return
  2050.  
  2051. def backlightoff(self):
  2052. self.backled.off()
  2053. return
  2054.  
  2055. ## DAISY-15 ############################################################
  2056.  
  2057. class Daisy15():
  2058.  
  2059. """
  2060. DAISY-15 (4DSystems lcd display) related class
  2061. http://www.acmesystems.it/DAISY-15
  2062. """
  2063.  
  2064. serial = None
  2065.  
  2066. def __init__(self,connector_id):
  2067. self.serial = serial.Serial(
  2068. port=serial_ports[connector_id],
  2069. baudrate=9600,
  2070. timeout=1,
  2071. parity=serial.PARITY_NONE,
  2072. stopbits=serial.STOPBITS_ONE,
  2073. bytesize=serial.EIGHTBITS
  2074. )
  2075.  
  2076. self.serial.write("U") # Autobaud char
  2077. rtc = self.serial.read(1) # Wait for a reply
  2078.  
  2079. self.serial.write("E") # Clear screen
  2080. rtc = self.serial.read(1) # Wait for a reply
  2081.  
  2082. def send(self,col,row,str):
  2083. self.serial.write("s%c%c%c%c%c%s%c" % (int(row),int(col),2,0xFF,0xFF,str,0x00))
  2084. rtc = self.serial.read(1)
  2085.  
  2086. ## DAISY-18 ############################################################
  2087.  
  2088. class Daisy18():
  2089.  
  2090. """
  2091. DAISY-18 (4 mosfet input) related class
  2092. http://www.acmesystems.it/DAISY-18
  2093. """
  2094.  
  2095. fd=None
  2096. kernel_id=-1
  2097.  
  2098. line_first = {
  2099. 'CH1' : '2',
  2100. 'CH2' : '3',
  2101. 'CH3' : '4',
  2102. 'CH4' : '5',
  2103. 'I1' : '2',
  2104. 'I2' : '3',
  2105. 'I3' : '4',
  2106. 'I4' : '5'
  2107. }
  2108.  
  2109. line_second = {
  2110. 'CH1' : '6',
  2111. 'CH2' : '7',
  2112. 'CH3' : '8',
  2113. 'CH4' : '9',
  2114. 'I1' : '6',
  2115. 'I2' : '7',
  2116. 'I3' : '8',
  2117. 'I4' : '9'
  2118. }
  2119.  
  2120.  
  2121. def __init__(self,connector="D11",position="first",id="CH1"):
  2122. if (position=="first"):
  2123. pin=self.line_first[id]
  2124. else:
  2125. pin=self.line_second[id]
  2126.  
  2127. self.kernel_id = pinname2kernelid(connector + "." + pin)
  2128.  
  2129. export(self.kernel_id)
  2130. direction(self.kernel_id,'in')
  2131.  
  2132. iopath=get_gpio_path(self.kernel_id)
  2133. if os.path.exists(iopath):
  2134. self.fd = open(iopath + '/value','r')
  2135.  
  2136. def get(self):
  2137. if self.fd!=None:
  2138. self.fd.seek(0)
  2139. a=self.fd.read()
  2140. if int(a)==0:
  2141. return False
  2142. else:
  2143. return True
  2144. return False
  2145.  
  2146. def state(self):
  2147. return self.get()
  2148.  
  2149. def wait_edge(self,fd,callback):
  2150. counter=0
  2151. po = select.epoll()
  2152. po.register(fd,select.EPOLLET)
  2153. while True:
  2154. events = po.poll()
  2155. if counter>0:
  2156. callback()
  2157. counter=counter+1
  2158.  
  2159. def set_edge(self,value,callback):
  2160. if self.fd!=None:
  2161. set_edge(self.kernel_id,value)
  2162. thread.start_new_thread(self.wait_edge,(self.fd,callback))
  2163. return
  2164. else:
  2165. thread.exit()
  2166.  
  2167. ## DAISY-19 ############################################################
  2168.  
  2169. class Daisy19():
  2170.  
  2171. """
  2172. DAISY-19 (4 mosfet output) related class
  2173. http://www.acmesystems.it/DAISY-19
  2174. """
  2175.  
  2176. kernel_id=-1
  2177.  
  2178. outputs_first = {
  2179. 'CH1' : '2',
  2180. 'CH2' : '3',
  2181. 'CH3' : '4',
  2182. 'CH4' : '5',
  2183. 'O1' : '2',
  2184. 'O2' : '3',
  2185. 'O3' : '4',
  2186. 'O4' : '5'
  2187. }
  2188.  
  2189. outputs_second = {
  2190. 'CH1' : '6',
  2191. 'CH2' : '7',
  2192. 'CH3' : '8',
  2193. 'CH4' : '9',
  2194. 'O1' : '6',
  2195. 'O2' : '7',
  2196. 'O3' : '8',
  2197. 'O4' : '9'
  2198. }
  2199.  
  2200. def __init__(self,connector_id,position,output_id):
  2201. if (position=="first"):
  2202. pin=self.outputs_first[output_id]
  2203. else:
  2204. pin=self.outputs_second[output_id]
  2205.  
  2206. self.kernel_id = pinname2kernelid(connector_id + "." + pin)
  2207.  
  2208. if (self.kernel_id!=0):
  2209. export(self.kernel_id)
  2210. direction(self.kernel_id,'low')
  2211.  
  2212.  
  2213. def on(self):
  2214. if (self.kernel_id!=0):
  2215. set_value(self.kernel_id,1)
  2216. else:
  2217. pass
  2218.  
  2219. def off(self):
  2220. if (self.kernel_id!=0):
  2221. set_value(self.kernel_id,0)
  2222. else:
  2223. pass
  2224.  
  2225. def get(self):
  2226. if get_value(self.kernel_id):
  2227. return True
  2228. else:
  2229. return False
  2230.  
  2231. ## DAISY-20 ############################################################
  2232.  
  2233. class Daisy20():
  2234.  
  2235. """
  2236. DAISY-20 (ADC module)
  2237. http://www.acmesystems.it/DAISY-20
  2238. """
  2239.  
  2240. maxvoltage=0
  2241. volt_per_point=0
  2242. adcpath="/sys/bus/platform/devices/at91_adc/"
  2243.  
  2244. def __init__(self,maxvoltage=10):
  2245. self.maxvoltage=maxvoltage
  2246. self.volt_per_point=float(maxvoltage)/float(2**10)
  2247. return
  2248.  
  2249. def get(self,ch=0):
  2250. fd = open(self.adcpath + "chan" + str(ch),"r")
  2251. value = fd.read()
  2252. fd.close()
  2253. return(float(value)*self.volt_per_point)
  2254.  
  2255.  
  2256. ## DAISY-24 ############################################################
  2257.  
  2258. class Daisy24():
  2259.  
  2260. """
  2261. DAISY-24 (16x2 LCD module)
  2262. http://www.acmesystems.it/DAISY-24
  2263. """
  2264.  
  2265. i2c_bus=-1
  2266.  
  2267. lcd_address = 0x3E
  2268.  
  2269. # I2C expansion address can be:
  2270. # PCF8574T 0x27
  2271. # PCF8574AT 0x3F
  2272. exp_address = 0x27
  2273.  
  2274. backled = -1
  2275. K0 = -1
  2276. K1 = -1
  2277. K2 = -1
  2278. K3 = -1
  2279.  
  2280. def __init__(self,bus_id=0,exp_address=-1):
  2281. self.exp_address = exp_address
  2282. self.i2c_bus = smbus.SMBus(bus_id)
  2283. self.sendcommand(0x38)
  2284. self.sendcommand(0x39)
  2285. self.sendcommand(0x14) #Internal OSC freq
  2286. self.sendcommand(0x72) #Set contrast
  2287. self.sendcommand(0x54) #Power/ICON control/Contrast set
  2288. self.sendcommand(0x6F) #Follower control
  2289. self.sendcommand(0x0C) #Display ON
  2290. self.clear()
  2291.  
  2292. #if (exp_address==-1):
  2293. # try:
  2294. # self.K0=Daisy22(bus_id,exp_address,0)
  2295. # catch:
  2296.  
  2297. self.K0=Daisy22(bus_id,exp_address,0)
  2298. self.K1=Daisy22(bus_id,exp_address,1)
  2299. self.K2=Daisy22(bus_id,exp_address,2)
  2300. self.K3=Daisy22(bus_id,exp_address,3)
  2301.  
  2302. self.backled=Daisy22(bus_id,exp_address,4)
  2303. return
  2304.  
  2305. def sendcommand(self,value):
  2306. self.i2c_bus.write_byte_data(self.lcd_address,0x00,value)
  2307. return
  2308.  
  2309. def senddata(self,value):
  2310. self.i2c_bus.write_byte_data(self.lcd_address,0x40,value)
  2311. return
  2312.  
  2313. def clear(self):
  2314. """
  2315. CLear the display content
  2316. """
  2317. self.sendcommand(0x01)
  2318. time.sleep(0.001)
  2319. return
  2320.  
  2321. def home(self):
  2322. """
  2323. Place the curson at home position
  2324. """
  2325. self.sendcommand(0x03)
  2326. time.sleep(0.001)
  2327. return
  2328.  
  2329. def setcontrast(self,value):
  2330. """
  2331. Set the display contrast
  2332. value = 0 to 15
  2333. """
  2334. self.sendcommand(0x70 + value)
  2335. return
  2336.  
  2337. def setdoublefont(self):
  2338. self.sendcommand(0x30 + 0x0C + 0x01)
  2339. return
  2340.  
  2341. def setsinglefont(self):
  2342. self.sendcommand(0x30 + 0x08 + 0x01)
  2343. return
  2344.  
  2345. def setcurpos(self,x,y):
  2346. if y<0 or y>1:
  2347. return
  2348. if x<0 or x>15:
  2349. return
  2350.  
  2351. if y==0:
  2352. self.sendcommand(0x80+0x00+x)
  2353. else:
  2354. self.sendcommand(0x80+0x40+x)
  2355. return
  2356.  
  2357. def putchar(self,value):
  2358. self.senddata(value)
  2359. return
  2360.  
  2361. def putstring(self,string):
  2362. if len(string)==0:
  2363. return
  2364. if len(string)>16:
  2365. string=string[0:16]
  2366.  
  2367. for char in string:
  2368. self.putchar(ord(char))
  2369. return
  2370.  
  2371. def backlighton(self):
  2372. self.backled.on()
  2373. return
  2374.  
  2375. def backlightoff(self):
  2376. self.backled.off()
  2377. return
  2378.  
  2379. def pressed(self,keyid):
  2380. if keyid==0:
  2381. return self.K0.pressed()
  2382. if keyid==1:
  2383. return self.K1.pressed()
  2384. if keyid==2:
  2385. return self.K2.pressed()
  2386. if keyid==3:
  2387. return self.K3.pressed()
  2388.  
  2389. return False
  2390.  
  2391. #--------------------------------------------------------------
  2392.  
  2393. w1path = "/sys/bus/w1/devices/w1 bus master"
  2394.  
  2395. def w1buslist():
  2396.  
  2397. if not os.path.exists(w1path):
  2398. print ("1-wire bus not found")
  2399. print ("Check if the 1-wire bus is installed")
  2400. return
  2401.  
  2402. deviceList = os.listdir(w1path)
  2403.  
  2404. # for deviceId in deviceList:
  2405. # print deviceId
  2406.  
  2407. return [deviceId[3:] for deviceId in deviceList if deviceId[0:2]=="28"]
  2408.  
  2409. class DS18B20():
  2410.  
  2411. sensor_path=""
  2412.  
  2413. def __init__(self,w1Id):
  2414. if not os.path.exists(w1path):
  2415. print ("1-wire bus not found")
  2416. return
  2417.  
  2418. self.sensor_path = os.path.join(w1path,"28-" + w1Id)
  2419.  
  2420. if not os.path.exists(self.sensor_path):
  2421. print ("Sensor %s not found" % (w1Id))
  2422. return
  2423.  
  2424. # print self.sensor_path
  2425.  
  2426. def getTemp(self):
  2427.  
  2428. f = open(self.sensor_path + '/w1_slave','r')
  2429. tString=f.read()
  2430. f.close()
  2431.  
  2432. if tString.find("NO")>=0:
  2433. print ("Wrong CRC")
  2434. return
  2435.  
  2436. p=tString.find("t=")
  2437. return float(tString[p+2:-1])/1000
  2438.  
  2439. class DS28EA00():
  2440.  
  2441. sensor_path=""
  2442.  
  2443. def __init__(self,w1Id):
  2444. if not os.path.exists(w1path):
  2445. print ("1-wire bus not found")
  2446. return
  2447.  
  2448. self.sensor_path = os.path.join(w1path,"42-" + w1Id)
  2449.  
  2450. if not os.path.exists(self.sensor_path):
  2451. print ("Sensor %s not found" % (w1Id))
  2452. return
  2453.  
  2454. # print self.sensor_path
  2455.  
  2456. def getTemp(self):
  2457.  
  2458. f = open(self.sensor_path + '/therm','r')
  2459. tString=f.read()
  2460. f.close()
  2461.  
  2462. if tString.find("NO")>=0:
  2463. print ("Wrong CRC")
  2464. return
  2465.  
  2466. p=tString.find("t=")
  2467. return float(tString[p+2:-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement