首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LCD未使用新值更新

LCD未使用新值更新
EN

Stack Overflow用户
提问于 2020-10-23 10:40:09
回答 1查看 61关注 0票数 0

我一直在开发一个在单板计算机和LCD上运行的蚁群模拟器。不过,我遇到了LCD没有正确更新的问题。我有一些变量,比如工人、真菌和叶位,它们会在幕后更新。但是,液晶屏上不会显示新值。只有旧的。它有什么问题?

此外,我知道食物逻辑是错误的,我仍然需要测试它。

代码语言:javascript
复制
#!/usr/bin/python

from multiprocessing import Process
from random import randint
import smbus #make sure to install python-smbus
import time
import sys
import datetime

# Define some device parameters
I2C_ADDR  = 0x27 # I2C device address
LCD_WIDTH = 16   # Maximum characters per line

# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT  = 0x08  # On
#LCD_BACKLIGHT = 0x00  # Off

ENABLE = 0b00000100 # Enable bit

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface
#bus = smbus.SMBus(0)  # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1

queens = 1
larva = 0
workers = 1
aphids = 0
fungus = 5
leafbits = 0
rand = 0
rand2 = 0
antstotal = 2

def lcd_init():
    # Initialise display
    lcd_byte(0x33,LCD_CMD) # 110011 Initialise
    lcd_byte(0x32,LCD_CMD) # 110010 Initialise
    lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
    lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off 
    lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
    lcd_byte(0x01,LCD_CMD) # 000001 Clear display
    time.sleep(E_DELAY)

def lcd_byte(bits, mode):
    # Send byte to data pins
    # bits = the data
    # mode = 1 for data
    #        0 for command

    bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
    bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

    # High bits
    bus.write_byte(I2C_ADDR, bits_high)
    lcd_toggle_enable(bits_high)

    # Low bits
    bus.write_byte(I2C_ADDR, bits_low)
    lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):
    # Toggle enable
    time.sleep(E_DELAY)
    bus.write_byte(I2C_ADDR, (bits | ENABLE))
    time.sleep(E_PULSE)
    bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
    time.sleep(E_DELAY)

def lcd_string(message,line):
    # Send string to display

    message = message.ljust(LCD_WIDTH," ")

    lcd_byte(line, LCD_CMD)

    for i in range(LCD_WIDTH):
        lcd_byte(ord(message[i]),LCD_CHR)

def lcd_print():
    #global starttime
    #colonyname = input("What would you like to name the colony? ")
    while True:
        global antstotal, queens, workers, larva, fungus, aphids, leafbits
        # Send some text
        lcd_string("Name: Lasius", LCD_LINE_1)
        lcd_string("Day: " + seasons(int(datetime.datetime.today().strftime('%w'))), LCD_LINE_2)

        time.sleep(4)
      
        lcd_string("Queens: " + str(queens), LCD_LINE_1)
        lcd_string("Larva: "+ str(larva), LCD_LINE_2)
        
        time.sleep(4)
        
        #third
        lcd_string("Workers: " + str(workers), LCD_LINE_1)
        lcd_string("Aphids: " + str(aphids), LCD_LINE_2)
    
        time.sleep(4)
        
        lcd_string("Fungus: " + str(fungus), LCD_LINE_1)
        lcd_string("Leaf Bits: " + str(leafbits), LCD_LINE_2)
        
        time.sleep(4)
        lcd_init()

def seasons(day):
    #sunday is 0
    #sunday = winter
    #monday = spring
    #tuesday = spring
    #wednesday = summer
    #thursday = summer
    #friday = fall
    #saturday = winter
    if (day == 0):
        season = "Winter"
    elif (day == 1):
        season = "Spring"
    elif (day == 2):
        season = "Spring"
    elif (day == 3):
        season = "Summer"
    elif (day == 4):
        season = "Summer"
    elif (day == 5):
        season = "Fall"
    elif (day == 6):
        season = "Winter"
        
    return (season);

