general project gudance ! if it is possible or not
-
I have a some code that I've created in python3 that simulates a virtual button. I have a raspberry pi and I've connected to it a analog sensor. I've created a while loop inside a function that runs for ever and checks if the signal from this sensor passed a threshold variable.
could I create a text window and when ever this threshold is passes to write the character "A" in it, then erase that character and wait until it passes the threshold again so it writes it again.
It works in my terminal but I can't get it to work to qt GUI. It has to do with multithreading but my level in programming is not that advanced. And I don't know any other way to continuously check without the while loop !
A text box of 10 - 20 characters even single line that does that thing, that's all I am asking.
over this code :
#!/usr/bin/python3 import time import spidev # import the SPI driver from time import sleep state="wrt" DEBUG = False vref = 3.3 * 1000 # V-Ref in mV (Vref = VDD for the MCP3002) resolution = 2**10 # for 10 bits of resolution calibration = 38 # in mV, to make up for the precision of the components GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) #start_time=time.time() #elapsed_time=time.time() # MCP3002 Control bits # # 7 6 5 4 3 2 1 0 # X 1 S O M X X X # # bit 6 = Start Bit # S = SGL or \DIFF SGL = 1 = Single Channel, 0 = \DIFF is pseudo differential # O = ODD or \SIGN # in Single Ended Mode (SGL = 1) # ODD 0 = CH0 = + GND = - (read CH0) # 1 = CH1 = + GND = - (read CH1) # in Pseudo Diff Mode (SGL = 0) # ODD 0 = CH0 = IN+, CH1 = IN- # 1 = CH0 = IN-, CH1 = IN+ # # M = MSBF # MSBF = 1 = LSB first format # 0 = MSB first format # ------------------------------------------------------------------------------ # SPI setup spi_max_speed = 1000000 # 1 MHz (1.2MHz = max for 2V7 ref/supply) # reason is that the ADC input cap needs time to get charged to the input level. CE = 0 # CE0 | CE1, selection of the SPI device spi = spidev.SpiDev() spi.open(0,CE) # Open up the communication to the device spi.max_speed_hz = spi_max_speed # # create a function that sets the configuration parameters and gets the results # from the MCP3002 # def read_mcp3002(channel): # see datasheet for more information # 8 bit control : # X, Strt, SGL|!DIFF, ODD|!SIGN, MSBF, X, X, X # 0, 1, 1=SGL, 0 = CH0 , 0 , 0, 0, 0 = 96d # 0, 1, 1=SGL, 1 = CH1 , 0 , 0, 0, 0 = 112d if channel == 0: cmd = 0b01100000 else: cmd = 0b01110000 if DEBUG : print("cmd = ", cmd) spi_data = spi.xfer2([cmd,0]) # send hi_byte, low_byte; receive hi_byte, low_byte if DEBUG : print("Raw ADC (hi-byte, low_byte) = {}".format(spi_data)) # receive data range: 000..3FF (10 bits) # MSB first: (set control bit in cmd for LSB first) # spidata[0] = X, X, X, X, X, 0, B9, B8 # spidata[1] = B7, B6, B5, B4, B3, B2, B1, B0 # LSB: mask all but B9 & B8, shift to left and add to the MSB adc_data = ((spi_data[0] & 3) << 8) + spi_data[1] return adc_data def main(): while state == "wrt": channeldata_1 = read_mcp3002(0) # get CH0 input sleep(0.001) channeldata_2 = read_mcp3002(0) # get CH0 input sleep(0.001) channeldata_3 = read_mcp3002(0) # get CH0 input channeldata = (channeldata_1+channeldata_2+channeldata_3)/3 # # Voltage = (CHX data * (V-ref [= 3300 mV] * 2 [= 1:2 input divider]) / 1024 [= 10bit resolution] # voltage = int(round(((channeldata * vref * 2) / resolution),0))+ calibration if ( voltage <= 2500) : #key is pressed print("A", end="", flush=True) sys.stdout.write('\010') if __name__ == '__main__': main()
-
Hi and welcome!
The code you provided does not have any GUI related parts.
But one thing you're probably doing wrong is: endless while loop. If you have such a loop in the Qt GUI thread then you block the event loop and your GUI freezes. You should provide your code including GUI, so others are able to help you.
To avoid a loop which polls the sensor you could use a timer to check the sensor regularly. If you really want to poll in a loop then you should move this loop to another thread, but this is more complex compared to timer. -
@Kourpetis
Hi, it sounds like your code is getting stuck in your while loop. I believe your code needs threading.