删除并替换python

时间:2016-02-20 04:34:28

标签: python-3.x terminal character project-management raspberry-pi2

我有这个代码读取16个模拟模拟传感器通过多路复用器和ADC连接到我的GPIO并相应地将所有内容转换为字符并将每个字符并排写入我的终端,如何删除和替换最后打印的性格? 现在它只是覆盖最后一个打印的字符,并在它旁边打印新的字符。这个项目的目的是创建一个oldschool短信模拟器。

这是我的代码:

#!/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 个答案:

答案 0 :(得分:3)

大多数终端在打印退格字符(ASCII 0x08)时向后移动插入符号:

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."

要删除文本,只需使用退格符向后移动,然后写入空格。

或者,在大多数终端上:

'\033[#D'

您可以在支持它们的终端(最现代的终端)上使用以下任何ANSI转义序列:

  • #:将光标向左移动'\033[2K'个字符。
  • '\033[1K':清除当前行(但不要移动光标)。
  • '\033[0K':清除当前位置左侧的行。
  • '\033[1G':清除当前位置右侧的行。
  • '\033[#;#f':将光标移动到行首。
  • #:将光标移动到特定位置。第一个#是行号,第二个StreamReader是列号。

维基百科是ANSI Escape Codes的便捷摘要。

答案 1 :(得分: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