def update_state(self, screen, frametime): ''' update the screen ''' events = pygame.event.get() self.handle_events(events, screen) screen.update(frametime)
def doActions(): try: log.current_state = "Getting light and water info from server" response = httpclient.get(api.__get_lightwater__(node.node_info['id']), auth.checkResponse) scn.update(server_resp="{} {}".format(response.status, response.reason)) log.current_state = "Deserializing light and water info from server" _node = json.loads(response.body) log.current_state = "Setting light pin" sys_gpio.setRelay(sys_gpio.__light_pin__, _node['lightOn']) log.current_state = "Setting water pin" sys_gpio.setRelay(sys_gpio.__water_pin__, _node['waterOn']) except TimeoutError: error_msg = "HTTP Timeout - main.py @ doActions()" #print error_msg, "\n" scn.update(last_error="Get Water and Light - at " + datetime.now().isoformat()) log.log_error(error_msg) return return
def show_time(force=False): global last_time (year, month, mday, hour, minute, second, weekday, yearday) = utime.localtime() if last_time[0] != minute or (last_time[1] != second and second % 60 == 0) or force: last_time = (minute, second) # show time screen.clear() screen.draw_text('当前时间', (width - 64) // 2, 32, screen.FNT_HZK16) # 48 screen.draw_text('{:02d}:{:02d}'.format(hour, minute), 0, 48, screen.FNT_ASC48) # 96 # screen.draw_text('{:02d}'.format(second), width - 32, 96, screen.FNT_ASC16) #112 # screen.draw_text('秒', width - 16, 96, screen.FNT_HZK16) offset = (width - 112) // 2 screen.draw_text('{:04d}'.format(year), 0 + offset, 112, screen.FNT_ASC16) screen.draw_text('年', 32 + offset, 112, screen.FNT_HZK16) screen.draw_text('{:02d}'.format(month), 48 + offset, 112, screen.FNT_ASC16) screen.draw_text('月', 64 + offset, 112, screen.FNT_HZK16) screen.draw_text('{:02d}'.format(mday), 80 + offset, 112, screen.FNT_ASC16) screen.draw_text('日', 96 + offset, 112, screen.FNT_HZK16) # 112 + 16 + 8 = 136 offset = (width - 48) // 2 screen.draw_text('星期', 0 + offset, 136, screen.FNT_HZK16) screen.draw_text([ '一', '二', '三', '四', '五', '六', '日', ][weekday], 32 + offset, 136, screen.FNT_HZK16) if force: screen.update() else: screen.update_fast() screen.update_fast()
def btn_event(): if btn.value() == 0: utime.sleep_ms(10) count = 0 # hold while btn.value() == 0: count += 1 utime.sleep_ms(10) if count >= 500: screen.clear() screen.update() mainloop.stop() return # click if count > 100: __show_info('开发模式') mainloop.stop() return if count > 0: sync_time() show_time(force=True)
def sendReadings(nextRun): if datetime.now() >= nextRun: #turn on the sensors # the transisotr enable the sensors to be powered sys_gpio.setTransistor(True) log.current_state = "Reading Air Temperature" air_temp = DHT11TemperatureSensor.read() log.current_state = "Posting Air Temperature" httpclient.post(api.__send_readings__, air_temp, auth.checkResponse) scn.update(env_temp=str(air_temp['reading'])) log.current_state = "Reading Air Humidity" air_hum = DHT11HumiditySensor.read() log.current_state = "Posting Air Humidity" httpclient.post(api.__send_readings__, air_hum, auth.checkResponse) scn.update(env_hum=str(air_hum['reading'])) log.current_state = "Reading Luminosity" air_lum = LDR.read() log.current_state = "Posting Luminosity" httpclient.post(api.__send_readings__, air_lum, auth.checkResponse) scn.update(env_lum="{0:0.2f}".format(air_lum['reading'])) log.current_state = "Reading Soil Temperature" soil_temp = DS18B20TemperatureSensor.read() log.current_state = "Reading Soil Temperature" httpclient.post(api.__send_readings__, soil_temp, auth.checkResponse) scn.update(soil_temp="{0:0.2f}".format(soil_temp['reading'])) sys_gpio.setTransistor(False) #print air_lum["date"] #print "Env: Temp.: {0:0.2f}C Humidity: {1:0.2f}% & Luminosity:{2:0.2f}%".format(air_temp['reading'], air_hum['reading'], air_lum['reading']) #print "Soil: Temp.: {0:0.2f}C & Moisture: 0%".format(soil_temp['reading']) #print "\n" log.log_info("System Ok") nextRun = datetime.now() + timedelta(seconds=60) return nextRun
def updateNode(nextRun): if datetime.now() >= nextRun: #print 'Start updating the node...' log.current_state = "Getting node info from server" scn.update(node_updating="Updating node info [Calling Server]") response = httpclient.get(api.__get_node__(node.node_info['id']), auth.checkResponse) scn.update(server_resp="{} {}".format(response.status, response.reason)) log.current_state = "Deserializing node info" body = json.loads(response.body) log.current_state = "Updating node info" scn.update(node_updating="Updating node info [Saving Info]") node.update(body) scn.update(node_updating="Last update at " + datetime.now().isoformat()) nextRun = datetime.now() + timedelta(seconds=60) #print 'Node update finised\n' return nextRun
screen = screen.Introduction() def repaint(): display.fill(0x000000) screen.displayOutput(display) graphics.userInterface.cursor.draw(display) pygame.display.flip() def handleInput(): global screen for event in pygame.event.get(): if hasattr(event, "key"): keyboard.handle(event) elif event.type == pygame.QUIT: screen = None else: screen = screen.respondToUserInput(event) for event in keyboard.getPressed(): screen = screen.respondToUserInput(event) keyboard.tick() graphics.userInterface.cursor.update() while screen is not None: repaint() screen.update() handleInput() clock.tick(60) pygame.quit()
try: print "\n----------------------\n" log.current_state = "Ready to setup the GPIO pins" sys_gpio.setup() print "GPIO Setup Succeeded" log.current_state = "GPIO pins set" print "\n----------------------\n" except Exception as e: print "GPIO Setup Error" print "GPIO Clean" sys_gpio.clean() print "GPIO Setup Succeeded" sys_gpio.setup() scn.update() scn.update(soil_moist = str(0)) # remove later nextSensorReading = datetime.now() nextNodeUpdate = datetime.now() delay = 0.25 while True: try: nextSensorReading = sendReadings(nextSensorReading) nextNodeUpdate = updateNode(nextNodeUpdate) doActions() scn.update() time.sleep(delay) except KeyboardInterrupt: scn.end()
#convert mouse coordinates if needed event2 = event #this is the most unpythonic code ever written. It has to be done, as event objects are immutable if event.type == pygame.MOUSEBUTTONDOWN or event.type == pygame.MOUSEBUTTONUP: event2 = pygame.event.Event( event.type, pos=event.pos - Vec2d(screen.offset, 0), button=event.button) #compensate for screen movement elif event.type == pygame.MOUSEMOTION: event2 = pygame.event.Event(event.type, pos=event.pos - Vec2d(screen.offset, 0)) spider.proccess_event(event2) #logic screen.update() spider.update() frog.update_frogs(screen, spider) bird.update_birds(screen) wasp.update_wasps(screen) stinger.update_bullets(screen) venom.update_shots(screen) flingable.update_flingables(screen) bomb.update_bombs() hand.update_hands(spider, frames) movement.update_movement(screen) #kill wasps and stingers with flingables for x in flingable.all_flung: #needs to be a while loop for pointy in stinger.all_bullets: