示例#1
0
def main():
    data_sent = 0
    state = GET_DATA_NUM
    while True:
        # State 0
        if state == GET_DATA_NUM:
            data_num = 1
            attempt = 0
            while True:
                cy, cx = divmod(data_num-1, 5)                          # Cursor x and y depending on the data_num
                display.set_pixel(cx, cy, 9)
                if button_a.is_pressed() and button_b.is_pressed():
                    state = READY_TO_SAMPLE                                        # TODO: Change state to some other state
                    data_sent = 0
                    sleep(500)
                    break
                elif button_a.is_pressed():
                    if data_num > 1:
                        display.set_pixel(cx, cy, 0)                                # Clear LED pixel if data_num > 1
                    data_num = data_num - 1 if (data_num > 1) else 1
                elif button_b.is_pressed():
                    data_num = data_num + 1 if (data_num < MAX_FILES_NUM) else MAX_FILES_NUM
                sleep(200)
        # State 1
        elif state == READY_TO_SAMPLE:
            while True:
                if button_a.is_pressed():
                    state = SAMPLE_DATA
                    break
                elif button_b.is_pressed():
                    display.clear()
                    cy, cx = divmod(data_num-data_sent-1, 5)
                    setPixelTill(cx, cy, 9)
                else:
                    display.show(Image.ARROW_W)
                sleep(200)
        # State 2
        elif state == SAMPLE_DATA:
            countdown(3)
            display.show(Image.TARGET)
            with open("file_{}.csv".format(data_sent), "w") as data_file:
                data_file.write("x,y,z\n")
                initial_time = running_time()
                while (running_time()-initial_time) < SAMPLE_DURATION:
                    t0 = running_time()
                    data_file.write("{},{},{}\n".format(*accelerometer.get_values()))
                    t_diff = running_time()-t0
                    sleep(0 if (SAMPLE_PERIOD-t_diff)<0 else SAMPLE_PERIOD-t_diff)
            data_sent += 1
            if (data_num-data_sent)>0:
                state = READY_TO_SAMPLE
            else:
                state = EXIT
        # State 3
        elif state == EXIT:
            display.show(Image.HAPPY)
            break
示例#2
0
 def get_data():
     x, y, z = accelerometer.get_values()
     return Data(
         Acc_X=x,
         Acc_Y=y,
         Acc_Z=z,
         # Acc_gesture=accelerometer.current_gesture(),
         Mag_X=compass.get_x(),
         Mag_Y=compass.get_x(),
         Mag_Z=compass.get_x(),
         Mag_heading=compass.heading())
示例#3
0
文件: memory.py 项目: MoMaT/slides
def read_move():
    x, y, z = accelerometer.get_values()
    if x > 500:
        return "right"
    if x < -500:
        return "left"
    if y > 800:
        return "down"
    if y < -300:
        return "up"
    return None
示例#4
0
def txp(did,ty,v):
    if ty==1:
        txt = "1,1,%i,%i" %(ms,cs)
    elif ty==2:
        txt = "1,2,%i,%i,%s" %(ms,cs,v)
    elif ty==3:
        #sensing_str = "%i,%i,%i,%i,%i,%i" % (int(pin_logo.is_touched()),display.read_light_level(),a_x,a_y,a_z,compass.heading())
        a_x,a_y,a_z = accelerometer.get_values()
        sensing_str = "%i,%i,%i,%i,%i,%i" % (int(pin_logo.is_touched()),di.read_light_level(),microphone.sound_level(),a_x,a_y,a_z)
        txt = "1,3,%i,%i,%i,%s" %(ms,cs,v, sensing_str)

    elif ty==20:
        txt = "1,20,%s" %(v)
    else:
        txt = "1,%i,%s" %(ty,v)
    tx(did,txt)
