# Example code for (GY-521) MPU6050 Accelerometer/Gyro Module
# Write in MicroPython by Warayut Poomiwatracanont JAN 2023
from mpu6050 import MPU6050
from os import listdir, chdir
from machine import Pin, I2C
from time import sleep_ms
import ssd1306
mpu = MPU6050()
# ESP32 Pin assignment
# i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, MPU6050.i2c)
while (True):
# Accelerometer Data
accel = mpu.read_accel_data() # read the accelerometer [ms^-2]
aX = accel["x"]
aY = accel["y"]
aZ = accel["z"]
print("x: " + str(aX) + " y: " + str(aY) + " z: " + str(aZ))
# # Gyroscope Data
# # gyro = mpu.read_gyro_data() # read the gyro [deg/s]
# # gX = gyro["x"]
# # gY = gyro["y"]
# # gZ = gyro["z"]
# # print("x:" + str(gX) + " y:" + str(gY) + " z:" + str(gZ))
# # Rough Temperature
# temp = mpu.read_temperature() # read the device temperature [degC]
# # print("Temperature: " + str(temp) + "°C")
# # G-Force
# # gforce = mpu.read_accel_abs(g=True) # read the absolute acceleration magnitude
# # print("G-Force: " + str(gforce))
# # Write to file
# data = {"Temp" : temp,
# "AcX" : aX,
# "AcY" : aY,
# "AcZ" : aZ
# }
# push = [ str(data[k]) for k in cols ]
# row = ",".join(push)
# f.write(row + "\n")
# # Time Interval Delay in millisecond (ms)
sleep_ms(100)