1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the
14 // * All advertising materials mentioning features or use of this
15 // software must display the following acknowledgement: This product
16 // includes software developed by tm3d.de and its contributors.
17 // * Neither the name of tm3d.de nor the names of its contributors may
18 // be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "owInterface.h"
37 owDevice::owDevice(owInterface *owi_,snum_t num) {
40 config=new owDeviceConfig;
41 configstate=OWDCS_NONE;
42 raw.insert(raw.begin(),4,0);
43 values.insert(values.begin(),4,0);
47 void owDevice::setDefaultConfig() {
51 std::vector<std::string> owDevice::getFamilyInfo() {
52 return owDeviceConfig::getFamilyInfo(snum.byte[0]);
54 int owDevice::readConfig() {
55 std::vector<uint8_t> cl;
56 int r=owi->maxrepeat+1;
63 Communicate(&cl, 1, 26);
64 //for (uint8_t v:cl) printf("%02X ",v);
65 if ((cl[1]==0xFF)||(cl[2]==0xFF)) {
66 owi->log->set(OWLOG_INFO,"Get Config not work, maybe not a emulation from www.tm3d.de");
71 for(int i=19;i<27;i++) if (cl[i]!=0xFF) {oldInfo=0;break;}
73 owi->log->set(OWLOG_INFO,"Old 16 Byte Config");
75 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+17))) {
76 owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
79 if (!owi->testCRC16(cl)) {
80 owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
83 if (owi->log->last()<OWLOG_WARNING) {
85 for(int i=0;i<7;i++) {cl[18+i]=cl[10+i];cl[10+i]=0;} //Move OWID2 Set Sensor to 0
91 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25))) {
92 owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
95 if (!owi->testCRC16(cl)) {
96 owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
102 while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
104 owi->log->set(OWLOG_ERROR,"To much Errors while read config");
107 config->setConfig(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25));
109 if (conf16) configstate=OWDCS_16; else configstate=OWDCS_24;
110 } else configstate=OWDCS_NONE;
115 int owDevice::Communicate(std::vector<uint8_t> *data, int scount, int rcount) {
118 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
119 owi->Communicate(data, scount, rcount);
120 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
125 int owDevice::CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount) {
128 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
129 owi->Communicate(data, scount, rcount);
130 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
136 void owDevice::changeID(snum_t nid) {
138 if (nid.byte[0] != snum.byte[0]) {
139 owi->log->set(OWLOG_ERROR,"Family ID should not be changed, no correct ID");
142 std::vector<uint8_t> id;
143 for(i=0;i<7;i++) id.push_back(nid.byte[i]);
144 id.push_back(owi->calcCRC8(id));
145 std::vector<uint8_t> cl;
146 int r=owi->maxrepeat+1;
151 cl.insert(cl.begin()+1,id.begin(),id.end());
152 Communicate(&cl, 9,0);
153 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
156 Communicate(&cl, 1, 8);
157 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
158 for (i = 0; i < 8; i++) {
159 if (cl[i + 1] != id[i]) {
160 owi->log->set(OWLOG_WARNING,"changeID Comunication ERROR");
166 while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
168 owi->log->set(OWLOG_ERROR,"Can not change ID");
173 cl.push_back(snum.byte[1]);
174 cl.push_back(snum.byte[5]);
175 cl.push_back(snum.byte[6]);
176 Communicate(&cl, 4, 0);
180 void owDevice::runFlasher() {
181 std::vector<uint8_t> cl;
183 this->Communicate(&cl, 1, 0);
184 this->Communicate(&cl, 1, 0);
185 this->Communicate(&cl, 1, 0);
196 void owDeviceDS18B20::setDefaultConfig() {
197 config->setConfig({1,1, 0,0, 0,0, 0,0, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0});
198 configstate=OWDCS_DEFAULT;
201 int owDeviceDS18B20::readScratchpad(std::vector<uint8_t> *sp) {
202 std::vector<uint8_t> cl;
203 int r=owi->maxrepeat+1;
207 Communicate(&cl, 1, 9);
208 if (owi->log->last()>=OWLOG_ERROR) return -1;
210 sp->insert(sp->begin(),cl.begin()+1,cl.end());
211 if (owi->calcCRC8(*sp)==0) return 1;
212 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS18B20 Scrachpad");
214 } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
219 int owDeviceDS18B20::convertAll() {
220 std::vector<uint8_t> sp;
222 Communicate(&sp, 1, 0);
225 if (readScratchpad(&sp)) {
227 tsht = sp[0] | ((int)sp[1] << 8);
231 values[0]=config->calculateValue(0, raw);
238 void owDeviceDS2438::setDefaultConfig() {
239 config->setConfig({1,6, 6,8, 4,7, 7,17, 0,2,3,12,4,0,0,0,0,0,0,0,0,0,0,0});
240 configstate=OWDCS_DEFAULT;
244 int owDeviceDS2438::readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall) {
245 std::vector<uint8_t> cl;
246 int r=owi->maxrepeat+1;
250 cl.push_back(0xB8); //recall
252 Communicate(&cl, 2, 0);
253 if (owi->log->last()>=OWLOG_ERROR) return -1;
258 Communicate(&cl, 2, 9);
259 if (owi->log->last()>=OWLOG_ERROR) return -1;
261 sp->insert(sp->begin(),cl.begin()+2,cl.end());
262 if (owi->calcCRC8(*sp)==0) return 1;
263 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
265 } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
270 int owDeviceDS2438::setConfigByte(uint8_t cb) {
271 std::vector<uint8_t> sp;
273 for(k=0;k<owi->maxrepeat;k++) {
279 Communicate(&sp,3,0);
280 if (owi->log->last()>=OWLOG_ERROR) return -1;
282 readScratchpad(&sp,0,0);
283 if (owi->log->last()>=OWLOG_ERROR) return -2;
284 if (cb==sp[0]) return 1;
285 owi->log->set(OWLOG_WARNING,"ERROR set config byte of DS2438");
287 owi->log->set(OWLOG_WARNING,"Config of DS2438 byte not set");
291 inline int16_t ow_fconvert(uint8_t b1, uint16_t b2) {
293 tsht=b1 |((int)b2<<8);
299 int owDeviceDS2438::convertAll() {
300 for(int k=0;k<owi->maxrepeat;k++) {
301 std::vector<uint8_t> sp;
303 Communicate(&sp, 1, 0);
304 if (owi->log->last()>=OWLOG_ERROR) continue;
306 if (setConfigByte(0x08)<=0) continue;
307 for(int k=0;k<owi->maxrepeat;k++) {
310 Communicate(&sp, 1, 0);
312 if (owi->log->last()>=OWLOG_ERROR) continue;
316 readScratchpad(&sp,0,1);
317 int temp=ow_fconvert(sp[1],sp[2]);
318 int VDD=ow_fconvert(sp[3],sp[4]);
319 if (setConfigByte(0x00)<=0) continue;
321 for(int k=0;k<owi->maxrepeat;k++) {
324 Communicate(&sp, 1, 0);
326 if (owi->log->last()>=OWLOG_ERROR) continue;
332 readScratchpad(&sp,0,1);
333 if (owi->log->last()>=OWLOG_ERROR) continue;
334 int I=ow_fconvert(sp[5],sp[6]);
335 int VAD=ow_fconvert(sp[3],sp[4]);
340 for(int i=0;i<4;i++) values[i]=config->calculateValue(i, raw);
343 if (owi->log->last()>=OWLOG_ERROR) return 0;
348 void owDeviceDS2450::setDefaultConfig() {
349 config->setConfig({6,9, 6,9, 6,9, 6,9, 0,5,5,5,5,0,0,0,0,0,0,0,0,0,0,0});
350 configstate=OWDCS_DEFAULT;
353 int owDeviceDS2450::convertAll() {
355 if (owi->log->last()>=OWLOG_WARNING) {
358 std::vector<uint8_t> sp;
360 if (owi->log->last()>=OWLOG_WARNING) {
364 for(int i=0;i<4;i++) {
365 raw[i]=(sp[2 * i] | sp[2 * i + 1] << 8);
367 for(int i=0;i<4;i++) {
368 values[i]=config->calculateValue(i, raw);
374 void owDeviceDS2450::readMemory(uint8_t addr,std::vector<uint8_t> *sp) {
375 std::vector<uint8_t> cl;
379 Communicate(&cl,3,10-addr);
380 if (owi->log->last()>=OWLOG_WARNING) {
383 if (!owi->testCRC16(cl)) {
384 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Memory of DS2450");
388 sp->insert(sp->begin(),cl.begin()+3,cl.end()-2);
391 void owDeviceDS2450::writeMemory(uint8_t addr,std::vector<uint8_t> *sp) {
392 std::vector<uint8_t> cl;
397 if (owi->log->last()>=OWLOG_WARNING) {
400 for(uint8_t b: (*sp)) {
402 owi->Communicate(&cl,cl.size(),2);
403 if (owi->log->last()>=OWLOG_WARNING) {
406 if (!owi->testCRC16(cl)) {
407 owi->log->set(OWLOG_ERROR,"CRC ERROR Writing Memory of DS2450");
411 owi->Communicate(&cl, 0, 1);
412 if (owi->log->last()>=OWLOG_WARNING) {
416 owi->log->set(OWLOG_ERROR,"ERROR Writing Memory of DS2450");
423 void owDeviceDS2450::convert(uint8_t mask, uint8_t preset) {
424 std::vector<uint8_t> cl;
427 cl.push_back(preset);
428 Communicate(&cl, 3, 2);
429 if (!owi->testCRC16(cl)) {
430 owi->log->set(OWLOG_ERROR,"CRC ERROR Convert Command of DS2450");
446 void owDeviceDS2423::setDefaultConfig() {
447 config->setConfig({9,13, 9,13, 9,13, 9,13, 0,19,19,19,19,0,0,0,0,0,0,0,0,0,0,0});
448 configstate=OWDCS_DEFAULT;
451 int owDeviceDS2423::convertAll() {
452 for(uint8_t i=12;i<16;i++) {
453 raw[i-12]=readCounter(i);
455 for(int i=0;i<4;i++) {
456 values[i]=config->calculateValue(i, raw);
460 uint32_t owDeviceDS2423::readCounter(uint8_t page) {
461 std::vector<uint8_t> cl;
463 uint16_t addr=(page<<5)+31;
464 cl.push_back(addr&0xFF);
465 cl.push_back(addr>>8);
466 Communicate(&cl,3,11);
467 //for (uint8_t v :cl) printf("%02X ",v);
469 if (!owi->testCRC16(cl)) {
470 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Counter of DS2423");
475 for(size_t i=cl.size()-8;i>cl.size()-11;i--) {