adafruit / Adafruit-MAX31855-library

Library for the Adafruit Thermocouple breakout with MAX31855K
http://www.adafruit.com/products/269
Other
100 stars 94 forks source link

Optimisation #17

Closed rswift closed 4 years ago

rswift commented 8 years ago

Created a new readTemperatures() function which makes a single call to spiread32() then processes the response to extract both hot and cold junction temperatures into a struct, which is then returned. This has had a dramatic impact on performance, using the spiread32() for software SPI was taking approximately 0.2 seconds to call both readInternal() then readCelsius() in order to linearise the temperature.

I've modified the header and code to include the new function, and associated definition. I've also split the readInternal() and readCelsius() functions to separate the processing logic to ensure backwards compatibility. Therefore the processInternal() and processCelsius() functions continue to be the single location for processing.

Based on the pull request from @matthijskooijman I also removed the delay() calls from the software SPI half of spiread32().

The overall result of these changes is that the probe can be read, including linearisation, in less than 12,000 microseconds (0.012 seconds) almost a 17x increase in application level performance.

I have tested this on an Adafruit Feather Huzzah ESP8266 (Product ID: 2821). I do not have any other boards to test this on I'm afraid. My tests included the new function, and the existing functions. I have not observed any impact, I therefore believe these changes are backwards compatible but also provide a slightly simpler interface (although if the library included the linearisation code, I suppose readTemperature() would be a logical development to simply return the temperature itself). The complete removal of the delay() could cause problems, although at the 100 nano second timing level (0.1 microsecond) this isn't likely. I have not tested this on a hardware SPI implementation, merely removed the delay() from that half of the code as per @matthijskooijman's pull request.

I also took a moment to refresh the keywords.txt file to include the new functions, but also a couple of missing ones.

Thanks for your time.

Robert.

zerox1212 commented 6 years ago

Any plans to merge this? Looks like a decent improvement.

tablatronix commented 5 years ago

This has been driving me mad, separate reads for all 3 getters.

see https://github.com/adafruit/Adafruit-MAX31855-library/pull/24#issuecomment-515120728

caternuson commented 4 years ago

I think the big hitter on time was the delay in the software SPI loop. This should be better now with the change to BusIO SPIDevice in version 1.0.5. https://github.com/adafruit/Adafruit-MAX31855-library/releases/tag/1.0.5

SPI is generally pretty fast. The separate reads shouldn't be huge time hit.

tablatronix commented 4 years ago

I have not profiled it, but when you are also driving an lcd and your also dealing with the CS select overhead on each call, I would think it would add up