示例#5
0
def main():
    state = READY
    while True:
        # Ready state
        if state == READY:
            while True:
                if button_a.is_pressed():
                    state = SAMPLE_DATA
                    break
                elif button_b.is_pressed():
                    radio.on()
                    radio.send("done")
                    radio.off()
                else:
                    display.show(Image.ARROW_W)
                sleep(100)
        # Sample Data state
        elif state == SAMPLE_DATA:
            data_sent = 0  # Reset data sent value
            countdown(3)  # Show countdown on the Microbit LED
            display.show(Image.TARGET)
            radio.on()
            initial_time = running_time()
            while (running_time() - initial_time) < SAMPLE_DURATION:
                t0 = running_time()
                if data_sent == 0:  # Turn off all Microbit LEDs
                    display.clear()
                cx, cy = divmod(
                    data_sent,
                    5)  # Get current LED pixel coordinate of the BBC Microbit
                radio.send(str(accelerometer.get_values()))
                display.set_pixel(4 - cx, cy, 9)
                data_sent = 0 if data_sent >= 24 else data_sent + 1  # Increase and limit data_sent value within 0-24 range
                wait_t = SAMPLE_INTERVAL - (running_time() - t0
                                            )  # Time till next sample
                if (wait_t > 0):
                    sleep(wait_t)
            radio.send("done")
            radio.off()
            state = READY
        # Exit state
        elif state == EXIT:
            display.show(Image.HAPPY)
            return 0
        sleep(100)
示例#6
0
def get_n_values(frequency):
    global n_mag_sum, n_v
    vals = (accelerometer.get_values())
    print(vals)
    for w in range(3):
        if vals[w] > n_v[w]: n_v[w] = vals[w]
        elif vals[w] < n_v[w + 3]: n_v[w + 3] = vals[w]
    n_v[6] += vals[0]
    n_v[7] += vals[1]
    n_v[8] += vals[2]
    n_mag_sum += (int((vals[0])**2 + (vals[1])**2 + (vals[2])**2)**.5)
    if frequency == 1:
        global n_f
        for q in range(3):
            if (vals[q] - n_f[0][q]) * (n_f[2][q] - n_f[0][q]) < 0:
                n_f[1][q] += 1
        n_f[2][0], n_f[2][1], n_f[2][2] = vals[0], vals[1], vals[2]
    sleep(50)
示例#7
0
def rccar_tx(did):
    global px_cur, py_cur
    [x, y, z] = accelerometer.get_values()
    px = mymap(x, -1000, 1000, 0, 5)
    py = mymap(y, -1000, 1000, 0, 5)
    #print("(%i,%i,%i,%i,%i)"%(x,y,z,px,py))

    if px_cur != px or py_cur != py:
        di.set_pixel(px_cur, py_cur, 0)
    di.set_pixel(px, py, 9)
    px_cur = px
    py_cur = py

    px_cmd = mymap(x, -1000, 1000, 0, 100)
    py_cmd = mymap(y, -1000, 1000, 0, 100)

    cmd = px_cmd * 100 + py_cmd
    txp(did, 23, str(cmd))
def get_n_values(frequency):
    global n_mag_sum, n_v
    vals = (accelerometer.get_values())
    #print(accelerometer.get_values()) #uncomment this line to see plot on mu editor
    for w in range(3):  #gets min max for xyz
        if vals[w] > n_v[w]: n_v[w] = vals[w]  #maxes
        elif vals[w] < n_v[w + 3]: n_v[w + 3] = vals[w]  #mins
    n_v[6] += vals[0]  #xsum
    n_v[7] += vals[1]  #ysum
    n_v[8] += vals[2]  #zsum
    n_mag_sum += (int((vals[0])**2 + (vals[1])**2 + (vals[2])**2)**.5)
    if frequency == 1:
        global n_f  #x, y, z accross top. avg, frq, old at bottom
        for q in range(3):
            if (vals[q] - n_f[0][q]) * (n_f[2][q] -
                                        n_f[0][q]) < 0:  #x, y, z frq
                n_f[1][q] += 1
        n_f[2][0], n_f[2][1], n_f[2][2] = vals[0], vals[1], vals[2]
    sleep(50)
示例#9
0
# Shove accelerometer data through the uart.
from microbit import accelerometer, sleep, uart, pin0, pin1
uart.init(rx=pin0, tx=pin1)

while True:
    uart.write(
        bytes(','.join([str(v)
                        for v in accelerometer.get_values()]), 'ascii') + '\n')
    sleep(20000)
示例#10
0
from microbit import accelerometer as acc, sleep

