2

我有這段代碼可以讀取16個模擬模擬傳感器,通過多路複用器和ADC連接到我的GPIO,並將所有內容都轉換爲相應的字符並並行寫入我的終端字符,我如何刪除和替換隻是最後打印的字符? 現在它只是覆蓋最後一個打印的字符並在它旁邊打印新的。這個項目的目的是創建一個老式的短信模擬器。刪除並替換python中打印到終端的最後一個字符

這是我的代碼:

#!/usr/bin/python 
import time 
import RPi.GPIO as GPIO 
import spidev # import the SPI driver 
from time import sleep 
from array import * 
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.setup(7, GPIO.OUT) 
GPIO.setup(11, GPIO.OUT) 
GPIO.setup(13, GPIO.OUT) 
GPIO.setup(15, GPIO.OUT) 
start_time=time.time() 
elapsed_time=time.time() 
keypressed=0 
i=0 
keyreleased = False 
sensor16=['1','-','\\','/','*','!'] 
sensor15=['4','G','H','I'] 
sensor14=['7','P','Q','R','S'] 
sensor13=['*'] 
sensor12=['2','A','B','C'] 
sensor11=['5','J','K','L'] 
sensor10=['8','T','U','V'] 
sensor09=['0',' '] 
sensor08=['3','D','E','F'] 
sensor07=['6','M','N','O'] 
sensor06=['9','W','X','Y','Z'] 
sensor05=['#'] 
sensor04=['BACKSPACE'] 
sensor03=['DELETE ALL'] 
sensor02=['READ'] 
sensor01=['TRANSMITE'] 
sensor=[sensor01,sensor02,sensor03,sensor04,sensor05,sensor06,sensor07,sensor08,sensor09,sensor10,sensor11,sensor12,sensor13,sensor14,sensor15,sensor16] 
max_press=[1,1,1,1,1,5,4,4,2,4,4,4,1,5,4,6] 
num_press=0 
steps=0 

# 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 
# ------------------------------------------------------------------------------ 
#events = (uinput.KEY_X, uinput.KEY_H, uinput.KEY_E, uinput.KEY_L, uinput.KEY_O) 
#device = uinput.Device(events) 

# 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 
# 
#events = (uinput.KEY_X, uinput.KEY_H, uinput.KEY_E, uinput.KEY_L, uinput.KEY_O) 
#device = uinput.Device(events) 

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 


