Created
February 27, 2016 18:51
-
-
Save Sorseg/0d6302c164eacfbb3e5e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
from droidhelper import DroidHelper | |
import socket | |
import struct | |
import time | |
SMOOTH = 2 | |
COEFF = 10000/20.0/SMOOTH | |
readings = [0] * SMOOTH | |
ind = 0 | |
if __name__ == '__main__': | |
s = socket.socket() | |
print("connecting") | |
s.connect(('192.168.1.35', 8888)) | |
print("connected") | |
with DroidHelper() as dh: | |
time.sleep(0.5) | |
while True: | |
time.sleep(0.006) | |
sensor = dh.droid.sensorsReadAccelerometer().result[1] | |
readings[ind] = sensor | |
ind = (ind + 1) % SMOOTH | |
result = int(max(sum(readings)*COEFF, 0)) | |
if not ind: | |
print result | |
s.send(struct.pack('h', result)) |
This file contains hidden or 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
from machine import Timer | |
import socket | |
import struct | |
def listen(): | |
tim = Timer(1, mode=Timer.PWM, width=16) | |
tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=5055) | |
s = socket.socket() | |
try: | |
s.bind(('0.0.0.0', 8888)) | |
s.listen(1) | |
c, a = s.accept() | |
while True: | |
in_ = struct.unpack('h', c.read(2))[0] | |
tim_a.duty_cycle(int(in_)) | |
finally: | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment