{"action":"opened","issue":{"url":"https://api.github.com/repos/piettetech/PietteTech_DHT/issues/11","repository_url":"https://api.github.com/repos/piettetech/PietteTech_DHT","labels_url":"https://api.github.com/repos/piettetech/PietteTech_DHT/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/piettetech/PietteTech_DHT/issues/11/comments","events_url":"https://api.github.com/repos/piettetech/PietteTech_DHT/issues/11/events","html_url":"https://github.com/piettetech/PietteTech_DHT/issues/11","id":194831363,"number":11,"title":"Problem with acquireAndWait(timeout)","user":{"login":"ScruffR","id":6604878,"avatar_url":"https://avatars.githubusercontent.com/u/6604878?v=3","gravatar_id":"","url":"https://api.github.com/users/ScruffR","html_url":"https://github.com/ScruffR","followers_url":"https://api.github.com/users/ScruffR/followers","following_url":"https://api.github.com/users/ScruffR/following{/other_user}","gists_url":"https://api.github.com/users/ScruffR/gists{/gist_id}","starred_url":"https://api.github.com/users/ScruffR/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ScruffR/subscriptions","organizations_url":"https://api.github.com/users/ScruffR/orgs","repos_url":"https://api.github.com/users/ScruffR/repos","events_url":"https://api.github.com/users/ScruffR/events{/privacy}","received_events_url":"https://api.github.com/users/ScruffR/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-12-11T13:16:52Z","updated_at":"2016-12-11T13:16:52Z","closed_at":null,"body":"The way the waiting is done in that function presents two problems.\r\nIf the wayiting time is rather long and the sensor slow to respond, the missing Particle.process() might cause the cloud connection to drop.\r\nBut the main issue is this part\r\n```cpp\r\nint PietteTech_DHT::acquireAndWait(uint32_t timeout) {\r\n    ...\r\n    uint32_t start = millis();\r\n    ...\r\n    while(acquiring() && (timeout == 0 || (millis() > start && (millis()-start) < timeout)));\r\n    ...\r\n}\r\n```\r\nSince those two lines will be execeuted within the same millisecond in almost every case, the `millis() > start` will almost always be `false` because they will be _equal_ causing to drop out of the wait immediately before the acutal measurement will be finished.\r\n\r\nBut in fact that condition is superfluous all together - as are other wrap round \"precautions\" - as unsigned integer calculation will be tolerant to such conditions.\r\n\r\nSo a (for me) working implementation of that function would look like this\r\n```cpp\r\nint PietteTech_DHT::acquireAndWait(uint32_t timeout) {\r\n    acquire();\r\n    uint32_t start = millis();\r\n    uint32_t wrapper;\r\n    while(acquiring() && (timeout == 0 || ((millis()-start) < timeout))) Particle.process();\r\n    if (timeout)\r\n    {\r\n// ---- most likely not needed, but haven't thought into it too deeply ;-) ----\r\n        if (millis() < start) // millis counter wrapped\r\n        {\r\n            wrapper = (~start)+1;   // Compute elapsed seconds between \"start\" and counter-wrap to 0\r\n            timeout -= wrapper;     // Subtract elapsed seconds to 0 from timeout\r\n        }\r\n// ----------------------------------------------------------------------------\r\n        _status = DHTLIB_ERROR_RESPONSE_TIMEOUT\r\n    }\r\n    return getStatus();\r\n}\r\n```\r\n"}}
{"actor":{"display_login":"ScruffR"}}