diff --git a/examples/adis16480_doodlejump.py b/examples/adis16480_doodlejump.py new file mode 100644 index 000000000..c47ab2bc0 --- /dev/null +++ b/examples/adis16480_doodlejump.py @@ -0,0 +1,80 @@ +# This demo application converts adis16480's accel channel values into keyboard inputs +# great for controlling existing games + +import adi #pyadi-iio +import keyboard #pip install keyboard + +dev = adi.adis16480(uri='ip:172.27.6.191') + +dev.rx_output_type = "raw" +dev.rx_enabled_channels = [3, 4, 5] +dev.sample_rate = 20000 +dev.rx_buffer_size = 100 + +print("Product id: " + str(dev.product_id)) +print("Serial number: " + dev.serial_number) +print("Firmware revision: " + dev.firmware_revision) +print("Firmware date: " + dev.firmware_date) +print("\nSampling frequency: " + str(dev.sample_rate)) + + +print("\nX acceleration: " + str(dev.accel_x_conv) + " m/s^2") +print("Y acceleration: " + str(dev.accel_y_conv) + " m/s^2") +print("Z acceleration: " + str(dev.accel_z_conv) + " m/s^2") + +# limits +directions = { + "center" : (-1.9,1.9), + "soft_right" : (-6.9,-2), + "hard_right" : (-15,-7), + "soft_left" : (2,6.9), + "hard_left" : (7,15), +} + +# directions +actions=[ + "center", + "soft_right", + "soft_left", + "hard_right", + "hard_left", +] + +def move_x(action): + if action in ["hard_left", "soft_left"]: + # keyboard.release('down') + print("left") + keyboard.release('right') + keyboard.press('left') + elif action in ["hard_right", "soft_right"]: + # keyboard.release('up') + print("right") + keyboard.release('left') + keyboard.press('right') + else: + print("center") + keyboard.release('right') + keyboard.release('left') + +def move_y(action): + if action in ["hard_left", "soft_left"]: + # keyboard.release('down') + print("down") + keyboard.press('down') + elif action in ["hard_right", "soft_right"]: + # keyboard.release('up') + print("up") + keyboard.press('up') + else: + print("center") + keyboard.release('down') + keyboard.release('up') + +while(True): + for action in actions: + ll,ul = directions[action] + if ll <= dev.accel_x_conv <= ul: + move_x(action) + if ll <= dev.accel_y_conv <= ul: + move_y(action) + diff --git a/examples/adis16480_subwaysurfers.py b/examples/adis16480_subwaysurfers.py new file mode 100644 index 000000000..86df22ef9 --- /dev/null +++ b/examples/adis16480_subwaysurfers.py @@ -0,0 +1,77 @@ +# This demo application converts adis16480's accel channel values into keyboard inputs +# great for controlling existing games + +import adi #pyadi-iio +import keyboard #pip install keyboard +from collections import Counter +import time + +dev = adi.adis16480(uri='ip:172.27.6.191') + +dev.rx_output_type = "raw" +dev.rx_enabled_channels = [3, 4, 5] +dev.sample_rate = 20000 +dev.rx_buffer_size = 100 + +print("Product id: " + str(dev.product_id)) +print("Serial number: " + dev.serial_number) +print("Firmware revision: " + dev.firmware_revision) +print("Firmware date: " + dev.firmware_date) +print("\nSampling frequency: " + str(dev.sample_rate)) + + +print("\nX acceleration: " + str(dev.accel_x_conv) + " m/s^2") +print("Y acceleration: " + str(dev.accel_y_conv) + " m/s^2") +print("Z acceleration: " + str(dev.accel_z_conv) + " m/s^2") + +# limits +directions = { + "center" : (-1.9,1.9), + "soft_right" : (-6.9,-2), + "hard_right" : (-15,-7), + "soft_left" : (2,6.9), + "hard_left" : (7,15), +} + +# directions +actions=[ + "center", + "soft_right", + "soft_left", + "hard_right", + "hard_left", +] + +recent_actions = [] + +def move_x(action): + if action in ["hard_left", "soft_left"]: + recent_actions.append('left') + elif action in ["hard_right", "soft_right"]: + recent_actions.append('right') + +def move_y(action): + if action in ["hard_left", "soft_left"]: + recent_actions.append('up') + elif action in ["hard_right", "soft_right"]: + recent_actions.append('down') + +while(True): + for action in actions: + ll,ul = directions[action] + if ll <= dev.accel_x_conv <= ul: + move_x(action) + if ll <= dev.accel_y_conv <= ul: + move_y(action) + if len(recent_actions) > 2: + counter = Counter(recent_actions) + key, count = counter.most_common(1)[0] + # if len(recent_actions) > 0: + key = recent_actions[0] + keyboard.press(key) + keyboard.release(key) + print(key) + # time.sleep(0.05) + recent_actions = [] + + diff --git a/requirements_demo.txt b/requirements_demo.txt new file mode 100644 index 000000000..fbfe72433 --- /dev/null +++ b/requirements_demo.txt @@ -0,0 +1 @@ +keyboard \ No newline at end of file