HDC1080 support
[owSlave2.git] / tools / owReadBMP.py
1 #!/usr/bin/python\r
2 # Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de\r
3 # All rights reserved. \r
4\r
5 # Redistribution and use in source and binary forms, with or without \r
6 # modification, are permitted provided that the following conditions are \r
7 # met: \r
8\r
9 #  * Redistributions of source code must retain the above copyright \r
10 #    notice, this list of conditions and the following disclaimer. \r
11 #  * Redistributions in binary form must reproduce the above copyright \r
12 #    notice, this list of conditions and the following disclaimer in the \r
13 #    documentation and/or other materials provided with the \r
14 #    distribution. \r
15 #  * All advertising materials mentioning features or use of this \r
16 #    software must display the following acknowledgement: This product \r
17 #    includes software developed by tm3d.de and its contributors. \r
18 #  * Neither the name of tm3d.de nor the names of its contributors may \r
19 #    be used to endorse or promote products derived from this software \r
20 #    without specific prior written permission. \r
21\r
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR \r
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \r
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, \r
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT \r
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, \r
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY \r
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \r
33 \r
34 \r
35 \r
36 \r
37 import time\r
38 import subprocess\r
39 import sys\r
40 \r
41 def owcom(dev,send,rc):\r
42         res=[]\r
43         f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)\r
44         f.write("".join(map(chr, send)))\r
45         if (rc!=0):\r
46                 res=map(ord,f.read(rc))\r
47         f.close()\r
48         return res\r
49                 \r
50 \r
51 def crc8(arr):\r
52         lscrc=0x0;\r
53         for v in arr:\r
54                 bit=1;\r
55                 while bit<256:\r
56                         if (v&bit)==bit:\r
57                                 lb=1\r
58                         else:\r
59                                 lb=0\r
60                         if (lscrc&1)!=lb:\r
61                                 lscrc=(lscrc>>1)^0x8c \r
62                         else:\r
63                                 lscrc=(lscrc>>1)\r
64                         bit=bit*2\r
65         return lscrc\r
66         \r
67 def testnr(s):\r
68         for c in s.lower()[:]:\r
69                 if not((c) in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','-']):\r
70                         return False\r
71         return True\r
72  \r
73 def addid(id,val):\r
74         for i in range(7):\r
75                 id[i+1]=id[i+1]+val\r
76                 if id[i+1]>254:\r
77                         id[i+1]=id[i+1]-254\r
78                         val=1\r
79                 else:\r
80                         return id\r
81         return id\r
82 #\r
83 \r
84 \r
85 l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)\r
86 dc=0\r
87 dl=[]\r
88 for g in  (l.split("\n")):\r
89         if len(g)>2:\r
90                 if testnr(g[0:2]):\r
91                         if (g[0:2]=="26"):\r
92                                 dl.append(g)\r
93                                 dc=dc+1\r
94                                 print dc,") ",g\r
95 if dc==0:\r
96         print "No 26 1-Wire Device found"\r
97         exit(0)\r
98 n=int(raw_input("No. of Device (28-Device will get automatically): "))\r
99 n=n-1\r
100 if (n>dc-1)or(n<0):\r
101                 exit(0)\r
102 s=dl[n]\r
103 config=owcom(s,[0x85],17)\r
104 print(config)\r
105 s2="%02x-%02x%02x%02x%02x%02x%02x" % (config[9],config[15],config[14],config[13],config[12],config[11],config[10])\r
106 print("Second Device: %s" %( s2))\r
107 f=open("/sys/bus/w1/devices/%s/w1_slave" %(s2),"rb",0)\r
108 res=f.read().split(" ")\r
109 f.close()\r
110 Press=((int(res[0],16)+int(res[1],16)*256)/16.0*3.2+700)\r
111 owcom(s,[0x4E,0x00,0x01],0)\r
112 owcom(s,[0x44],0)\r
113 time.sleep(1)\r
114 owcom(s,[0xB4],0)\r
115 time.sleep(1)\r
116 \r
117 p=owcom(s,[0xBE,0x00],10)\r
118 print (p)\r
119 T=((p[1]+p[2]*256)/256.0)\r
120 if (config[5]==8):\r
121         RH=((p[3]+p[4]*256)/100.0)\r
122 if (config[5]==7):\r
123         RH=(((p[3]+p[4]*256)/ 500.0 - 0.16) / 0.0062) / (1.0546 - 0.00216*T); \r
124 \r
125 print("Temperatur: %f °C" %(T));\r
126 print("Luftfeuchte: %f %%" %(RH));\r
127 print("Druck (absolut): %f hPa" %(Press));\r
128 h=float(raw_input("Höhe über Meerespiegel in m: "))\r
129 P0=Press/((1-h/44330)**5.255)\r
130 print("Druck (normalisiert): %f hPa" %(P0));\r