tx = 10
ty = 10
tz = 40
x = y = z = 0

while True:
    nx, ny, nz = acc.get_values()

    if abs(nx - x) >= tx or abs(ny - y) >= ty or abs(nz - z) >= tz:
        x, y, z = nx, ny, nz
        print(x, y, z)

    sleep(50)
示例#11
0
def get_tilt():
    x, y, z = accelerometer.get_values()
    return tilt_scale(x), tilt_scale(y), tilt_scale(z)
示例#12
0
def main():
    display.show(Image.HAPPY)
    up_pressed, down_pressed, left_pressed, right_pressed = False, False, False, False

    while True:
        # Only send arrow commands when Button a is pressed
        if button_a.is_pressed():
            # Hold down or release arrow keys based on accelerometer values
            x, y, z = accelerometer.get_values()
            if x > 300:
                if not right_pressed:
                    keyboard.key_down("right")
                right_pressed = True
            else:
                if right_pressed:
                    keyboard.key_up("right")
                right_pressed = False
            if x < -300:
                if not left_pressed:
                    keyboard.key_down("left")
                left_pressed = True
            else:
                if left_pressed:
                    keyboard.key_up("left")
                left_pressed = False
            if y < -300:
                if not up_pressed:
                    keyboard.key_down("up")
                up_pressed = True
            else:
                if up_pressed:
                    keyboard.key_up("up")
                up_pressed = False
            if y > 300:
                if not down_pressed:
                    keyboard.key_down("down")
                down_pressed = True
            else:
                if down_pressed:
                    keyboard.key_up("down")
                down_pressed = False

            # Display direction on matrix
            img = Image.CHESSBOARD
            if up_pressed:
                if left_pressed:
                    img = Image.ARROW_NW
                elif right_pressed:
                    img = Image.ARROW_NE
                else:
                    img = Image.ARROW_N
            elif down_pressed:
                if left_pressed:
                    img = Image.ARROW_SW
                elif right_pressed:
                    img = Image.ARROW_SE
                else:
                    img = Image.ARROW_S
            elif left_pressed:
                img = Image.ARROW_W
            elif right_pressed:
                img = Image.ARROW_E
            display.show(img, delay=50, wait=False)
        else:
            if up_pressed:
                keyboard.key_up("up")
            if down_pressed:
                keyboard.key_up("down")
            if left_pressed:
                keyboard.key_up("left")
            if right_pressed:
                keyboard.key_up("right")
            up_pressed, down_pressed, left_pressed, right_pressed = False, False, False, False
            display.show(Image.HAPPY)

        # Press keys when buttons are pressed
        if button_b.is_pressed():
            keyboard.press("space")
            display.show("S")
            sleep(150)
示例#13
0
# -*- coding: utf-8 -*-
"""Wait for a request to read and send orientation.

NOTES:
  1- I wasn't able to make it work with gestures.
  2- Working with accelerometer, x-y-z reads
"""

from microbit import display, sleep, accelerometer
import radio

display.scroll("dice")
display.off()
radio.on()
while True:
    if radio.receive() == 'get_gesture':
        radio.send(','.join(map(str,
                                accelerometer.get_values())))  # 'x,y,z' string
    sleep(500)
from microbit import accelerometer, display, sleep

with open("data.txt","w") as data:
    for _ in range(100):
        readings = accelerometer.get_values()
        data.write(str(readings)+"\n")
        sleep(100)

display.scroll('Done!', wait=False, loop=True)
示例#15
0
# A two-axis bubble level that uses the BBC microbit's accelerometer
from microbit import display, accelerometer, sleep

circle = display.Image("02220:" "20002:" "20002:" "20002:" "02220:")
display.show(circle)

while True:

    tgt_bright = 9
    bubble_bright = 6

    scale = 30
    max_x = 2
    max_y = max_x

    lx, ly, lz = accelerometer.get_values()
    lx = int(lx / scale)
    ly = int(ly / scale)

    if lx < -max_x:
        lx = -max_x
    elif lx > max_x:
        lx = max_x

    if ly < -max_y:
        ly = -max_y
    elif ly > max_y:
        ly = max_y

    if lx == 0 and ly == 0:
        dot_brightness = tgt_bright