# WiFiBoy Gyro example (BlitEngine/WiFiBoyMini)
# 2018.10.24, tklua@wifiboy.org

from machine import I2C, Pin
from mpu9250 import MPU9250
i2c = I2C(scl=Pin(22), sda=Pin(23))
sensor = MPU9250(i2c)

wb.blitpal()
star=[]; starspeed=[]
for i in range(0,20):star.append(wb.rand(16384));starspeed.append(wb.rand(4)+1)
x,y = 50,50;
while True:
    wb.blitbuf()
    for j in range(0,20):
        wb.blitbuf(star[j],255)
        star[j] = star[j]+128*starspeed[j]
        if (star[j]>16384): star[j] = star[j] - 16384
    if sensor.acceleration[0]>1:
        if sensor.acceleration[0]>3: x+=4
        else: x+=2
        if x > 112: x = 112
    if sensor.acceleration[0]<-1:
        if sensor.acceleration[0]<-3: x-=4
        else: x-=2
        if x < 0: x = 0    
    if sensor.acceleration[1]>1:
        if sensor.acceleration[0]>3: y+=4
        else: y+=2
        if y > 112: y = 112
    if sensor.acceleration[1]<-1:
        if sensor.acceleration[0]<-3: y-=4
        else: y-=2
        if y < 0: y = 0
    wb.blitimg(115, x, y, 16) 
    wb.blit()
    