CSP on pyboard

Lately i worked a lot with clojure and core.async, and i was very impressed with CSP. Mostly because it saves me from js callback hell. For example, js code with sequential http requests (heh, with promises it’s less ugly):

    return $http.post('/user-data/', data.items);
    return $http.get('/posts/');

With clojurescript and core.async it will be:

(go (let [users (<! (http/get "/users/"))]
       (<! (http/post "/user-data/" (:items users)))
       (js/console.log (<! (http/get "/posts/")))))

Clojurescript code looks more readable and simple.

And i developed CSP for micropython in my microasync library.

Example application — servo should rotate to angle which equal to X angle of accelerometer, and user can start/stop app with button:

from microasync.device import get_servo, get_accel, get_switch
from microasync.async import coroutine, loop, Delay, select

def servo_coroutine():
    servo, _ = get_servo(1)  # get_servo returns set and get channels
    accel = get_accel()
    switch = get_switch()
    on = False
    x = 0
    accel_or_switch = select(switch, accel)  # like select from go and like clojure core.async alts!
    while True:
        chan, val = yield accel_or_switch.get()  # like clojure (<! (accel_or_switch))
        if chan == accel:
            x, *_ = val  # we don't need y and z
        elif chan == switch:
            on = not on

        if on:
            yield servo.put(x)  # like clojure (>! servo x)
        yield Delay(0)  # like clojure (<! (timeout 0))


And recorded on google glass (yep, i bought it few days ago) video of result:

comments powered by Disqus