def __init__(self, height, width, sim_speed=2, default=1): ''' Initialize simulation Input:\n height,width - Height and width of the screen default - Set up platform and box init parameters (optional) Output: None ''' ## Setup init Pygame ## pygame.init() self.sim_speed = 1 / sim_speed #seconds # Initalize text pygame.font.init() self.font = pygame.font.SysFont("arial", 10, bold=False) self.screen = pygame.display.set_mode((width, height)) self.clock = pygame.time.Clock() #Buttons state self.is_sim_run = 0 self.is_reset = 0 #Objects self.buttons = Buttons(self.screen, self.clock, self.sim_speed) self.box = None #Box object self.platform = None #Platform object self.box_path = None #Box path object self.controler = None #Platform controler object if default: self.setup_objects_default()
class Hardware(): def __init__(self): self.matrixReader = MatrixReader() self.buttons = Buttons() # Array of Light GPIOS self.lights = ShiftReg595(10, 8, 7, 11, ["NC0", "White", "Red", "ent_light", "sel_light", "throw_light", "remove_light", "NC1"]) self.board_lights() def board_lights(self): self.lights["White"] = 1 self.lights["Red"] = 0 def light_switch(self, input): light = input.split() if (light[0] in self.lights): if light[1] == "0": self.lights[light[0]] = 0 elif light[1] == "1": self.lights[light[0]] = 1 def refresh(self): global buff buff.extend(self.matrixReader.get_reads()) buff.extend(self.buttons.get_reads()) def end(self): for i in self.lights: self.lights[i] = 0 GPIO.cleanup()
def runMain(self): #Create instance of standby self.startup = Startup() #Initialize the serial port and return the connection variable and status self.serialStatus, self.serialConnection = self.startup.initSerial() #Get first day, first sec and emission coefficient #self.things is an array. self.things = self.startup.sendReceive() self.firstDay = self.things[0] self.firstSec = self.things[1] self.emCoeffS = self.things[2] #Create an instane of the button class self.buttons = Buttons() #Pass on the serial connection self.buttons.buttonSerial = self.serialConnection #Pass the first day, first second and emission coefficient to buttons self.buttons.firstDay = self.firstDay self.buttons.firstSec = self.firstSec self.buttons.emCoeff = self.emCoeffS #Print Emission Coefficient to GUI self.buttons.printEmCoeff(self.buttons.emCoeff) #Print serial status to statusbar for 5 seconds self.statusBar().showMessage(self.serialStatus, 5000) #Set the central widget (Put buttons in window) #Buttons will be running most of the window from here. self.setCentralWidget(self.buttons)
def __init__(self): self.matrixReader = MatrixReader() self.buttons = Buttons() # Array of Light GPIOS self.lights = ShiftReg595(10, 8, 7, 11, ["NC0", "White", "Red", "ent_light", "sel_light", "throw_light", "remove_light", "NC1"]) self.board_lights()
def __init__(self, width, height, screen, mixer): self.__screen = screen self.logo = logo self.buttons = Buttons( [[ Caption("QUIT", 30, config.colors.get("Grey")), Caption("HIGHSCORES", 30, config.colors.get("Grey")), Caption("START", 30) ], [ Caption("PLAYER NAME: ", 30, config.colors.get("Grey")), Caption(config.player_name, 30, config.colors.get("Grey")) ], [ Caption("DIFFICULTY: ", 30, config.colors.get("Grey")), Caption(str(config.difficulty), 30, config.colors.get("Grey")) ], [Caption("MUSIC: ON", 30, config.colors.get("Grey"))]]) self.width = width self.height = height self.selection = Caption("QUIT") self.mixer = mixer
def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic") # the container is where we'll stack a bunch of frames # on top of each other, then the one we want visible # will be raised above the others container = tk.Frame(self) container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (StartPage, PageOne, PageTwo, Interface): page_name = F.__name__ frame = F(parent=container, controller=self) self.frames[page_name] = frame # put all of the pages in the same location; # the one on the top of the stacking order # will be the one that is visible. frame.grid(row=0, column=0, sticky="nsew") page_name = Interface.__name__ interface = Interface(parent=container, controller=self) createMenu = CreateMenu() tbs = TopButtonSelect() showPic = ShowPicture() openPic = OpenPicture(interface.fileName, interface.new_fileName) openAns = OpenAnswer(interface.file_answer) # openPic = OpenPicture(interface.fileName) makePicPath = MakePicPath(interface.new_fileName) # makePicPath = MakePicPath(interface.fileName) savePic = SavePicture() createMenu.creatMenu(self, tbs, openPic, openAns, showPic, interface, savePic, makePicPath) var = IntVar() buttons = Buttons(self, tbs, interface, makePicPath, showPic, openPic, var, openAns) buttons.topButton() buttons.nextButton() self.show_frame("StartPage")
class Menu: ############# MENU HANDLER ################# def __init__(self, width, height, screen, mixer): self.__screen = screen self.logo = logo self.buttons = Buttons( [[ Caption("QUIT", 30, config.colors.get("Grey")), Caption("HIGHSCORES", 30, config.colors.get("Grey")), Caption("START", 30) ], [ Caption("PLAYER NAME: ", 30, config.colors.get("Grey")), Caption(config.player_name, 30, config.colors.get("Grey")) ], [ Caption("DIFFICULTY: ", 30, config.colors.get("Grey")), Caption(str(config.difficulty), 30, config.colors.get("Grey")) ], [Caption("MUSIC: ON", 30, config.colors.get("Grey"))]]) self.width = width self.height = height self.selection = Caption("QUIT") self.mixer = mixer def run(self): run = True while run: self.selection = self.buttons.handle_events() if self.selection is not None: if self.selection == Caption("QUIT"): return 0 elif self.selection == Caption("HIGHSCORES"): return 1 elif self.selection == Caption("START"): return 2 elif self.selection == Caption( "PLAYER NAME: " ) or self.selection == self.buttons.buttons[1][1]: self.selection = self.buttons.buttons[1][1] self.buttons.row = 1 self.buttons.col = 1 self.__get_playername() # let the user type in the name elif self.selection == Caption( "DIFFICULTY: " ) or self.selection == self.buttons.buttons[2][1]: self.selection = self.buttons.buttons[2][1] self.buttons.row = 2 self.buttons.col = 1 self.__get_difficulty() # let the user change difficulty elif self.selection == Caption("MUSIC: ON") or Caption( "MUSIC: OFF"): if self.mixer.is_on(): self.buttons.buttons[3][0] = Caption( "MUSIC: OFF", 30) else: self.buttons.buttons[3][0] = Caption( "MUSIC: ON", 30) self.mixer.change_mode() self.update() return self.selection def update(self): self.__screen.fill((0, 0, 0)) self.__screen.blit(self.logo, ((self.width - self.logo.get_width()) / 2, self.height - self.logo.get_height() - 50)) self.buttons.update(self.__screen) pygame.display.update() def open_highscores(self): run = True highscores = File(config.highscores_filename) highscores.open() title = Caption("HIGHSCORE TABLE", 30) x_pos = int((self.width - title.get_width()) / 2) while run: y_pos = 40 self.__screen.fill((0, 0, 0)) self.__screen.blit(title.text, (x_pos, y_pos)) y_pos += title.get_height() for score in highscores.data: player = (Caption(score.player_name, 20), Caption(": ", 20), Caption(score.score, 20)) y_pos += int((player[0].get_height() / 2) * 3) for caption in player: self.__screen.blit(caption.text, (x_pos, y_pos)) x_pos += caption.get_width() x_pos = int((self.width - title.get_width()) / 2) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return False pygame.display.update() def __get_playername(self): player_name = "" run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: if len(player_name): player_name = player_name[:-1] elif event.key == pygame.K_ESCAPE: player_name = config.player_name self.buttons.buttons[1][1] = Caption( player_name, 30) run = False elif event.key == pygame.K_RETURN: config.player_name = player_name run = False elif len(player_name) < 16: player_name += event.unicode self.buttons.buttons[1][1] = Caption(player_name, 30) self.update() self.selection = self.buttons.buttons[1][0] self.buttons.row = 1 self.buttons.col = 0 def __get_difficulty(self): difficulty = config.difficulty run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_w or event.key == pygame.K_UP: if difficulty < config.max_level: difficulty += 1 elif event.key == pygame.K_s or event.key == pygame.K_DOWN: if difficulty > config.min_level: difficulty -= 1 elif event.key == pygame.K_ESCAPE: difficulty = config.difficulty self.buttons.buttons[2][1] = Caption( config.difficulty, 30) run = False elif event.key == pygame.K_SPACE or event.key == pygame.K_RETURN: config.difficulty = difficulty run = False self.buttons.buttons[2][1] = Caption(difficulty, 30) self.update() self.selection = self.buttons.buttons[2][0] self.buttons.row = 2 self.buttons.col = 0
from subprocess import * from time import sleep, strftime from datetime import datetime configfile = 'lcdmenu.xml' # set DEBUG=1 for print debug statements DEBUG = 0 DISPLAY_ROWS = 2 DISPLAY_COLS = 16 # set busnum param to the correct value for your pi # RS, E, [Data] lcd = Adafruit_CharLCD(14, 15, [17, 18, 27, 22]) btn = Buttons("lcdmenu", "/home/pi/.lircrc") # in case you add custom logic to lcd to check if it is connected (useful) #if lcd.connected == 0: # quit() lcd.begin(DISPLAY_COLS, DISPLAY_ROWS) # commands def DoQuit(): lcd.clear() lcd.message('Are you sure?\nPress Sel for Y') while 1: nb = btn.readButton() if nb == 'left':
import os import time import smbus import socket import fcntl import struct import oledFunc import commands from Buttons import Buttons # open /dev/i2c-1 bus = smbus.SMBus(1) # 导入按键设置 btn = Buttons("radio", "/etc/lirc/irexec.conf") # RDA REGISTER RDA_reg_02H = 0xd0 RDA_reg_data = [0x00, 0x00, 0x00, 0x00, 0x40, 0x90, 0x88] # 定时关机时间及标志位 flag_timing = 10 flag_shutdown = 0 radio_timing = 0 # SET I2C ADDRESS oledaddress = 0x51 radioaddress = 0x10 # 频道名称汉字 class ChannelWord:
class ClimateStationWindow(QtGui.QMainWindow): def __init__(self): #Inherit from QtGui.QMainWindow and initiate super(ClimateStationWindow, self).__init__() #Array with parameters from Startup() self.things = None #Day and second of first data point self.firstDay = None self.firstSec = None #Emission factor self.emCoeffS = None self.serialStatus = None self.serialConnection = None #Run initialization and main self.initUI() self.runMain() #Set window size and title def initUI(self): #Window geometry self.setGeometry(100, 75, 750, 450) self.setWindowTitle("Climate Station Monitor") #Lock the window size self.setMinimumSize(750, 450) self.setMaximumSize(750, 450) #Draw lines in window def paintEvent(self, e): qPainter = QtGui.QPainter() qPainter.begin(self) self.drawLines(qPainter) qPainter.end() #Function for drawing frames in main window def drawLines(self, qPainter): pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine) qPainter.setPen(pen) #Draw first frame qPainter.drawLine(45, 55, 30, 55) qPainter.drawLine(30, 55, 30, 360) qPainter.drawLine(30, 360, 730, 360) qPainter.drawLine(400, 360, 400, 55) qPainter.drawLine(400, 55, 130, 55) #Draw second frame qPainter.drawLine(445, 55, 400, 55) qPainter.drawLine(730, 360, 730, 55) qPainter.drawLine(730, 55, 610, 55) #Main function. Runs startup and passes its parameters # to the Button() object. def runMain(self): #Create instance of standby self.startup = Startup() #Initialize the serial port and return the connection variable and status self.serialStatus, self.serialConnection = self.startup.initSerial() #Get first day, first sec and emission coefficient #self.things is an array. self.things = self.startup.sendReceive() self.firstDay = self.things[0] self.firstSec = self.things[1] self.emCoeffS = self.things[2] #Create an instane of the button class self.buttons = Buttons() #Pass on the serial connection self.buttons.buttonSerial = self.serialConnection #Pass the first day, first second and emission coefficient to buttons self.buttons.firstDay = self.firstDay self.buttons.firstSec = self.firstSec self.buttons.emCoeff = self.emCoeffS #Print Emission Coefficient to GUI self.buttons.printEmCoeff(self.buttons.emCoeff) #Print serial status to statusbar for 5 seconds self.statusBar().showMessage(self.serialStatus, 5000) #Set the central widget (Put buttons in window) #Buttons will be running most of the window from here. self.setCentralWidget(self.buttons) #Question box for quitting the program def closeEvent(self, event): reply = QtGui.QMessageBox.question(self, 'Confirm Exit', 'Are you sure you want to quit?', QtGui.QMessageBox.Yes | QtGui.QMessageBox.No) if (reply == QtGui.QMessageBox.Yes): event.accept() else: event.ignore() #Close window when "ESC" is pressed def keyPressEvent(self, e): if e.key() == QtCore.Qt.Key_Escape: #Close serial port. self.serialConnection.close() #Close the program self.close()
from subprocess import * from time import sleep, strftime from datetime import datetime configfile = 'lcdmenu.xml' # set DEBUG=1 for print debug statements DEBUG = 0 DISPLAY_ROWS = 2 DISPLAY_COLS = 16 # set busnum param to the correct value for your pi # RS, E, [Data] lcd = Adafruit_CharLCD(14, 15, [17,18,27,22]) btn = Buttons("lcdmenu", "/home/pi/.lircrc") # in case you add custom logic to lcd to check if it is connected (useful) #if lcd.connected == 0: # quit() lcd.begin(DISPLAY_COLS, DISPLAY_ROWS) # commands def DoQuit(): lcd.clear() lcd.message('Are you sure?\nPress Sel for Y') while 1: nb = btn.readButton() if nb == 'left': break;
class Main_window(): def __init__(self, height, width, sim_speed=2, default=1): ''' Initialize simulation Input:\n height,width - Height and width of the screen default - Set up platform and box init parameters (optional) Output: None ''' ## Setup init Pygame ## pygame.init() self.sim_speed = 1 / sim_speed #seconds # Initalize text pygame.font.init() self.font = pygame.font.SysFont("arial", 10, bold=False) self.screen = pygame.display.set_mode((width, height)) self.clock = pygame.time.Clock() #Buttons state self.is_sim_run = 0 self.is_reset = 0 #Objects self.buttons = Buttons(self.screen, self.clock, self.sim_speed) self.box = None #Box object self.platform = None #Platform object self.box_path = None #Box path object self.controler = None #Platform controler object if default: self.setup_objects_default() def reset_sim(self): self.setup_objects_default() def setup_objects_default(self): ''' Function for setting up default simulation parameters\n Input: None\n Output: None ''' self.platform = Platform((200, 200), (10, 10), 50, self.screen, self.font, self.sim_speed) self.box = Box((40, 80), [450, 700, 0], self.sim_speed, self.screen) def run_sim(self): ''' Function handling simulation\n Input: None\n Output: None ''' #Update simulation state self.is_sim_run, self.is_reset = self.buttons.get_simulaion_state() if self.is_reset: self.reset_sim() if self.is_sim_run: try: #self.platform.setup_all_cells_vel([0.5,-1,0.2]) self.platform.check_contact(self.box) contact_list = self.platform.get_contact_cell_list() if contact_list: self.platform.update_cells_vel( contact_list, [0.5, -1, 0.2]) #for testing purpose self.box.move_obj( self.platform.get_cell_vel( [contact_list[0].get_cell_number()])) except: self.is_sim_run = 0 self.buttons['Start/Stop'].set_text( chenage_text[self.is_sim_run]) print('Cant run sim') self.draw_update_objects() def draw_update_objects(self): ''' Function to draw all objects on\n Input: None\n Output: None ''' self.screen.fill((0, 0, 0)) self.platform.draw_platform(1) self.box.draw_box() self.buttons.draw_buttons()