try: 
    while True: 
     for x in range(0, 16): # setting the 4 channels of the multiplexer HIGH or LOW accordinlgy 
      if x == 0: 
       GPIO.output(7, 0) 
       GPIO.output(11, 0) 
       GPIO.output(13, 0) 
       GPIO.output(15, 0) 
      elif x == 1: 
       GPIO.output(7, 1) 
       GPIO.output(11, 0) 
       GPIO.output(13, 0) 
       GPIO.output(15, 0) 
      elif x == 2: 
       GPIO.output(7, 0) 
       GPIO.output(11, 1) 
       GPIO.output(13, 0) 
       GPIO.output(15, 0) 
      elif x == 3: 
       GPIO.output(7, 1) 
       GPIO.output(11, 1) 
       GPIO.output(13, 0) 
       GPIO.output(15, 0) 
      elif x == 4: 
       GPIO.output(7, 0) 
       GPIO.output(11, 0) 
       GPIO.output(13, 1) 
       GPIO.output(15, 0) 
      elif x == 5: 
       GPIO.output(7, 1) 
       GPIO.output(11, 0) 
       GPIO.output(13, 1) 
       GPIO.output(15, 0) 
      elif x == 6: 
       GPIO.output(7, 0) 
       GPIO.output(11, 1) 
       GPIO.output(13, 1) 
       GPIO.output(15, 0) 
      elif x == 7: 
       GPIO.output(7, 1) 
       GPIO.output(11, 1) 
       GPIO.output(13, 1) 
       GPIO.output(15, 0) 
      elif x == 8: 
       GPIO.output(7, 0) 
       GPIO.output(11, 0) 
       GPIO.output(13, 0) 
       GPIO.output(15, 1) 
      elif x == 9: 
       GPIO.output(7, 1) 
       GPIO.output(11, 0) 
       GPIO.output(13, 0) 
       GPIO.output(15, 1) 
      elif x == 10: 
       GPIO.output(7, 0) 
       GPIO.output(11, 1) 
       GPIO.output(13, 0) 
       GPIO.output(15, 1) 
      elif x == 11: 
       GPIO.output(7, 1) 
       GPIO.output(11, 1) 
       GPIO.output(13, 0) 
       GPIO.output(15, 1) 
      elif x == 12: 
       GPIO.output(7, 0) 
       GPIO.output(11, 0) 
       GPIO.output(13, 1) 
       GPIO.output(15, 1) 
      elif x == 13: 
       GPIO.output(7, 1) 
       GPIO.output(11, 0) 
       GPIO.output(13, 1) 
       GPIO.output(15, 1) 
      elif x == 14: 
       GPIO.output(7, 0) 
       GPIO.output(11, 1) 
       GPIO.output(13, 1) 
       GPIO.output(15, 1) 
      elif x == 15: 
       GPIO.output(7, 1) 
       GPIO.output(11, 1) 
       GPIO.output(13, 1) 
       GPIO.output(15, 1)  
      # average three readings to get a more stable one 
      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 DEBUG : print("Data (bin) {0:010b}".format(channeldata)) 
      if x==15 :  # some problem with this sensor so i had to go and twicked the thresshold 
       voltage = voltage - 500 
      #time.sleep(0.05) 
      if (voltage > 2500) : #key is released 
       keyreleased = True 
      if (voltage < 2500) : #key is pressed 
       keyreleased=False 
       keypressed=x   #define which key is pressed 
       # print(i) 
       if key == keypressed : 
        while keyreleased == False : 

        #for i in range (max_press[keypressed]): 
         # average three readings to get a more stable one 
         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 DEBUG : print("Data (bin) {0:010b}".format(channeldata)) 
         if x==15 :   # some problem with this sensor so i had to go and twicked the thresshold 
          voltage = voltage - 500 
         #time.sleep(0.05) 
         if (voltage > 2500) :  #key is released 
          keyreleased = True 
          i=0 
         if i < max_press[keypressed] and keyreleased == False : 
         ######################################################## 
         #this is where my characters are printed but i need to # 
         #print them side by side and to delete just the last # 
         #character if i have to !!!!!!!!!!!     # 
         ######################################################## 
          print("\b", sensor[keypressed][i], end="", flush=True) 
          time.sleep(0.5) 
          i=i+1 
         else : 
          i=0 
      GPIO.output(7, 0) 
      GPIO.output(11, 0) 
      GPIO.output(13, 0) 
      GPIO.output(15, 0) 
      key = keypressed 
      start_time=time.time() 
     if DEBUG : print("-----------------") 


except KeyboardInterrupt: # Ctrl-C 
    if DEBUG : print ("Closing SPI channel") 
    spi.close() 

def main(): 
    pass 

if __name__ == '__main__': 
    main() 

任何想法如何?

回答

2

大多數終端移動插入符號向後一旦當退格字符(ASCII 0x08的)印刷:

sys.stdout.write('...'); # print "..." 
time.sleep(1); # wait a second 
sys.stdout.write('\010\010\010Done.\n') # replace "..." with "Done." 

要刪除文本簡單地與退格字符向後移動,然後寫空間。

另外,在大多數終端:

sys.stdout.write('...'); # print "..." 
time.sleep(1); # wait a second 
sys.stdout.write('\033[2K\033[1G') # erase line and go to beginning of line 
sys.stdout.write('Done.\n') # print "Done." 

您可以使用以下任何ANSI轉義序列在支持它們(這是大多數現代終端)終端:

  • '\033[#D':移動光標留下#個字符。
  • '\033[2K':清除當前行(但不要移動光標)。
  • '\033[1K':清除當前位置左側的行。
  • '\033[0K':清除當前位置右側的行。
  • '\033[1G':將光標移動到行首。
  • '\033[#;#f':將光標移動到指定位置。第一個#是行號,第二個#是列號。

維基百科作爲方便的總結ANSI Escape Codes

0

好吧,我在這裏搞砸了。它部分地起作用:

if i < max_press[keypressed] and keyreleased == False 
    ######################################################## 
    #this is where my characters are printed but i need to # 
    #print them side by side and to delete just the last # 
    #character if i have to !!!!!!!!!!!     # 
    ####################################################### 
     print(sensor[keypressed][i], end="", flush=True) 
     time.sleep(0.5) 
     sys.stdout.write('\010') 
     i=i+1 
    else : 
     sys.stdout.write('\033[1C') 
     i=0 

並且它在同一個地方去並且重複字符,直到達到最後的值。然後它右移一行並重復相同的過程,我不知道爲什麼? LLLLLOOOOOOSSSSTTTTTT

相關問題