def colony_logic():
    #Logic of the colony:
    #
    #Queens produce 1 larva every hour 
    #larva consume 1 fungus every hour
    #workers collect 1 leafbit every 30 minutes
    #aphids produce food buffer
    #fungus feeds colony
    #leafbits convert to fungus at a rate of 1 every 10 minutes
    #
    #extras:
    #
    #aphids are a random chance every half hour. a number between 1-10. If it is 5 you get an aphid
    x = 2
    while True:
        global antstotal, queens, workers, larva, fungus, aphids, leafbits
        time.sleep(600) #ten minutes post start
        print("x is " + str(x))
        #fungus
        if (x == 7):
            x = 2
        if (leafbits >= 1):
            fungus = fungus + 1
            print("Fungus increased by 1")
            leafbits = leafbits - 1
            print("Leaf bits decreased by 1")
            #x = x + 1
            time.sleep(600)
        elif (x == 2) or (x == 4) or (x == 6):
            #leafbits
            if (workers >= 1) & (seasons(int(datetime.datetime.today().strftime('%w'))) != "Winter"):
                leafbits = leafbits + workers
                print("Workers found " + str(workers) + " leaf bits")
            #aphids
            rand = randint(1, 10)
            if (rand == 5):
                aphids = aphids + 1
                print ("Aphids increased by 1")
            #x = x + 1
        elif (x == 5):  
            #Queens logic. Must have 1 queen, 100 fungus, and slight random chance
            rand = randint(1, 25)
            if (queens >= 1) & (fungus >= 100) & (rand == 5) & (larva >= 1):
                queens = queens + 1
                larva = larva - 1
                print("Queens increased by 1")
                print("Larva decreased by 1")
            #larva logic
            #global queens, fungus
            if (queens >= 1) & (fungus >= 1) & (seasons(int(datetime.datetime.today().strftime('%w'))) != "Winter"):
                larva = (larva + (larva * queens))
                print("Queens laid: " + str(queens) + " eggs")
            #workers logic
                time.sleep(600)
                workers = workers + 1
                larva = larva - 1
                print("A larva turned into a worker")
                time.sleep(600)
            #x = x + 1
        else:
            x = x + 1
            #rot: a random number is chosen every hour from 1-100. If it is 60 you lose fungus
            #Random attacks from other colonies: every hour a random number is picked.
            #   If it is 50 you are attacked and lose 1-25 workers
            #person attacks: every hour
            #person feeds: increases leafbits
        
            #rot logic
        rand = randint(1, 100)
        if (rand == 60) & (x == 6):
            rand2 = randint(1, 25)
            fungus = fungus - rand2
            print(str(rand2) + "fungus rotted")
            #random attacks logic
        if (rand == 50) & (seasons(int(datetime.datetime.today().strftime('%w'))) != "Winter") & (x == 6):
            rand2 = randint(1, 25)
            workers = workers - rand2
            print(str(rand2) + " workers were killed by a rival colony")
        #person attacks
        if (rand == 40) & (x == 6):
            rand2 = randint(1, 25)
            workers = workers - rand2
            print(str(rand2) + " workers were killed by a random human")
        #person feeds
        if (rand == 30) & (x == 6):
            rand2 = randint(1, 25)
            leafbits = leafbits + rand2
            print(str(rand2) + " leaf bits were placed by the colony by a kind human")
        x = x + 1
def food_logic():
    #logic for feeding and starvation
    while True:
        time.sleep(3600)
        global antstotal, queens, workers, larva, fungus, aphids
        antstotal = queens + larva + workers
        print("You have: " + str(antstotal) + " total ants")
        if (seasons(int(datetime.datetime.today().strftime('%w'))) != "Winter"):
            if (fungus > antstotal):
                fungus = fungus - antstotal
                print("Your ants have eaten: " + str(antstotal) + " fungus")
            else:
                fungus = (aphids * 5)
                print("You ran out of food and needed to kill aphids")
                print("You now have: " + str(fungus) + " fungus")
            while (fungus == 0):
                print("Your colony is starving")
                workers = workers - 1
                larva = larva - 1
                if (workers == 0) & (larva == 0) & (queens >= 1):
                    queens = queens - 1
                    print("A queen has died")
                if (antstotal == 0):
                    print("Your colony has died")
                    sys.exit
        else:
            if (fungus > antstotal):
                fungus = fungus - (antstotal/4)
                print("Your ants have eaten: " + str(antstotal) + " fungus")
            else:
                fungus = (aphids * 5)
                print("You ran out of food and needed to kill aphids")
                print("You now have: " + str(fungus) + " fungus")
            while (fungus == 0):
                print("Your colony is starving")
                workers = workers - 1
                larva = larva - 1
                if (workers == 0) & (larva == 0) & (queens >= 1):
                    queens = queens - 1
                    print("A queen has died")
                    time.sleep(3600)
                if (antstotal == 0):
                    print("Your colony has died")
                    sys.exit


