VL53L1X (ToF) Sensor Problem to understand some script stuff


#1

Hi,

I modified the example python script which I find after installation of the breakout garden SW. I don’t understand the following config line item
pos = 80 - (rb_20.getsize(str(cm))[0] / 2).

especially rb_20.getsize(str(cm))[0] size of a font + cm variable in list ? divided by 2?

Could someone explain this nested stuff :-)
Many thx in advance

Peter


#2

So, there are a few things happening here. Look at the line below it:

draw.text((pos, 30), “{}”.format(cm), fill=“white”, font=rb_20)

This draws text on the image to be displayed on the OLED, so pos is a coordinate for drawing text, and controls how far over on the X axis of the screen the text appears. The line you’re talking about:
pos = 80 - (rb_20.getsize(str(cm))[0] / 2)
Is about calculating how far over on the X axis the text should be. I don’t have the ToF sensor but from the demo video the text which shows the distance on the OLED is always centred between the arrows, so this line with pos is about figuring out where the text has to be draw to be centred.

  • (str(cm)) takes the ToF distance measurement and converts it to a string. This is necessary because the next step only accepts strings, it can’t handle integers.

rb_20.getsize(str(cm)) is a bit complex.

  • If you look further up the script (line 40) you can see that rb_20 is an instance of an ImageFont object, with the parameters rb_path and 20. rb_path is the location of the Roboto-Black.ttf font, and 20 is the font size. So, this is an object of text, in Roboto Black font, size 20. It’s really just a way of specifying font style and size for later use.
  • rb_20.getsize(str(cm)) uses the getsize function to get the size in pixels of the text (str(cm)) when using the rb_20 font.

Now, the above gives you a list of two values, an X size and a Y size. The [0] part of the line returns the first value in this list, which is the horizontal size of the text in pixels.

The 80-([X size]/2) bit I’m guessing is about figuring out where to position the text. By dividing the horizontal distance by 2 you get the middle of the length of text, and I’m guessing the 80 - middle_of_X bit is about where on the OLED they want the text positioned. This way the middle of the text is always positioned at the same place on the screen.

I think that’s what it does.


#3

Hi Shoe,

got it and many thx for your detailed explanation!

Peter