Created
November 29, 2017 06:42
-
-
Save samhains/a149b8011280cb4f4e6b6451cd070d97 to your computer and use it in GitHub Desktop.
code for parsing xbee protocol in python with touch designer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# dat - the DAT that received the data | |
# rowIndex - the row number the data was placed into | |
# message - an ascii representation of the data | |
# Unprintable characters and unicode characters will | |
# not be preserved. Use the 'bytes' parameter to get | |
# the raw bytes that were sent. | |
# bytes - byte array of the data received | |
import binascii | |
incoming = [] | |
def parse_packet(which_packet): | |
adc_start = 11 | |
num_samples = which_packet[8] | |
total = 0 | |
j = 0 | |
address = which_packet[5] + which_packet[4] * 256 | |
for i in range(0, num_samples): | |
j = i + 2 | |
high_value = which_packet[j+adc_start] | |
low_value = which_packet[(j + 1) + adc_start] | |
print("high value ", high_value) | |
print("low value ", low_value) | |
this_sample = (high_value * 256) + low_value | |
total = total + this_sample | |
average = total / num_samples | |
print("average", average) | |
return average | |
def onReceive(dat, rowIndex, message, bytes): | |
value = ord(bytes) | |
if (value == 0x7E): | |
if (len(incoming) == 22): | |
parse_packet(incoming) | |
incoming.clear() | |
incoming.append(value) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment