Exemplo n.º 1
0
 def __init__(self, driver):
     self.driver = driver
     self.evm = event.EventMachine(self.driver)
     self.evm.registerCallback(self)
     self.networks = []
     self.channels = []
     self.running = False
     self.options = [0x00, 0x00, 0x00]
Exemplo n.º 2
0
    def open(self):
        #Setup the logger:
        self.logger = LogWriter(filename=self.outfile, runName=self.runName)
        # Initialize driver
        self.stick = driver.USB1Driver(self.serial,
                                       log=self.logger,
                                       debug=True,
                                       baud_rate=4800)
        self.stick.open()

        # Initialize event machine
        self.evm = event.EventMachine(self.stick)
        self.evm.registerCallback(MyCallback())
        self.evm.start()

        # Reset
        logging.debug("\n\n-------------------------------\n:")
        logging.debug("Setting UP")
        logging.debug("\n\n-------------------------------\n")
        msg = SystemResetMessage()
        self.stick.write(msg.encode())
        time.sleep(1)

        # Set network key
        msg = NetworkKeyMessage(key=NETKEY)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: SETTING NETWORK KEY')
        #            sys.exit()

        # Initialize it as a receiving channel using our network key
        msg = ChannelAssignMessage()
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: INITIALISING AS RECEIVING CHANNEL')
        #            sys.exit()

        # Now set the channel id for pairing with an ANT+ bike cadence/speed sensor
        msg = ChannelIDMessage(device_type=121)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: SETTING CHANNEL ID ')
        #            sys.exit()

        # Listen forever and ever (not really, but for a long time)
        msg = ChannelSearchTimeoutMessage(timeout=255)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UPL LISTENING TIMEOUT')
        #            sys.exit()

        # We want a ~4.05 Hz transmission period
        msg = ChannelPeriodMessage(period=8085)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: TRANSMISSION FREQUENCY')
        #            sys.exit()

        # And ANT frequency 57, of course
        msg = ChannelFrequencyMessage(frequency=57)
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: SETTING FREQUENCY')
        #            sys.exit()

        # Time to go live
        msg = ChannelOpenMessage()
        self.stick.write(msg.encode())
        sleep(1)
        #        if self.evm.waitForAck(msg) != RESPONSE_NO_ERROR:
        #            logging.debug( 'ERROR SETTING UP: GOING LIVE')
        #            sys.exit()

        logging.debug("\n\n-------------------------------\n:")
        logging.debug("Listening for ANT events: Press CTRL+C to Exit.")
        logging.debug("\n\n-------------------------------\n")
NETKEY = '\xB9\xA5\x21\xFB\xBD\x72\xC3\x45'


# Event callback
class MyCallback(event.EventCallback):
    def process(self, msg):
        print msg


# Initialize driver
stick = driver.USB2Driver(SERIAL, log=LOG, debug=DEBUG)
stick.open()

# Initialize event machine
evm = event.EventMachine(stick)
evm.registerCallback(MyCallback())
evm.start()

# Reset
msg = SystemResetMessage()
stick.write(msg.encode())
time.sleep(1)

# Set network key
msg = NetworkKeyMessage(key=NETKEY)
stick.write(msg.encode())
if evm.waitForAck(msg) != RESPONSE_NO_ERROR:
    sys.exit()

# Initialize it as a receiving channel using our network key
Exemplo n.º 4
0
 def __init__(self, driver, name=None):
     self.evm = event.EventMachine(driver)
     self.name = name
     self.networks = []
     self.channels = []
     self.options = [0x00, 0x00, 0x00]