def addNeutralValues(self, startTime, endTime): if len(self.recordedRotations) > 0: if self.recordedRotations[0].getTime() != startTime: newEvent = RotationEvent(startTime, Decimal('0'), Decimal('0')) self.recordedRotations.insert(0, newEvent) if self.recordedRotations[len(self.recordedRotations) - 1].getTime() != endTime: lastEvent = self.recordedRotations[len(self.recordedRotations) - 1] newEvent = RotationEvent(endTime, (Decimal('-1') * lastEvent.getValue()).normalize(), Decimal('0')) self.recordedRotations.append(newEvent) if len(self.recordedButtons) > 0: if self.recordedButtons[0].getTime() != startTime: newEvent = ButtonEvent(startTime, Decimal('0')) self.recordedButtons.insert(0, newEvent) if self.recordedButtons[len(self.recordedButtons) - 1].getTime() != endTime: newEvent = ButtonEvent(endTime, Decimal('0')) self.recordedButtons.append(newEvent) for location in self.touches: touchEvents = self.touches[location] if touchEvents[0].getTime() != startTime: newEvent = TouchEvent(startTime, location, Decimal('0')) touchEvents.insert(0, newEvent) if touchEvents[len(touchEvents) - 1].getTime() != endTime: newEvent = TouchEvent(endTime, location, Decimal('0')) touchEvents.append(newEvent)
def addNeutralValues(self, startTime, endTime): #Add startTime and endTime if not present #Should be able to workaround this with adapted calculations if len(self.recordedRotations) > 0: if self.recordedRotations[0].getTime() != startTime: newEvent = RotationEvent(startTime, Decimal('0'), Decimal('0')) self.recordedRotations.insert(0, newEvent) if self.recordedRotations[len(self.recordedRotations) - 1].getTime() != endTime: lastEvent = self.recordedRotations[len(self.recordedRotations) - 1] newEvent = RotationEvent(endTime, (Decimal('-1') * lastEvent.getValue()).normalize(), Decimal('0')) self.recordedRotations.append(newEvent) else: newEvent = RotationEvent(startTime, Decimal('0'), Decimal('0')) self.recordedRotations.append(newEvent) newEvent = RotationEvent(endTime, Decimal('0'), Decimal('0')) self.recordedRotations.append(newEvent) if len(self.recordedButtons) > 0: if self.recordedButtons[0].getTime() != startTime: newEvent = ButtonEvent(startTime, Decimal('0')) self.recordedButtons.insert(0, newEvent) if self.recordedButtons[len(self.recordedButtons) - 1].getTime() != endTime: newEvent = ButtonEvent(endTime, Decimal('0')) self.recordedButtons.append(newEvent) else: newEvent = ButtonEvent(startTime, Decimal('0')) self.recordedButtons.append(newEvent) newEvent = ButtonEvent(endTime, Decimal('0')) self.recordedButtons.append(newEvent) if len(self.recordedTouches) > 0: if self.recordedTouches[0].getTime() != startTime: newEvent = TouchEvent(startTime, None, Decimal('0')) self.recordedTouches.insert(0, newEvent) if self.recordedTouches[len(self.recordedTouches) - 1].getTime() != endTime: newEvent = TouchEvent(endTime, None, Decimal('0')) self.recordedTouches.append(newEvent) else: newEvent = TouchEvent(startTime, None, Decimal('0')) self.recordedTouches.append(newEvent) newEvent = TouchEvent(endTime, None, Decimal('0')) self.recordedTouches.append(newEvent)
def startListening(self): self.sum = 0 while True: self.checkSharedMemory() pollResult = self.knob.read_event(0) if pollResult is None: continue (time, event, val) = pollResult time = Decimal('{}'.format(time)).normalize() val = Decimal('{}'.format(val)).normalize() if event == EVENT_ROTATE: self.sum = Decimal(self.sum) + val event = RotationEvent(time, val, self.sum) elif event == EVENT_BUTTON: event = ButtonEvent(time, val) self.bareSignalsLock.acquire() self.bareSignals.append(event) self.bareSignalsLock.release() self.cleanUp()
def startListening(self): tapTimeStamp = None timeout = 0.5 self.sum = 0 while True: self.checkSharedMemory() pollResult = self.knob.read_event(0) if pollResult is None: continue (time, event, val) = pollResult time = Decimal('{}'.format(time)).normalize() val = Decimal('{}'.format(val)).normalize() if event == EVENT_ROTATE: self.sum = Decimal(self.sum) + val event = RotationEvent(time, val, self.sum) elif event == EVENT_BUTTON: event = ButtonEvent(time, val) #Check For Double Tap if event.getEvent() == EVENT_BUTTON and event.getValue( ) == Decimal('0'): if tapTimeStamp != None and (event.getTime() - tapTimeStamp) <= Decimal( '{}'.format(timeout)): pass tapTimeStamp = event.getTime() self.signalsLock.acquire() self.signals.append(event) self.signalsLock.release() self.cleanUp()
del self.bareSignals[:] self.bareSignalsCounter = 0 self.bareSignalsLock.release() def cleanUp(self): print('Verfication finished') ### #Bachelorarbeit if __name__ == '__main__': bareSignalsLock = threading.Lock() signalsLock = threading.Lock() bareSignals = [] signals = [] v = Verification(bareSignals, bareSignalsLock, signals, signalsLock) v.start() e1 = ButtonEvent(0, 1) e2 = ButtonEvent(1.8, 0) e3 = ButtonEvent(3.5, 1) e4 = ButtonEvent(3.7, 1) bareSignalsLock.acquire() bareSignals.append(e1) bareSignals.append(e2) bareSignals.append(e3) bareSignals.append(e4) bareSignalsLock.release() ###
def transformMotion(self, signals): self.signals = signals interpolator = Interpolator() n = 64 startTime = None endTime = None for event in self.signals: if startTime == None: startTime = event.getTime() elif startTime > event.getTime(): startTime = event.getTime() if endTime == None: endTime = event.getTime() elif endTime < event.getTime(): endTime = event.getTime() if isinstance(event, RotationEvent): self.recordedRotations.append(event) elif isinstance(event, ButtonEvent): self.recordedButtons.append(event) elif isinstance(event, TouchEvent): self.recordedTouches.append(event) else: pass if startTime is None or endTime is None: raise NotEnoughSignals() #Construct Motion transformedMotion = Motion() transformedMotion.setStartTime(startTime) transformedMotion.setEndTime(endTime) self.addNeutralValues(startTime, endTime) #Rotation Part of Motion result = interpolator.linearInterpolation(self.recordedRotations, n) transformedTime = result[0] transformedSum = result[1] for i in range(len(transformedTime)): event = RotationEvent(transformedTime[i], None, transformedSum[i]) transformedMotion.addEvent(event) #Button Part of Motion result = interpolator.linearInterpolation(self.recordedButtons, n) transformedTime = result[0] transformedValue = result[1] for i in range(len(transformedTime)): event = ButtonEvent(transformedTime[i], transformedValue[i]) transformedMotion.addEvent(event) #Touch Part of Motion touchEventDictionary = self.sortTouches() for touchLocation in touchEventDictionary: result = interpolator.linearInterpolation(touchLocation[1], n) transformedTime = result[0] transformedValue = result[1] for i in range(len(transformedTime)): event = TouchEvent(transformedTime[i], touchLocation[0], transformedValue[i]) transformedMotion.addEvent(event) #Scaling and adjustment self.scaleMotion(transformedMotion) self.adjustValues(transformedMotion) return transformedMotion
def transformMotion(self, signals): self.signals = signals interpolator = Interpolator() n = 64 startTime = None endTime = None for event in self.signals: if startTime == None: startTime = event.getTime() elif startTime > event.getTime(): startTime = event.getTime() if endTime == None: endTime = event.getTime() elif endTime < event.getTime(): endTime = event.getTime() if isinstance(event, RotationEvent): self.recordedRotations.append(event) elif isinstance(event, ButtonEvent): self.recordedButtons.append(event) elif isinstance(event, TouchEvent): #self.recordedTouches.append(event) if event.getLocation is None: raise NameError('Hier ist ein Fehler aufgetreten') #Sort Touches by Locations if event.getLocation() not in self.touches: self.touches[event.getLocation()] = [] self.touches[event.getLocation()].append(event) else: pass if startTime is None or endTime is None: raise NotEnoughSignals() #Construct Motion transformedMotion = Motion() transformedMotion.setStartTime(startTime) transformedMotion.setEndTime(endTime) self.addNeutralValues(startTime, endTime) #print('ROTATIONS') #print(self.recordedRotations) #print('BUTTONS') #print(self.recordedButtons) #print('TOUCHES ARRAY') #print(self.recordedTouches) #print('TOUCHES DIC') #print(self.touches) #return #Rotation Part of Motion if len(self.recordedRotations) > 0: result = interpolator.linearInterpolation(self.recordedRotations, n) transformedTime = result[0] transformedSum = result[1] for i in range(len(transformedTime)): event = RotationEvent(transformedTime[i], None, transformedSum[i]) transformedMotion.addEvent(event) else: print('Es wurde keine Rotation hinzugefügt') #Button Part of Motion if len(self.recordedButtons) > 0: result = interpolator.linearInterpolation(self.recordedButtons, n) transformedTime = result[0] transformedValue = result[1] for i in range(len(transformedTime)): event = ButtonEvent(transformedTime[i], transformedValue[i]) transformedMotion.addEvent(event) else: print('Es wurde kein Button hinzugefügt') #Touch Part of Motion for location in self.touches: touchEvents = self.touches[location] result = interpolator.linearInterpolation(touchEvents, n) transformedTime = result[0] transformedValue = result[1] for i in range(len(transformedTime)): event = TouchEvent(transformedTime[i], location, transformedValue[i]) transformedMotion.addEvent(event) #Scaling and adjustment self.scaleMotion(transformedMotion) self.adjustValues(transformedMotion) return transformedMotion
def getMotion(self, path): deviceManager = DeviceManager() f = open(path, 'r') motion = Motion() for line in f: if line[:len("Name")] == "Name": motion.setName(line[len("Name:"):line.find(";")]) continue elif line[:len("Device")] == "Device": deviceName = line[len("Device:"):line.find(";")] if deviceName == 'None': continue device = deviceManager.getDevice(deviceName) motion.assignDevice(device) continue elif line[:len("Function")] == "Function": functionName = line[len("Function:"):line.find(";")] if functionName == 'None': continue device = motion.getAssignedDevice() function = getattr(device, functionName) motion.assignFunction(function) continue time = line[line.find("Time:") + len("Time:"):line.find(";")] time = Decimal(time) line = line[line.find(";") + 1:] event = line[line.find("Event:") + len("Event:"):line.find(";")] line = line[line.find(";") + 1:] location = line[line.find("Location:") + len("Location:"):line.find(";")] line = line[line.find(";") + 1:] if location == 'None': location = None value = line[line.find("Value:") + len("Value:"):line.find(";")] if value == 'None': value = None else: value = Decimal(value) line = line[line.find(";") + 1:] sum = line[line.find("Sum:") + len("Sum:"):line.find(";")] if sum == 'None': sum = None else: sum = Decimal(sum) line = line[line.find(";"):] if event == EVENT_BASE: event = BaseEvent(time) elif event == EVENT_ROTATE: event = RotationEvent(time, value, sum) elif event == EVENT_BUTTON: event = ButtonEvent(time, value) elif event == EVENT_TOUCH: event = TouchEvent(time, location, value) motion.addEvent(event) return motion