Skip to content

Fix cpu temps array

I've realized that the cpu_temps array is being recreated everytime we call read_data() method, which eliminates the purpose of having this array in the first place 😄

The cpu_temps array should be created once at the beginning, and then it should be keeping a bunch of values for smoothing out with some averaging to decrease jitter.

From the original Pimoroni Enviro+ examples:

image

The flow is like this:

  1. Get the cpu temp and fill the array with that value for a specified size, in this case the size is 5. Let's say the cpu temp is 50, the array should look like this: [50, 50, 50, 50, 50]
  2. Get the new cpu temp and store it in variable, let's say it's 55 now
  3. Get all the elements of cpu_temp except the first one, and add the last one to the end. The array should look like this now: [50, 50, 50, 50, 55]
  4. Now calculate the average cpu temp and use it in the compensation calculations.
  5. Next time, repeat the same process. Let's say the cpu temp is 58 now, the array should look like this: [50, 50, 50, 55, 58]
  6. Next time: [50, 50, 55, 58, 59]
  7. Next time: [50, 55, 58, 59, 57]
  8. Next time: [55, 58, 59, 57, 62]

So we will always have the last 5 cpu temps to calculate average of.

But with the current code, the cpu_temps array is being recreated everytime we call read_data() method, which is not ideal. In the exact situation as above, the array will look like this

  1. [50, 50, 50, 50, 55]
  2. [55, 55, 55, 55, 58]
  3. [58, 58, 58, 58, 59]
  4. [59, 59, 59, 59, 57]
  5. [57, 57, 57, 57, 62]

The values are not sliding, we always end up with just two different values in the array. First 4 values are from first cpu temp measure, the last value is from the second cpu temp measure.

Relocated the cpu_temps array to the top and used this global variable in the read_data() method, so we'll have the last 5 cpu temp sliding in the array.

Somewhat related tip: We can increase the array size from 5 to 20 for example, to have more smooth average cpu temp calculation 😄

Merge request reports

Loading