def main():
    lcd_init()
    p1 = Process(target = colony_logic)
    p2 = Process(target = food_logic)
    p3 = Process(target = lcd_print)
    p1.start()
    p2.start()
    p3.start()
    p1.join()
    p2.join()
    p3.join()
    
if __name__ == '__main__':
    main()

编辑:

下面是一个更小的代码示例。我已经将它还原为LCD代码和一个递增的行。

我已确定问题与液晶屏代码有关。在没有LCD部分的情况下,使用命令行或在控制台的Eclipse中运行我的原始代码都运行得很好。我的意思是,如果我使用print()而不是打印到LCD,它可以很好地工作。尝试在液晶屏上显示变量时出现问题。显示原始值,但如果它们更新或递增,则不会显示新值。

我可能得重新考虑LCD的代码了。如果这是唯一给我带来问题的东西,那么它可能值得用不同的方式来做。

代码语言:javascript
复制
#!/usr/bin/python
#--------------------------------------
# Adapted/Started from Matt Hawkins LCD code, here is his website
# http://www.raspberrypi-spy.co.uk/
#
#--------------------------------------
from multiprocessing import Process
import smbus #make sure to install python-smbus
import time
import sys

# Define some device parameters
I2C_ADDR  = 0x27 # I2C device address
LCD_WIDTH = 16   # Maximum characters per line

# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT  = 0x08  # On
#LCD_BACKLIGHT = 0x00  # Off

ENABLE = 0b00000100 # Enable bit

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface
#bus = smbus.SMBus(0)  # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1

incx = 0

def lcd_init():
    # Initialise display
    lcd_byte(0x33,LCD_CMD) # 110011 Initialise
    lcd_byte(0x32,LCD_CMD) # 110010 Initialise
    lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
    lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off 
    lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
    lcd_byte(0x01,LCD_CMD) # 000001 Clear display
    time.sleep(E_DELAY)

def lcd_byte(bits, mode):
    # Send byte to data pins
    # bits = the data
    # mode = 1 for data
    #        0 for command

    bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
    bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

    # High bits
    bus.write_byte(I2C_ADDR, bits_high)
    lcd_toggle_enable(bits_high)

    # Low bits
    bus.write_byte(I2C_ADDR, bits_low)
    lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):
    # Toggle enable
    time.sleep(E_DELAY)
    bus.write_byte(I2C_ADDR, (bits | ENABLE))
    time.sleep(E_PULSE)
    bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
    time.sleep(E_DELAY)

def lcd_string(message,line):
    # Send string to display

    message = message.ljust(LCD_WIDTH," ")

    lcd_byte(line, LCD_CMD)

    for i in range(LCD_WIDTH):
        lcd_byte(ord(message[i]),LCD_CHR)

def lcd_print():
    #global starttime
    #colonyname = input("What would you like to name the colony? ")
    while True:
        global incx
        # Send some text
        lcd_string("Variable: " + str(incx), LCD_LINE_1)

        time.sleep(4)

def inc_logic():
    global incx
    incx = incx + 1
    time.sleep(4)
def main():
    lcd_init()
    #inc_logic() doesn't work either
    p1 = Process(target = inc_logic)
    p2 = Process(target = lcd_print)
    p1.start()
    p2.start()
    p1.join()
    p2.join()
    
if __name__ == '__main__':

    main()
EN

回答 1

Stack Overflow用户

发布于 2020-10-25 11:08:44

我已经将这个项目切换到了PILCD库。我将继续测试它,如果我有更多的问题,我会尝试更多的库,并可能稍后在这里发布新的帖子,因为代码现在有很大的不同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64493094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档