Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1584

Python • How to print current value of some variables inside a class

$
0
0
I am using thonny under opensuse linux.
I am using a pico w

I am very new to python.

For debugging, I would like to print current value of variables used inside a class.
As soon as a calculation is done in the class, I would like to print the calculated value.

First question which concern only the class MY_DEVICE :

As an example : MY_Device.py contains

Code:

import arrayimport micropythonimport utimefrom machine import Pinfrom micropython import constMAX_UNCHANGED = const(100)MIN_INTERVAL_US = const(1000000)HIGH_LEVEL = const(50)EXPECTED_PULSES = const(84)class MY_DEVICE:    _temperature: float    _humidity: float    def __init__(self, pin):        self._pin = pin        self._last_measure = utime.ticks_us()        self._temperature = -1        self._humidity = -1    def measure(self):          current_ticks = utime.ticks_us()          if utime.ticks_diff(current_ticks, self._last_measure) < MIN_INTERVAL_US and (              self._temperature > -1 or self._humidity > -1 ):              print("Less than a second since last read, which is too soon according to the datasheet")              return          ...          ...          # Do acquisition          # Do control          ...          ...        self._humidity = buffer[0] + buffer[1] / 10        self._temperature = buffer[2] + buffer[3] / 10                [color=#FF0000]how to print here  current humidity value[/color]        [color=#FF0000]how to print here  current temperature value[/color]                ....        ....        ...        /code]The program :[code]from machine import Pinfrom time import sleepfrom MY_Device import MY_DEVICETEMP: floatHUMI: floatNUM_LOOP = 0PIN_NUM = 2MYSENSOR = MY_DEVICE(Pin(PIN_NUM, Pin.IN, Pin.PULL_UP))sleep(1)while True:    NUM_LOOP += 1    print('NUM LOOP', NUM_LOOP)    print("CALLING : MY_DEVICE.measure")    MY_DEVICE.measure()    print("GET TEMP")    TEMP = MYSENSOR.temperature()    print("GET HUMI")    HUMI = MYSENSOR.humidity()    print('Temperature: %3.1f C' %TEMP)    print('Humidity: %3.1f %%' %HUMI)    print()    sleep(3)
Second question which concern the call to get data in the main program :

To access the data, the class contain :

Code:

 @property def humidity(self):         self.measure()         return self._humidity @propertydef temperature(self):         self.measure()         return self._temperature
The main program
a) First a cal is made to 'MY_DEVICE.Measure()' which initialize the device, then get the temperature and humidity values.
b) Secondly a call is made to get temperature data by calling MYSENSOR.Temperature ()
c) Thirdly, a call is made to get humidity data by calling MYSENSOR.Humidity ()
So the measure function is call thrice violating the 2 seconds wait before accessing the hardware.
Is that a correct inference ?

Any help is welcome.

Statistics: Posted by jcd95 — Fri May 09, 2025 5:05 pm



Viewing all articles
Browse latest Browse all 1584

Trending Articles