0

display sensor variable on OLED...
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

display sensor variable on OLED...

by ChipHildreth on Tue Jan 28, 2020 4:10 pm

Is it possible to pass a text variable to a circuitpython display object rather than a quoted text string?
I want to display sensor output as formatted text on a Feather 1306 OLED.
I can only find demo code or references to passing "Hello World" to a display object. I want to pass variable data such as sonar.distance.
It seems like this should be obvious but I can't find anything.
Any help?

ChipHildreth
 
Posts: 6
Joined: Sat Jan 25, 2020 3:39 pm

Re: display sensor variable on OLED...

by V2man on Tue Jan 28, 2020 4:59 pm

Maybe this will help. Look at the CircuitPython example for TFT Gizmo I posted here https://forums.adafruit.com/viewtopic.php?f=58&t=160567

V2man
 
Posts: 703
Joined: Mon Dec 03, 2018 12:38 am

Re: display sensor variable on OLED...

by V2man on Tue Jan 28, 2020 6:38 pm

Code: Select all | TOGGLE FULL SIZE
cpu_temp = microcontroller.cpu.temperature * (9/5) + 32
text_area1.text = "Temp:  %0.1f *F" % cpu_temp

V2man
 
Posts: 703
Joined: Mon Dec 03, 2018 12:38 am

Re: display sensor variable on OLED...

by adafruit_support_carter on Tue Jan 28, 2020 6:51 pm

Yep. Above is correct. The general answer to this is "string formatting". Ultimately, you still pass a string. But with string formatting, you can insert variables, format them to your liking, etc.

There's also another syntax:
Code: Select all | TOGGLE FULL SIZE
message = "Temperature = {}".format(sensor.temperature)

and you can add formatting inside the {}.

A good way to learn this is to just play around with it in the REPL (Python prompt).
Code: Select all | TOGGLE FULL SIZE
>>> "Value = {}".format(3.1415)
'Value = 3.1415'
>>> "Value = {:6.3}".format(3.1415)
'Value =   3.14'
>>> "Value = {:05.2}".format(3.1415)
'Value = 003.1'
>>> "Value = {:05.3}".format(3.1415)
'Value = 03.14'
>>>


More info:
https://docs.python.org/3/library/strin ... ing-syntax

adafruit_support_carter
 
Posts: 14475
Joined: Tue Nov 29, 2016 2:45 pm

Re: display sensor variable on OLED...

by ChipHildreth on Tue Jan 28, 2020 9:11 pm

Thank you for the quick response. My brain is waxed right now. I'll put this to work in the AM.

Thanks again.

ChipHildreth
 
Posts: 6
Joined: Sat Jan 25, 2020 3:39 pm

Please be positive and constructive with your questions and comments.