2 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
\r
3 // All rights reserved.
\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
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
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
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
35 #include "owInterface.h"
\r
41 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
42 // All rights reserved.
44 // Redistribution and use in source and binary forms, with or without
45 // modification, are permitted provided that the following conditions are
48 // * Redistributions of source code must retain the above copyright
49 // notice, this list of conditions and the following disclaimer.
50 // * Redistributions in binary form must reproduce the above copyright
51 // notice, this list of conditions and the following disclaimer in the
52 // documentation and/or other materials provided with the
54 // * All advertising materials mentioning features or use of this
55 // software must display the following acknowledgement: This product
56 // includes software developed by tm3d.de and its contributors.
57 // * Neither the name of tm3d.de nor the names of its contributors may
58 // be used to endorse or promote products derived from this software
59 // without specific prior written permission.
61 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
62 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
63 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
64 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
65 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
66 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
67 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
68 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
69 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
70 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
71 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 #include "owInterface.h"
80 void usleep(int waitTime) {
\r
81 __int64 time1 = 0, time2 = 0, freq = 0;
\r
83 QueryPerformanceCounter((LARGE_INTEGER *)&time1);
\r
84 QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
\r
87 QueryPerformanceCounter((LARGE_INTEGER *)&time2);
\r
88 } while ((time2 - time1) < waitTime);
\r
92 owDevice::owDevice(owInterface *owi_,snum_t num) {
\r
95 config=new owDeviceConfig;
\r
96 configstate=OWDCS_NONE;
\r
97 raw.insert(raw.begin(),4,0);
\r
98 values.insert(values.begin(),4,0);
\r
102 void owDevice::setDefaultConfig() {
\r
103 config->setInvalid();
\r
106 std::vector<std::string> owDevice::getFamilyInfo() {
\r
107 return owDeviceConfig::getFamilyInfo(snum.byte[0]);
\r
109 int owDevice::readConfig() {
\r
110 std::vector<uint8_t> cl;
\r
111 int r=owi->maxrepeat+1;
\r
112 setDefaultConfig();
\r
117 cl.push_back(0x85);
\r
118 Communicate(&cl, 1, 26);
\r
119 //for (uint8_t v:cl) printf("%02X ",v);
\r
120 if ((cl[1]==0xFF)||(cl[2]==0xFF)) {
\r
121 owi->log->set(OWLOG_INFO,"Get Config not work, maybe not a emulation from www.tm3d.de");
\r
126 for(int i=19;i<27;i++) if (cl[i]!=0xFF) {oldInfo=0;break;}
\r
128 owi->log->set(OWLOG_INFO,"Old 16 Byte Config");
\r
129 if (cl[18]==0xFF) {
\r
130 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+17))) {
\r
131 owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
\r
134 if (!owi->testCRC16(cl)) {
\r
135 owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
\r
138 if (owi->log->last()<OWLOG_WARNING) {
\r
139 cl[9]=2; //Attiny84A
\r
140 for(int i=0;i<7;i++) {cl[18+i]=cl[10+i];cl[10+i]=0;} //Move OWID2 Set Sensor to 0
\r
145 if (cl[26]==0xFF) {
\r
146 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25))) {
\r
147 owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
\r
150 if (!owi->testCRC16(cl)) {
\r
151 owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
\r
157 while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
\r
159 owi->log->set(OWLOG_ERROR,"To much Errors while read config");
\r
162 config->setConfig(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25));
\r
163 if (config->valid) {
\r
164 if (conf16) configstate=OWDCS_16; else configstate=OWDCS_24;
\r
165 } else configstate=OWDCS_NONE;
\r
170 int owDevice::Communicate(std::vector<uint8_t> *data, int scount, int rcount) {
\r
172 owi->MatchRom(snum);
\r
173 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
\r
174 owi->Communicate(data, scount, rcount);
\r
175 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
\r
180 int owDevice::CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount) {
\r
183 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
\r
184 owi->Communicate(data, scount, rcount);
\r
185 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
\r
191 void owDevice::changeID(snum_t nid) {
\r
193 if (nid.byte[0] != snum.byte[0]) {
\r
194 owi->log->set(OWLOG_ERROR,"Family ID should not be changed, no correct ID");
\r
197 std::vector<uint8_t> id;
\r
198 for(i=0;i<7;i++) id.push_back(nid.byte[i]);
\r
199 id.push_back(owi->calcCRC8(id));
\r
200 std::vector<uint8_t> cl;
\r
201 int r=owi->maxrepeat+1;
\r
205 cl.push_back(0x75);
\r
206 cl.insert(cl.begin()+1,id.begin(),id.end());
\r
207 Communicate(&cl, 9,0);
\r
208 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
\r
210 cl.push_back(0xA7);
\r
211 Communicate(&cl, 1, 8);
\r
212 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
\r
213 for (i = 0; i < 8; i++) {
\r
214 if (cl[i + 1] != id[i]) {
\r
215 owi->log->set(OWLOG_WARNING,"changeID Comunication ERROR");
\r
221 while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
\r
223 owi->log->set(OWLOG_ERROR,"Can not change ID");
\r
227 cl.push_back(0x79);
\r
228 cl.push_back(snum.byte[1]);
\r
229 cl.push_back(snum.byte[5]);
\r
230 cl.push_back(snum.byte[6]);
\r
231 Communicate(&cl, 4, 0);
\r
235 void owDevice::runFlasher() {
\r
236 std::vector<uint8_t> cl;
\r
237 cl.push_back(0x88);
\r
238 this->Communicate(&cl, 1, 0);
\r
239 this->Communicate(&cl, 1, 0);
\r
240 this->Communicate(&cl, 1, 0);
\r
251 void owDeviceDS18B20::setDefaultConfig() {
\r
252 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});
\r
253 configstate=OWDCS_DEFAULT;
\r
256 int owDeviceDS18B20::readScratchpad(std::vector<uint8_t> *sp) {
\r
257 std::vector<uint8_t> cl;
\r
258 int r=owi->maxrepeat+1;
\r
261 cl.push_back(0xBE);
\r
262 Communicate(&cl, 1, 9);
\r
263 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
265 sp->insert(sp->begin(),cl.begin()+1,cl.end());
\r
266 if (owi->calcCRC8(*sp)==0) return 1;
\r
267 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS18B20 Scrachpad");
\r
269 } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
\r
274 int owDeviceDS18B20::convertAll() {
\r
275 std::vector<uint8_t> sp;
\r
276 sp.push_back(0x44);
\r
277 Communicate(&sp, 1, 0); ///########################################################
\r
280 if (readScratchpad(&sp)) {
\r
282 tsht = sp[0] | ((int)sp[1] << 8);
\r
284 tsht |= 0xFFFFF0000;
\r
286 values[0]=config->calculateValue(0, raw);
\r
293 void owDeviceDS2438::setDefaultConfig() {
\r
294 config->setConfig({1,6, 6,8, 4,7, 6,17, 0,2,3,12,4,0,0,0,0,0,0,0,0,0,0,0});
\r
295 configstate=OWDCS_DEFAULT;
\r
299 int owDeviceDS2438::readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall) {
\r
300 std::vector<uint8_t> cl;
\r
301 int r=owi->maxrepeat+1;
\r
305 cl.push_back(0xB8); //recall
\r
306 cl.push_back(page);
\r
307 Communicate(&cl, 2, 0);
\r
308 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
311 cl.push_back(0xBE);
\r
312 cl.push_back(page);
\r
313 Communicate(&cl, 2, 9);
\r
314 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
316 sp->insert(sp->begin(),cl.begin()+2,cl.end());
\r
317 if (owi->calcCRC8(*sp)==0) return 1;
\r
318 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
\r
320 } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
\r
325 int owDeviceDS2438::readMemory(int page,int start, int count,std::vector<uint8_t> *data) {
\r
326 if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
\r
327 std::vector<uint8_t> cl;
\r
329 cl.push_back(0xB8); //recall
\r
330 cl.push_back(page);
\r
331 Communicate(&cl, 2, 0);
\r
332 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
334 cl.push_back(0xBE);
\r
335 cl.push_back(page);
\r
336 Communicate(&cl, 2, 9);
\r
337 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
338 cl.erase(cl.begin());
\r
339 cl.erase(cl.begin());
\r
340 if (owi->calcCRC8(cl)!=0) {
\r
341 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
\r
344 data->insert(data->begin(),cl.begin()+start,cl.begin()+start+count);
\r
347 int owDeviceDS2438::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {
\r
348 if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
\r
349 std::vector<uint8_t> sp;
\r
350 if ((start>0)||(count<8)) {
\r
351 readMemory(page,0,8,&sp);
\r
353 std::vector<uint8_t> cl;
\r
354 cl.push_back(0x4E);
\r
355 cl.push_back(page);
\r
357 for(int i=0;i<8;i++) {
\r
358 if ((i<start)||(i>=start+count)) cl.push_back(sp[i]); else {cl.push_back((*data)[j]);j++;}
\r
360 Communicate(&cl, 10,0);
\r
361 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
362 std::vector<uint8_t> cl1;
\r
363 cl1.push_back(0xBE);
\r
364 cl1.push_back(page);
\r
365 Communicate(&cl1, 2, 9);
\r
366 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
367 cl1.erase(cl1.begin());
\r
368 cl1.erase(cl1.begin());
\r
369 if (owi->calcCRC8(cl1)!=0) {
\r
370 owi->log->set(OWLOG_WARNING,"CRC ERROR rereading DS2438 Scrachpad");
\r
372 for(int i=0;i<8;i++) {
\r
373 if (cl1[i]!=cl[i+2]) {
\r
374 owi->log->set(OWLOG_ERROR,"Reread not equal, nothing copied");
\r
379 cl.push_back(0x48);
\r
380 cl.push_back(page);
\r
381 Communicate(&cl, 2, 0);
\r
382 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
388 int owDeviceDS2438::setConfigByte(uint8_t cb) {
\r
389 std::vector<uint8_t> sp;
\r
391 for(k=0;k<owi->maxrepeat;k++) {
\r
394 sp.push_back(0x4E);
\r
395 sp.push_back(0x00);
\r
397 Communicate(&sp,3,0);
\r
398 if (owi->log->last()>=OWLOG_ERROR) return -1;
\r
400 readScratchpad(&sp,0,0);
\r
401 if (owi->log->last()>=OWLOG_ERROR) return -2;
\r
402 if (cb==sp[0]) return 1;
\r
403 owi->log->set(OWLOG_WARNING,"ERROR set config byte of DS2438");
\r
405 owi->log->set(OWLOG_WARNING,"Config of DS2438 byte not set");
\r
409 inline int16_t ow_fconvert(uint8_t b1, uint16_t b2) {
\r
411 tsht=b1 |((int)b2<<8);
\r
413 tsht |= 0xFFFFF0000;
\r
417 int owDeviceDS2438::convertAll() {
\r
418 for(int k=0;k<owi->maxrepeat;k++) {
\r
419 std::vector<uint8_t> sp;
\r
420 sp.push_back(0x44);
\r
421 Communicate(&sp, 1, 0);///########################################################
\r
422 if (owi->log->last()>=OWLOG_ERROR) continue;
\r
424 if (setConfigByte(0x08)<=0) continue;
\r
425 for(int k=0;k<owi->maxrepeat;k++) {
\r
427 sp.push_back(0xB4);
\r
428 Communicate(&sp, 1, 0);
\r
430 if (owi->log->last()>=OWLOG_ERROR) continue;
\r
434 readScratchpad(&sp,0,1);
\r
435 int temp=ow_fconvert(sp[1],sp[2]);
\r
436 int VDD=ow_fconvert(sp[3],sp[4]);
\r
437 if (setConfigByte(0x00)<=0) continue;
\r
439 for(int k=0;k<owi->maxrepeat;k++) {
\r
441 sp.push_back(0xB4);
\r
442 Communicate(&sp, 1, 0);
\r
444 if (owi->log->last()>=OWLOG_ERROR) continue;
\r
450 readScratchpad(&sp,0,1);
\r
451 if (owi->log->last()>=OWLOG_ERROR) continue;
\r
452 int I=ow_fconvert(sp[5],sp[6]);
\r
453 int VAD=ow_fconvert(sp[3],sp[4]);
\r
458 for(int i=0;i<4;i++) values[i]=config->calculateValue(i, raw);
\r
461 if (owi->log->last()>=OWLOG_ERROR) return 0;
\r
466 void owDeviceDS2450::setDefaultConfig() {
\r
467 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});
\r
468 configstate=OWDCS_DEFAULT;
\r
471 int owDeviceDS2450::convertAll() {
\r
473 if (owi->log->last()>=OWLOG_WARNING) {
\r
476 std::vector<uint8_t> sp;
\r
478 if (owi->log->last()>=OWLOG_WARNING) {
\r
482 for(int i=0;i<4;i++) {
\r
483 raw[i]=(sp[2 * i] | sp[2 * i + 1] << 8);
\r
485 for(int i=0;i<4;i++) {
\r
486 values[i]=config->calculateValue(i, raw);
\r
492 void owDeviceDS2450::readMemory(uint8_t addr,std::vector<uint8_t> *sp) {
\r
493 std::vector<uint8_t> cl;
\r
494 cl.push_back(0xAA);
\r
495 cl.push_back(addr);
\r
496 cl.push_back(0x00);
\r
497 Communicate(&cl,3,10-addr);
\r
498 if (owi->log->last()>=OWLOG_WARNING) {
\r
501 if (!owi->testCRC16(cl)) {
\r
502 for (uint8_t v :cl) printf("%02X ",v);
\r
504 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Memory of DS2450");
\r
508 sp->insert(sp->begin(),cl.begin()+3,cl.end()-2);
\r
511 void owDeviceDS2450::writeMemory(uint8_t addr,std::vector<uint8_t> *sp) {
\r
512 std::vector<uint8_t> cl;
\r
513 cl.push_back(0x55);
\r
514 cl.push_back(addr);
\r
515 cl.push_back(0x00);
\r
516 owi->MatchRom(snum);
\r
517 if (owi->log->last()>=OWLOG_WARNING) {
\r
520 for(uint8_t b: (*sp)) {
\r
522 owi->Communicate(&cl,cl.size(),2);
\r
523 if (owi->log->last()>=OWLOG_WARNING) {
\r
526 if (!owi->testCRC16(cl)) {
\r
527 owi->log->set(OWLOG_ERROR,"CRC ERROR Writing Memory of DS2450");
\r
531 owi->Communicate(&cl, 0, 1);
\r
532 if (owi->log->last()>=OWLOG_WARNING) {
\r
536 owi->log->set(OWLOG_ERROR,"ERROR Writing Memory of DS2450");
\r
543 void owDeviceDS2450::convert(uint8_t mask, uint8_t preset) {
\r
544 std::vector<uint8_t> cl;
\r
545 cl.push_back(0x3C);
\r
546 cl.push_back(mask);
\r
547 cl.push_back(preset);
\r
548 Communicate(&cl, 3, 2);
\r
549 if (!owi->testCRC16(cl)) {
\r
550 for (uint8_t v :cl) printf("%02X ",v);
\r
552 owi->log->set(OWLOG_ERROR,"CRC ERROR Convert Command of DS2450");
\r
568 void owDeviceDS2423::setDefaultConfig() {
\r
569 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});
\r
570 configstate=OWDCS_DEFAULT;
\r
573 int owDeviceDS2423::convertAll() {
\r
574 for(uint8_t i=12;i<16;i++) {
\r
575 raw[i-12]=readCounter(i);
\r
577 for(int i=0;i<4;i++) {
\r
578 values[i]=config->calculateValue(i, raw);
\r
582 uint32_t owDeviceDS2423::readCounter(uint8_t page) {
\r
583 std::vector<uint8_t> cl;
\r
584 cl.push_back(0xA5);
\r
585 uint16_t addr=(page<<5)+31;
\r
586 cl.push_back(addr&0xFF);
\r
587 cl.push_back(addr>>8);
\r
588 Communicate(&cl,3,11);
\r
589 //for (uint8_t v :cl) printf("%02X ",v);
\r
591 if (!owi->testCRC16(cl)) {
\r
592 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Counter of DS2423");
\r
597 for(size_t i=cl.size()-7;i>cl.size()-11;i--) {
\r
606 int owDeviceDS2423::readMemory(int page,int start, int count,std::vector<uint8_t> *data){
\r
607 std::vector<uint8_t> cl;
\r
608 cl.push_back(0xF0);
\r
609 uint16_t adr=page<<5;
\r
610 cl.push_back(adr&0xFF);
\r
611 cl.push_back((adr>>8)&0xFF);
\r
612 Communicate(&cl, 3, 32);
\r
615 data->insert(data->begin(),cl.begin()+3+start,cl.begin()+start+3+count);
\r
619 int owDeviceDS2423::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {
\r
620 std::vector<uint8_t> cl;
\r
621 cl.push_back(0x0F);
\r
622 uint16_t adr=(page<<5)+start;
\r
623 cl.push_back(adr&0xFF);
\r
624 cl.push_back(adr>>8);
\r
625 if (count > (int)(*data).size()) count = (*data).size();
\r
626 for(int i=0;i<count;i++) {
\r
627 cl.push_back((*data)[i]);
\r
629 Communicate(&cl, 3+count, 0);
\r
630 for (uint8_t v :cl) printf("%02X ",v);printf("\n");
\r
632 cl.push_back(0xAA);
\r
633 Communicate(&cl, 1, 36);
\r
634 //for (uint8_t v :cl) printf("%02X ",v);;printf("\n");
\r
636 Communicate(&cl, 4, 1);
\r
639 for (uint8_t v :cl) printf("%02X ",v);;printf("\n");
\r
640 owi->log->set(OWLOG_ERROR,"DS2423 Copy from Scratchpad to Memmory Error");
\r
654 owDevice::owDevice(owInterface *owi_,snum_t num) {
657 config=new owDeviceConfig;
658 configstate=OWDCS_NONE;
659 raw.insert(raw.begin(),4,0);
660 values.insert(values.begin(),4,0);
664 void owDevice::setDefaultConfig() {
665 config->setInvalid();
668 std::vector<std::string> owDevice::getFamilyInfo() {
669 return owDeviceConfig::getFamilyInfo(snum.byte[0]);
671 int owDevice::readConfig() {
672 std::vector<uint8_t> cl;
673 int r=owi->maxrepeat+1;
680 Communicate(&cl, 1, 26);
681 //for (uint8_t v:cl) printf("%02X ",v);
682 if ((cl[1]==0xFF)||(cl[2]==0xFF)) {
683 owi->log->set(OWLOG_INFO,"Get Config not work, maybe not a emulation from www.tm3d.de");
688 for(int i=19;i<27;i++) if (cl[i]!=0xFF) {oldInfo=0;break;}
690 owi->log->set(OWLOG_INFO,"Old 16 Byte Config");
692 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+17))) {
693 owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
696 if (!owi->testCRC16(cl)) {
697 owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
700 if (owi->log->last()<OWLOG_WARNING) {
702 for(int i=0;i<7;i++) {cl[18+i]=cl[10+i];cl[10+i]=0;} //Move OWID2 Set Sensor to 0
708 if (!owi->calcCRC8(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25))) {
709 owi->log->set(OWLOG_WARNING,"CRC8 ERROR Reading Config");
712 if (!owi->testCRC16(cl)) {
713 owi->log->set(OWLOG_WARNING,"CRC16 ERROR Reading Config ");
719 while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
721 owi->log->set(OWLOG_ERROR,"To much Errors while read config");
724 config->setConfig(std::vector<uint8_t>(cl.begin()+1,cl.begin()+25));
726 if (conf16) configstate=OWDCS_16; else configstate=OWDCS_24;
727 } else configstate=OWDCS_NONE;
732 int owDevice::Communicate(std::vector<uint8_t> *data, int scount, int rcount) {
735 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
736 owi->Communicate(data, scount, rcount);
737 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
742 int owDevice::CommunicateShort(std::vector<uint8_t> *data, int scount, int rcount) {
745 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
746 owi->Communicate(data, scount, rcount);
747 if (owi->log->last()==OWLOG_ERROR) {owi->Free();return 0;}
753 void owDevice::changeID(snum_t nid) {
755 if (nid.byte[0] != snum.byte[0]) {
756 owi->log->set(OWLOG_ERROR,"Family ID should not be changed, no correct ID");
759 std::vector<uint8_t> id;
760 for(i=0;i<7;i++) id.push_back(nid.byte[i]);
761 id.push_back(owi->calcCRC8(id));
762 std::vector<uint8_t> cl;
763 int r=owi->maxrepeat+1;
768 cl.insert(cl.begin()+1,id.begin(),id.end());
769 Communicate(&cl, 9,0);
770 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
773 Communicate(&cl, 1, 8);
774 if (owi->log->last()==OWLOG_ERROR) {r--;continue;}
775 for (i = 0; i < 8; i++) {
776 if (cl[i + 1] != id[i]) {
777 owi->log->set(OWLOG_WARNING,"changeID Comunication ERROR");
783 while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
785 owi->log->set(OWLOG_ERROR,"Can not change ID");
790 cl.push_back(snum.byte[1]);
791 cl.push_back(snum.byte[5]);
792 cl.push_back(snum.byte[6]);
793 Communicate(&cl, 4, 0);
797 void owDevice::runFlasher() {
798 std::vector<uint8_t> cl;
800 this->Communicate(&cl, 1, 0);
801 this->Communicate(&cl, 1, 0);
802 this->Communicate(&cl, 1, 0);
813 void owDeviceDS18B20::setDefaultConfig() {
814 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});
815 configstate=OWDCS_DEFAULT;
818 int owDeviceDS18B20::readScratchpad(std::vector<uint8_t> *sp) {
819 std::vector<uint8_t> cl;
820 int r=owi->maxrepeat+1;
824 Communicate(&cl, 1, 9);
825 if (owi->log->last()>=OWLOG_ERROR) return -1;
827 sp->insert(sp->begin(),cl.begin()+1,cl.end());
828 if (owi->calcCRC8(*sp)==0) return 1;
829 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS18B20 Scrachpad");
831 } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
836 int owDeviceDS18B20::convertAll() {
837 std::vector<uint8_t> sp;
838 //sp.push_back(0x44);
839 //communicate(&sp, 1, 0); ///########################################################
842 if (readScratchpad(&sp)) {
844 tsht = sp[0] | ((int)sp[1] << 8);
848 values[0]=config->calculateValue(0, raw);
855 void owDeviceDS2438::setDefaultConfig() {
856 config->setConfig({1,6, 6,8, 4,7, 6,17, 0,2,3,12,4,0,0,0,0,0,0,0,0,0,0,0});
857 configstate=OWDCS_DEFAULT;
861 int owDeviceDS2438::readScratchpad(std::vector<uint8_t> *sp, uint8_t page, int recall) {
862 std::vector<uint8_t> cl;
863 int r=owi->maxrepeat+1;
867 cl.push_back(0xB8); //recall
869 Communicate(&cl, 2, 0);
870 if (owi->log->last()>=OWLOG_ERROR) return -1;
875 Communicate(&cl, 2, 9);
876 if (owi->log->last()>=OWLOG_ERROR) return -1;
878 sp->insert(sp->begin(),cl.begin()+2,cl.end());
879 if (owi->calcCRC8(*sp)==0) return 1;
880 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
882 } while ((owi->log->last()>=OWLOG_WARNING)&&(r>0));
887 int owDeviceDS2438::readMemory(int page,int start, int count,std::vector<uint8_t> *data) {
888 if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
889 std::vector<uint8_t> cl;
891 cl.push_back(0xB8); //recall
893 Communicate(&cl, 2, 0);
894 if (owi->log->last()>=OWLOG_ERROR) return -1;
898 Communicate(&cl, 2, 9);
899 if (owi->log->last()>=OWLOG_ERROR) return -1;
900 cl.erase(cl.begin());
901 cl.erase(cl.begin());
902 if (owi->calcCRC8(cl)!=0) {
903 owi->log->set(OWLOG_WARNING,"CRC ERROR reading DS2438 Scrachpad");
906 data->insert(data->begin(),cl.begin()+start,cl.begin()+start+count);
909 int owDeviceDS2438::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {
910 if ((page>7)||(page<0)||(start>7)||(start<0)||(start+count>8)) return 0;
911 std::vector<uint8_t> sp;
912 if ((start>0)||(count<8)) {
913 readMemory(page,0,8,&sp);
915 std::vector<uint8_t> cl;
919 for(int i=0;i<8;i++) {
920 if ((i<start)||(i>=start+count)) cl.push_back(sp[i]); else {cl.push_back((*data)[j]);j++;}
922 Communicate(&cl, 10,0);
923 if (owi->log->last()>=OWLOG_ERROR) return -1;
924 std::vector<uint8_t> cl1;
927 Communicate(&cl1, 2, 9);
928 if (owi->log->last()>=OWLOG_ERROR) return -1;
929 cl1.erase(cl1.begin());
930 cl1.erase(cl1.begin());
931 if (owi->calcCRC8(cl1)!=0) {
932 owi->log->set(OWLOG_WARNING,"CRC ERROR rereading DS2438 Scrachpad");
934 for(int i=0;i<8;i++) {
935 if (cl1[i]!=cl[i+2]) {
936 owi->log->set(OWLOG_ERROR,"Reread not equal, nothing copied");
943 Communicate(&cl, 2, 0);
944 if (owi->log->last()>=OWLOG_ERROR) return -1;
950 int owDeviceDS2438::setConfigByte(uint8_t cb) {
951 std::vector<uint8_t> sp;
953 for(k=0;k<owi->maxrepeat;k++) {
959 Communicate(&sp,3,0);
960 if (owi->log->last()>=OWLOG_ERROR) return -1;
962 readScratchpad(&sp,0,0);
963 if (owi->log->last()>=OWLOG_ERROR) return -2;
964 if (cb==sp[0]) return 1;
965 owi->log->set(OWLOG_WARNING,"ERROR set config byte of DS2438");
967 owi->log->set(OWLOG_WARNING,"Config of DS2438 byte not set");
971 inline int16_t ow_fconvert(uint8_t b1, uint16_t b2) {
973 tsht=b1 |((int)b2<<8);
979 int owDeviceDS2438::convertAll() {
980 for(int k=0;k<owi->maxrepeat;k++) {
981 std::vector<uint8_t> sp;
982 //sp.push_back(0x44);
983 //Communicate(&sp, 1, 0);///########################################################
984 if (owi->log->last()>=OWLOG_ERROR) continue;
986 if (setConfigByte(0x08)<=0) continue;
987 for(int k=0;k<owi->maxrepeat;k++) {
990 Communicate(&sp, 1, 0);
992 if (owi->log->last()>=OWLOG_ERROR) continue;
996 readScratchpad(&sp,0,1);
997 int temp=ow_fconvert(sp[1],sp[2]);
998 int VDD=ow_fconvert(sp[3],sp[4]);
999 if (setConfigByte(0x00)<=0) continue;
1001 for(int k=0;k<owi->maxrepeat;k++) {
1004 Communicate(&sp, 1, 0);
1006 if (owi->log->last()>=OWLOG_ERROR) continue;
1012 readScratchpad(&sp,0,1);
1013 if (owi->log->last()>=OWLOG_ERROR) continue;
1014 int I=ow_fconvert(sp[5],sp[6]);
1015 int VAD=ow_fconvert(sp[3],sp[4]);
1020 for(int i=0;i<4;i++) values[i]=config->calculateValue(i, raw);
1023 if (owi->log->last()>=OWLOG_ERROR) return 0;
1028 void owDeviceDS2450::setDefaultConfig() {
1029 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});
1030 configstate=OWDCS_DEFAULT;
1033 int owDeviceDS2450::convertAll() {
1035 if (owi->log->last()>=OWLOG_WARNING) {
1038 std::vector<uint8_t> sp;
1040 if (owi->log->last()>=OWLOG_WARNING) {
1044 for(int i=0;i<4;i++) {
1045 raw[i]=(sp[2 * i] | sp[2 * i + 1] << 8);
1047 for(int i=0;i<4;i++) {
1048 values[i]=config->calculateValue(i, raw);
1054 void owDeviceDS2450::readMemory(uint8_t addr,std::vector<uint8_t> *sp) {
1055 std::vector<uint8_t> cl;
1059 Communicate(&cl,3,10-addr);
1060 if (owi->log->last()>=OWLOG_WARNING) {
1063 if (!owi->testCRC16(cl)) {
1064 for (uint8_t v :cl) printf("%02X ",v);
1066 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Memory of DS2450");
1070 sp->insert(sp->begin(),cl.begin()+3,cl.end()-2);
1073 void owDeviceDS2450::writeMemory(uint8_t addr,std::vector<uint8_t> *sp) {
1074 std::vector<uint8_t> cl;
1078 owi->MatchRom(snum);
1079 if (owi->log->last()>=OWLOG_WARNING) {
1082 for(uint8_t b: (*sp)) {
1084 owi->Communicate(&cl,cl.size(),2);
1085 if (owi->log->last()>=OWLOG_WARNING) {
1088 if (!owi->testCRC16(cl)) {
1089 owi->log->set(OWLOG_ERROR,"CRC ERROR Writing Memory of DS2450");
1093 owi->Communicate(&cl, 0, 1);
1094 if (owi->log->last()>=OWLOG_WARNING) {
1098 owi->log->set(OWLOG_ERROR,"ERROR Writing Memory of DS2450");
1105 void owDeviceDS2450::convert(uint8_t mask, uint8_t preset) {
1106 std::vector<uint8_t> cl;
1109 cl.push_back(preset);
1110 Communicate(&cl, 3, 2);
1111 if (!owi->testCRC16(cl)) {
1112 for (uint8_t v :cl) printf("%02X ",v);
1114 owi->log->set(OWLOG_ERROR,"CRC ERROR Convert Command of DS2450");
1130 void owDeviceDS2423::setDefaultConfig() {
1131 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});
1132 configstate=OWDCS_DEFAULT;
1135 int owDeviceDS2423::convertAll() {
1136 for(uint8_t i=12;i<16;i++) {
1137 raw[i-12]=readCounter(i);
1139 for(int i=0;i<4;i++) {
1140 values[i]=config->calculateValue(i, raw);
1144 uint32_t owDeviceDS2423::readCounter(uint8_t page) {
1145 std::vector<uint8_t> cl;
1147 uint16_t addr=(page<<5)+31;
1148 cl.push_back(addr&0xFF);
1149 cl.push_back(addr>>8);
1150 Communicate(&cl,3,11);
1151 //for (uint8_t v :cl) printf("%02X ",v);
1153 if (!owi->testCRC16(cl)) {
1154 owi->log->set(OWLOG_ERROR,"CRC ERROR Reading Counter of DS2423");
1159 for(size_t i=cl.size()-8;i>cl.size()-11;i--) {
1169 int owDeviceDS2423::readMemory(int page,int start, int count,std::vector<uint8_t> *data){
1170 std::vector<uint8_t> cl;
1172 uint16_t adr=page<<5;
1173 cl.push_back(adr&0xFF);
1174 cl.push_back((adr>>8)&0xFF);
1175 Communicate(&cl, 3, 32);
1178 data->insert(data->begin(),cl.begin()+3+start,cl.begin()+start+3+count);
1182 int owDeviceDS2423::writeMemory(int page,int start, int count,std::vector<uint8_t> *data) {
1183 std::vector<uint8_t> cl;
1185 uint16_t adr=(page<<5)+start;
1186 cl.push_back(adr&0xFF);
1187 cl.push_back(adr>>8);
1188 for(int i=0;i<count;i++) {
1189 cl.push_back((*data)[i]);
1191 Communicate(&cl, 3+count, 0);
1192 for (uint8_t v :cl) printf("%02X ",v);printf("\n");
1195 Communicate(&cl, 1, 36);
1196 //for (uint8_t v :cl) printf("%02X ",v);;printf("\n");
1198 Communicate(&cl, 4, 1);
1201 for (uint8_t v :cl) printf("%02X ",v);;printf("\n");
1202 owi->log->set(OWLOG_ERROR,"DS2423 Copy from Scratchpad to Memmory Error");
1213 >>>>>>> origin/master