def __init__(self, ipcon, key_queue): if not config.UID_DUAL_BUTTON_BRICKLET[0]: print("Not Configured: Dual Button 1") if not config.UID_DUAL_BUTTON_BRICKLET[1]: print("Not Configured: Dual Button 2") self.key_queue = key_queue self.ipcon = ipcon if config.UID_DUAL_BUTTON_BRICKLET[0]: self.db1 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[0], self.ipcon) else: self.db1 = None if config.UID_DUAL_BUTTON_BRICKLET[1]: self.db2 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[1], self.ipcon) else: self.db2 = None if self.db1: try: self.db1.get_button_state() print("Found: Dual Button 1 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[0]) except: self.db1 = None print("Not Found: Dual Button 1 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[0]) if self.db2: try: self.db2.get_button_state() print("Found: Dual Button 2 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[1]) except: self.db2 = None print("Not Found: Dual Button 2 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[1]) if self.db1: self.db1.register_callback(self.db1.CALLBACK_STATE_CHANGED, self.cb_state_changed1) if self.db2: self.db2.register_callback(self.db2.CALLBACK_STATE_CHANGED, self.cb_state_changed2) self.press_timer = RepeatedTimer(0.1, self.press_tick)
def __init__(self, ipcon, key_queue): if not config.UID_DUAL_BUTTON_BRICKLET[0]: print("Not Configured: Dual Button 1") if not config.UID_DUAL_BUTTON_BRICKLET[1]: print("Not Configured: Dual Button 2") self.key_queue = key_queue self.ipcon = ipcon if config.UID_DUAL_BUTTON_BRICKLET[0]: self.db1 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[0], self.ipcon) else: self.db1 = None if config.UID_DUAL_BUTTON_BRICKLET[1]: self.db2 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[1], self.ipcon) else: self.db2 = None if self.db1: try: self.db1.get_button_state() print("Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0]) except: self.db1 = None print("Not Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0]) if self.db2: try: self.db2.get_button_state() print("Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1]) except: self.db2 = None print("Not Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1]) if self.db1: self.db1.register_callback(self.db1.CALLBACK_STATE_CHANGED, self.cb_state_changed1) if self.db2: self.db2.register_callback(self.db2.CALLBACK_STATE_CHANGED, self.cb_state_changed2) self.press_timer = RepeatedTimer(0.1, self.press_tick)
def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # enumeration is for dual button bricklet if device_identifier == DualButton.DEVICE_IDENTIFIER: if self.db != None: return # create dual button device object self.db = DualButton(uid, self.ipcon) # register button push callback to function cb_state_changed self.db.register_callback(self.db.CALLBACK_STATE_CHANGED, self.cb_state_changed) # set led state self.db.set_led_state(DualButton.LED_STATE_ON, DualButton.LED_STATE_OFF) Timer(LRF.TIMER_MS, self.led_blink).start()
def start_ipcon(self): if self._ipcon != None: EventLogger.warning(self.name + " tried to start IPCON, but IPCON was already started!") return self._ipcon = IPConnection() # Init Components self._db = DualButton(self._DB_UID, self._ipcon) self._nfc = NFCRFID(self._NFC_UID, self._ipcon) try: self._ipcon.connect(self._HOST, self._PORT) except Exception as e: EventLogger.critical("A critical error occur: " + str(e)) raise Exception("A critical error occur: " + str(e)) #Init Callbacks self._db.register_callback(DualButton.CALLBACK_STATE_CHANGED, self.__cb_db_state_changed) self._nfc.register_callback(self._nfc.CALLBACK_STATE_CHANGED, lambda x, y: self.__cb_nfc_state_changed(x, y, self._nfc))
class GuiControl(): def __init__(self): self._name = "[GuiControl]" # ipcon self._ipcon = None self._HOST = "localhost" self._PORT = 4223 #DualButton States self._db = None self._DB_UID = "mAo" self.recognition_running = True self.recognition_state = False #TODO bad idea? self.recognition_progress = False # NFC/RFID self._nfc = None self._NFC_UID = "oDg" self._nfc_cb_to_profiler = None self._nfc_write_mode = False self._nfc_write_name = None def start_ipcon(self): if self._ipcon != None: EventLogger.warning(self.name + " tried to start IPCON, but IPCON was already started!") return self._ipcon = IPConnection() # Init Components self._db = DualButton(self._DB_UID, self._ipcon) self._nfc = NFCRFID(self._NFC_UID, self._ipcon) try: self._ipcon.connect(self._HOST, self._PORT) except Exception as e: EventLogger.critical("A critical error occur: " + str(e)) raise Exception("A critical error occur: " + str(e)) #Init Callbacks self._db.register_callback(DualButton.CALLBACK_STATE_CHANGED, self.__cb_db_state_changed) self._nfc.register_callback(self._nfc.CALLBACK_STATE_CHANGED, lambda x, y: self.__cb_nfc_state_changed(x, y, self._nfc)) def stop_ipcon(self): if self._ipcon == None: EventLogger.warning(self.name + " tried to disconnect IPCON, but IPCON was not connected!") return self._ipcon.disconnect() self._ipcon = None # DualButton Functions def __cb_db_state_changed(self, button_l, button_r, led_r, led_l): #Button Right = Programm Control => True = Program Quit if button_r == DualButton.BUTTON_STATE_PRESSED: led_r = DualButton.LED_STATE_OFF self._db.set_selected_led_state(DualButton.LED_RIGHT, led_r) # exit program self.recognition_running = False if not self.recognition_progress: if button_l == DualButton.BUTTON_STATE_PRESSED: led_l = DualButton.LED_STATE_ON self._db.set_selected_led_state(DualButton.LED_LEFT, led_l) self.recognition_state = True else: led_l = DualButton.LED_STATE_OFF self._db.set_selected_led_state(DualButton.LED_LEFT, led_l) self.recognition_state = False def _db_start_state(self): self._db.set_selected_led_state(DualButton.LED_LEFT, DualButton.LED_STATE_OFF) self._db.set_selected_led_state(DualButton.LED_RIGHT, DualButton.LED_STATE_ON) def _db_end_state(self): self._db.set_selected_led_state(DualButton.LED_LEFT, DualButton.LED_STATE_OFF) self._db.set_selected_led_state(DualButton.LED_RIGHT, DualButton.LED_STATE_OFF) def _db_recognizer_state_off(self): self._db.set_selected_led_state(DualButton.LED_LEFT, DualButton.LED_STATE_OFF) self.recognition_state = False self.recognition_progress = False def __cb_nfc_state_changed(self, state, idle, nr): name = "NO NAME" if state == nr.STATE_REQUEST_TAG_ID_READY: EventLogger.debug('Tag found') # Write 16 byte to pages 5-8 if self._nfc_write_mode: data_write = Utils.string_to_byte_array(self._nfc_write_name) nr.write_page(5, data_write) EventLogger.debug('Writing data...') else: nr.request_page(5) EventLogger.debug('Requesting data...2') elif state == nr.STATE_WRITE_PAGE_READY: # only when writing before! # Request pages 5-8 nr.request_page(5) EventLogger.debug('Requesting data...') elif state == nr.STATE_REQUEST_PAGE_READY: # Get and print pages data = nr.get_page() name = str(Utils.byte_array_to_string(data)) EventLogger.debug('Read data:' + name) if self._nfc_cb_to_profiler != None: self._nfc_cb_to_profiler(name) elif state & (1 << 6): # All errors have bit 6 set if state == self._nfc.STATE_REQUEST_TAG_ID_ERROR: EventLogger.info( 'No NFC/RFID Tag found! TODO: Message - Token @ Lesegereat -> Button') # TODO: Message - Token @ Lesegeraet -> Button else: EventLogger.debug('Error: ' + str(state)) # TODO check for errors in coding! #EventLogger.error(self._name + "_nfc_cb_to_profiler was None! DEBUG ONLY!") def _debug_print(self): print "self.recognition_running = " + str(self.recognition_running) + "\nself.recognition_state = " + str( self.recognition_state) + "\nself.recognition_progress = " + str(self.recognition_progress) + "\n"
class DualButtonInput: current_state = 0 current_state_counter = [0]*4 press_timer = None def __init__(self, ipcon, key_queue): if not config.UID_DUAL_BUTTON_BRICKLET[0]: print("Not Configured: Dual Button 1") if not config.UID_DUAL_BUTTON_BRICKLET[1]: print("Not Configured: Dual Button 2") self.key_queue = key_queue self.ipcon = ipcon if config.UID_DUAL_BUTTON_BRICKLET[0]: self.db1 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[0], self.ipcon) else: self.db1 = None if config.UID_DUAL_BUTTON_BRICKLET[1]: self.db2 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[1], self.ipcon) else: self.db2 = None if self.db1: try: self.db1.get_button_state() print("Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0]) except: self.db1 = None print("Not Found: Dual Button 1 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[0]) if self.db2: try: self.db2.get_button_state() print("Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1]) except: self.db2 = None print("Not Found: Dual Button 2 ({0})").format(config.UID_DUAL_BUTTON_BRICKLET[1]) if self.db1: self.db1.register_callback(self.db1.CALLBACK_STATE_CHANGED, self.cb_state_changed1) if self.db2: self.db2.register_callback(self.db2.CALLBACK_STATE_CHANGED, self.cb_state_changed2) self.press_timer = RepeatedTimer(0.1, self.press_tick) def stop(self): if self.press_timer is not None: self.press_timer.stop() def cb_state_changed1(self, button_l, button_r, led_l, led_r): l = button_l == DualButton.BUTTON_STATE_PRESSED r = button_r == DualButton.BUTTON_STATE_PRESSED state = (l << 0) | (r << 1) changed_state = (self.current_state ^ state) & 0b0011 self.current_state = state self.state_to_queue(changed_state & self.current_state) def cb_state_changed2(self, button_l, button_r, led_l, led_r): l = button_l == DualButton.BUTTON_STATE_PRESSED r = button_r == DualButton.BUTTON_STATE_PRESSED state = (l << 2) | (r << 3) changed_state = (self.current_state ^ state) & 0b1100 self.current_state = state self.state_to_queue(changed_state & self.current_state) def state_to_queue(self, state): for item in config.KEYMAP_DUAL_BUTTON.items(): if state & (1 << item[0]): self.key_queue.put(item[1]) def press_tick(self): state = 0 for i in range(4): if self.current_state & (1 << i): self.current_state_counter[i] += 1 else: self.current_state_counter[i] = 0 if self.current_state_counter[i] > 1: state |= (1 << i) self.state_to_queue(state)
class LRF: HOST = "localhost" PORT = 4223 PROG_PATH = "/home/pi/SICKRPi-Scanner/" #PROG_PATH = "/home/gus484/Programme/octomap_ibeo/" TIMER_MS = 1.0 def __init__(self): self.db = None self.is_scan = False self.led_l_blink = False self.led_r_blink = False self.run = True self.ipcon = None self.pro = None self.bat_lv = None try: # create ip connection self.ipcon = IPConnection() # register ip connection callbacks self.ipcon.register_callback(IPConnection.CALLBACK_ENUMERATE, self.cb_enumerate) self.ipcon.register_callback(IPConnection.CALLBACK_CONNECTED, self.cb_connected) self.ipcon.connect(LRF.HOST, LRF.PORT) #self.ipcon.enumerate() except: self.run = False print "no connection to tinkerforge devices!" exit(0) def release(self): if self.ipcon != None: # turn off leds self.db.set_led_state(DualButton.LED_STATE_OFF,DualButton.LED_STATE_OFF) # disconnect self.ipcon.disconnect() print "quit" self.run = False # thread: start laserscan def call_laser(self): # nicht blockierend self.pro = subprocess.Popen(LRF.PROG_PATH+"bin/main", shell=False, preexec_fn=os.setsid) # callback handles device connections and configures possibly lost # configuration of lcd and temperature callbacks, backlight etc. def cb_enumerate(self, uid, connected_uid, position, hardware_version, firmware_version, device_identifier, enumeration_type): if enumeration_type == IPConnection.ENUMERATION_TYPE_CONNECTED or enumeration_type == IPConnection.ENUMERATION_TYPE_AVAILABLE: # enumeration is for dual button bricklet if device_identifier == DualButton.DEVICE_IDENTIFIER: if self.db != None: return # create dual button device object self.db = DualButton(uid, self.ipcon) # register button push callback to function cb_state_changed self.db.register_callback(self.db.CALLBACK_STATE_CHANGED, self.cb_state_changed) # set led state self.db.set_led_state(DualButton.LED_STATE_ON, DualButton.LED_STATE_OFF) Timer(LRF.TIMER_MS, self.led_blink).start() # callback handles reconnection of ip connection def cb_connected(self, connected_reason): self.ipcon.enumerate() # callback function for interrupts def cb_state_changed(self, button_l, button_r, led_l, led_r): # check for both interrupts if button_l == DualButton.BUTTON_STATE_PRESSED and button_r == DualButton.BUTTON_STATE_PRESSED: print "stop script" #if self.is_scan: execfile(LRF.PROG_PATH+"quit.py") time.sleep(0.5) try: print "terminate process" #os.killpg(self.pro.pid, signal.SIGTERM) self.pro.terminate() except: print "..." self.run = False # check start interrupt elif button_l == DualButton.BUTTON_STATE_PRESSED: if self.is_scan == False: print "start scan" self.led_l_blink = False self.db.set_selected_led_state(DualButton.LED_RIGHT,DualButton.LED_STATE_ON) self.is_scan = True # start laser thread start_new_thread(self.call_laser,()) # check stop interrupt elif button_r == DualButton.BUTTON_STATE_PRESSED: if self.is_scan: print "stop scan" self.led_l_blink = False self.db.set_selected_led_state(DualButton.LED_RIGHT,DualButton.LED_STATE_OFF) execfile(LRF.PROG_PATH+"quit.py") self.is_scan = False def led_blink(self): if self.db != None and self.run == True: # get led state ls = self.db.get_led_state() if self.led_l_blink: if ls[0] == DualButton.LED_STATE_ON: l = DualButton.LED_STATE_OFF else: l = DualButton.LED_STATE_ON # set led state self.db.set_selected_led_state(DualButton.LED_LEFT,l) if self.led_r_blink: if ls[1] == DualButton.LED_STATE_ON: l = DualButton.LED_STATE_OFF else: l = DualButton.LED_STATE_ON # set led state self.db.set_selected_led_state(DualButton.LED_RIGHT,l) if self.run == True: Timer(LRF.TIMER_MS, self.led_blink).start() def check_battery_level(self): try: #open serial dev s = serial.Serial() s.port = "/dev/ttyAMA0" s.baudrate = 38400 s.timeout = 0.15 s.open() if not s.isOpen(): return s.write("@EPR"+'\r\n') #s.write("@USB"+'\r\n') if s.readable(): msg = s.read(1000) msg = msg.split(":") msg = msg[1].split("V") self.bat_lv = float(msg[0]) print "Voltage:" + str(self.bat_lv) s.close() except: self.bat_lv = None
class DualButtonInput: current_state = 0 current_state_counter = [0] * 4 press_timer = None def __init__(self, ipcon, key_queue): if not config.UID_DUAL_BUTTON_BRICKLET[0]: print("Not Configured: Dual Button 1") if not config.UID_DUAL_BUTTON_BRICKLET[1]: print("Not Configured: Dual Button 2") self.key_queue = key_queue self.ipcon = ipcon if config.UID_DUAL_BUTTON_BRICKLET[0]: self.db1 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[0], self.ipcon) else: self.db1 = None if config.UID_DUAL_BUTTON_BRICKLET[1]: self.db2 = DualButton(config.UID_DUAL_BUTTON_BRICKLET[1], self.ipcon) else: self.db2 = None if self.db1: try: self.db1.get_button_state() print("Found: Dual Button 1 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[0]) except: self.db1 = None print("Not Found: Dual Button 1 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[0]) if self.db2: try: self.db2.get_button_state() print("Found: Dual Button 2 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[1]) except: self.db2 = None print("Not Found: Dual Button 2 ({0})").format( config.UID_DUAL_BUTTON_BRICKLET[1]) if self.db1: self.db1.register_callback(self.db1.CALLBACK_STATE_CHANGED, self.cb_state_changed1) if self.db2: self.db2.register_callback(self.db2.CALLBACK_STATE_CHANGED, self.cb_state_changed2) self.press_timer = RepeatedTimer(0.1, self.press_tick) def stop(self): if self.press_timer is not None: self.press_timer.stop() def cb_state_changed1(self, button_l, button_r, led_l, led_r): l = button_l == DualButton.BUTTON_STATE_PRESSED r = button_r == DualButton.BUTTON_STATE_PRESSED state = (l << 0) | (r << 1) changed_state = (self.current_state ^ state) & 0b0011 self.current_state = state self.state_to_queue(changed_state & self.current_state) def cb_state_changed2(self, button_l, button_r, led_l, led_r): l = button_l == DualButton.BUTTON_STATE_PRESSED r = button_r == DualButton.BUTTON_STATE_PRESSED state = (l << 2) | (r << 3) changed_state = (self.current_state ^ state) & 0b1100 self.current_state = state self.state_to_queue(changed_state & self.current_state) def state_to_queue(self, state): for item in config.KEYMAP_DUAL_BUTTON.items(): if state & (1 << item[0]): self.key_queue.put(item[1]) def press_tick(self): state = 0 for i in range(4): if self.current_state & (1 << i): self.current_state_counter[i] += 1 else: self.current_state_counter[i] = 0 if self.current_state_counter[i] > 1: state |= (1 << i) self.state_to_queue(state)