Skip to content
Snippets Groups Projects
Commit c48986f2 authored by Gaspard Le Gouic's avatar Gaspard Le Gouic
Browse files

added comments

parent 1271627b
No related branches found
No related tags found
No related merge requests found
...@@ -459,15 +459,15 @@ class Uart: ...@@ -459,15 +459,15 @@ class Uart:
class Accel: class Accel:
def init(): def init():
"""_summary_ """Initialize the accelerometer of MyLab2
""" """
i2c.writeto_mem(0x1d, 0x20, b'\x57') i2c.writeto_mem(0x1d, 0x20, b'\x57')
def get_x(): def get_x():
"""_summary_ """Get the x axis value in g
Returns: Returns:
_type_: _description_ float: The x axis value in g
""" """
buf = i2c.readfrom_mem(0x1d, 0xa8, 2) buf = i2c.readfrom_mem(0x1d, 0xa8, 2)
x = ((buf[0] & 0xff) | ((buf[1] & 0xff) << 8)) / 16384 x = ((buf[0] & 0xff) | ((buf[1] & 0xff) << 8)) / 16384
...@@ -476,10 +476,10 @@ class Accel: ...@@ -476,10 +476,10 @@ class Accel:
return x return x
def get_y(): def get_y():
"""_summary_ """Get the y axis value in g
Returns: Returns:
_type_: _description_ float: The y axis value in g
""" """
buf = i2c.readfrom_mem(0x1d, 0xaa, 2) buf = i2c.readfrom_mem(0x1d, 0xaa, 2)
y = ((buf[0] & 0xff) | ((buf[1] & 0xff) << 8)) / 16384 y = ((buf[0] & 0xff) | ((buf[1] & 0xff) << 8)) / 16384
...@@ -488,10 +488,10 @@ class Accel: ...@@ -488,10 +488,10 @@ class Accel:
return y return y
def get_z(): def get_z():
"""_summary_ """Get the z axis value in g
Returns: Returns:
_type_: _description_ float: The y axis value in g
""" """
buf = i2c.readfrom_mem(0x1d, 0xac, 2) buf = i2c.readfrom_mem(0x1d, 0xac, 2)
z = ((buf[0] & 0xff) | ((buf[1] & 0xff) << 8)) / 16384 z = ((buf[0] & 0xff) | ((buf[1] & 0xff) << 8)) / 16384
...@@ -500,13 +500,13 @@ class Accel: ...@@ -500,13 +500,13 @@ class Accel:
return z return z
def facing(side): def facing(side):
"""_summary_ """Indicate if we are facing the "side" argument of the MyLab2
Args: Args:
side (_type_): _description_ side (Direction): Either Direction.FRONT or Direction.BACK
Returns: Returns:
_type_: _description_ bool: True or False
""" """
z = Accel.get_z() z = Accel.get_z()
if side == Direction.FRONT: if side == Direction.FRONT:
...@@ -523,13 +523,13 @@ class Accel: ...@@ -523,13 +523,13 @@ class Accel:
return False return False
def tilting(dir): def tilting(dir):
"""_summary_ """Indicate if the MyLab2 card is tilting in the "dir" argument direction
Args: Args:
dir (_type_): _description_ dir (Direction): Either Direction.NORTH or Direction.SOUTH or Direction.EAST or Direction.WEST
Returns: Returns:
_type_: _description_ bool: True or False
""" """
if dir == Direction.NORTH or dir == Direction.SOUTH: if dir == Direction.NORTH or dir == Direction.SOUTH:
x = Accel.get_x() x = Accel.get_x()
...@@ -562,39 +562,39 @@ class Touch: ...@@ -562,39 +562,39 @@ class Touch:
@classmethod @classmethod
def attach(cls, cb): def attach(cls, cb):
"""_summary_ """Attach the callback "cb" to the touchscreen interruption
Args: Args:
cb (function): _description_ cb (function): Callback. It callback must take the single argument "pos" indicating a Position
""" """
cls.callback = cb cls.callback = cb
@classmethod @classmethod
def detach(cls): def detach(cls):
"""_summary_ """Detach the callback
""" """
cls.callback = None cls.callback = None
@classmethod @classmethod
def callback_do(cls, *args, **kwargs): def callback_do(cls, *args, **kwargs):
"""_summary_ """Execute the callback
""" """
if cls.callback: if cls.callback:
cls.callback(*args, **kwargs) cls.callback(*args, **kwargs)
def init(): def init():
"""_summary_ """Initialize the touchscreen of MyLab2
""" """
i2c.writeto_mem(0x38, 0xa4, b'\x00') i2c.writeto_mem(0x38, 0xa4, b'\x00')
def read(pos): def read(pos):
"""_summary_ """Indicate if we are touching the "pos" position
Args: Args:
pos (_type_): _description_ pos (Position): Either Position.SOUTH_EAST or Position.SOUTH_WEST or Position.NORTH_EAST or Position.NORTH_WEST
Returns: Returns:
_type_: _description_ bool: True or False
""" """
buf = i2c.readfrom_mem(0x38, 0x03, 4) buf = i2c.readfrom_mem(0x38, 0x03, 4)
flag = buf[0] >> 6 flag = buf[0] >> 6
...@@ -623,14 +623,14 @@ class Touch: ...@@ -623,14 +623,14 @@ class Touch:
return False return False
def compute_pos(x, y): def compute_pos(x, y):
"""_summary_ """Convert x, y screen position to dial position
Args: Args:
x (_type_): _description_ x (int): x screen position
y (_type_): _description_ y (int): y screen position
Returns: Returns:
_type_: _description_ Position: Dial position
""" """
if x < 120 and y < 160: if x < 120 and y < 160:
return Position.SOUTH_EAST return Position.SOUTH_EAST
...@@ -642,10 +642,10 @@ class Touch: ...@@ -642,10 +642,10 @@ class Touch:
return Position.NORTH_WEST return Position.NORTH_WEST
def read_pos(): def read_pos():
"""_summary_ """Read x, y touch on screen position
Returns: Returns:
_type_: _description_ Tuple(int): x, y touch on screen position
""" """
buf = i2c.readfrom_mem(0x38, 0x03, 4) buf = i2c.readfrom_mem(0x38, 0x03, 4)
x = ((buf[0] & 0x0f) << 8) | (buf[1] & 0xff) x = ((buf[0] & 0x0f) << 8) | (buf[1] & 0xff)
...@@ -653,10 +653,10 @@ class Touch: ...@@ -653,10 +653,10 @@ class Touch:
return x, y return x, y
def touch_cb(pos): def touch_cb(pos):
"""_summary_ """Callback displaying corresponding color of dial on LED matrix
Args: Args:
pos (_type_): _description_ pos (Position): The touch dial position
""" """
if pos == Position.NORTH_WEST: if pos == Position.NORTH_WEST:
Matrix.clear(0) Matrix.clear(0)
...@@ -680,7 +680,7 @@ class Touch: ...@@ -680,7 +680,7 @@ class Touch:
Matrix.set_led(i + 4, j + 4, Color.BLUE) Matrix.set_led(i + 4, j + 4, Color.BLUE)
def handler(): def handler():
"""_summary_ """Touchscreen IRQ handler
""" """
x, y = Touch.read_pos() x, y = Touch.read_pos()
pos = Touch.compute_pos(x, y) pos = Touch.compute_pos(x, y)
...@@ -689,25 +689,25 @@ class Touch: ...@@ -689,25 +689,25 @@ class Touch:
class Lcd: class Lcd:
def write_cmd(cmd): def write_cmd(cmd):
"""_summary_ """Write command "cmd" to SPI MOSI
Args: Args:
cmd (_type_): _description_ cmd (byte): The command byte
""" """
p26.off() p26.off()
spi.write(cmd) spi.write(cmd)
def write_data(data): def write_data(data):
"""_summary_ """Write data "data" to SPI MOSI
Args: Args:
data (_type_): _description_ data (byte): The data byte
""" """
p26.on() p26.on()
spi.write(data) spi.write(data)
def init(): def init():
"""_summary_ """Initialize MyLab2 LCD
""" """
Lcd.write_cmd(b'\x01') Lcd.write_cmd(b'\x01')
time.sleep_ms(5) time.sleep_ms(5)
...@@ -823,13 +823,13 @@ class Lcd: ...@@ -823,13 +823,13 @@ class Lcd:
time.sleep_ms(50) time.sleep_ms(50)
def set_window(x, y, width, height): def set_window(x, y, width, height):
"""_summary_ """Define MyLab2 LCD window
Args: Args:
x (_type_): _description_ x (int): x window screen position
y (_type_): _description_ y (int): y window screen position
width (_type_): _description_ width (int): window width
height (_type_): _description_ height (int): window height
""" """
Lcd.write_cmd(b'\x2a'); Lcd.write_cmd(b'\x2a');
Lcd.write_data(((x >> 8) & 0xff).to_bytes(1, 'big')); Lcd.write_data(((x >> 8) & 0xff).to_bytes(1, 'big'));
...@@ -844,7 +844,7 @@ class Lcd: ...@@ -844,7 +844,7 @@ class Lcd:
Lcd.write_data(((y + height - 1) & 0xff).to_bytes(1, 'big')); Lcd.write_data(((y + height - 1) & 0xff).to_bytes(1, 'big'));
def touch_test(): def touch_test():
"""_summary_ """Test function for touchscreen. Print touched dial.
""" """
if Touch.read(Position.NORTH_WEST): if Touch.read(Position.NORTH_WEST):
print("Touch north west") print("Touch north west")
...@@ -856,14 +856,14 @@ def touch_test(): ...@@ -856,14 +856,14 @@ def touch_test():
print("Touch south east") print("Touch south east")
def touch_irq_test(): def touch_irq_test():
"""_summary_ """Test function for touchscreen with IRQ. Display correponding color on LED matrix.
""" """
p4.irq(lambda pin: Touch.handler(), Pin.IRQ_FALLING) p4.irq(lambda pin: Touch.handler(), Pin.IRQ_FALLING)
Touch.attach(Touch.touch_cb) Touch.attach(Touch.touch_cb)
Touch.init() Touch.init()
def lcd_test(): def lcd_test():
"""_summary_ """Test function for LCD. Display 4 differents colors dials.
""" """
p22.on() p22.on()
cs(0) cs(0)
...@@ -899,7 +899,7 @@ def lcd_test(): ...@@ -899,7 +899,7 @@ def lcd_test():
Lcd.write_data(b'\x1f') Lcd.write_data(b'\x1f')
def accel_test(): def accel_test():
"""_summary_ """Test function for accelerometer. Print facing direction, tilting direction and x,y,z values in g.
""" """
if Accel.tilting(Direction.NORTH): if Accel.tilting(Direction.NORTH):
print('Tilting North !') print('Tilting North !')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment