diff --git a/pycardium/modules/py/color.py b/pycardium/modules/py/color.py new file mode 100644 index 0000000000000000000000000000000000000000..b072ef97f9a33fbd8f3177ccfe8a8f75b2dedd1a --- /dev/null +++ b/pycardium/modules/py/color.py @@ -0,0 +1,44 @@ +import ucollections + +_ColorTuple = ucollections.namedtuple("Color", ["red", "green", "blue"]) + + +class Color(_ColorTuple): + @classmethod + def from_hex(cls, color): + red = (color & 0xff0000) >> 16 + green = (color & 0x00ff00) >> 8 + blue = (color & 0x0000ff) + return cls(red, green, blue) + + def __str__(self): + # Return the color in hex + return "#{:02x}{:02x}{:02x}".format( + self.red, self.green, self.blue + ) + + +Color.BLACK = Color.from_hex(0x000000) +Color.WHITE = Color.from_hex(0xffffff) +Color.RED = Color.from_hex(0xff0000) +Color.GREEN = Color.from_hex(0x00ff00) +Color.YELLOW = Color.from_hex(0xffff00) +Color.BLUE = Color.from_hex(0x0000ff) +Color.MAGENTA = Color.from_hex(0xff00ff) +Color.CYAN = Color.from_hex(0x00ffff) + + +# Add the colors and constructors to the module for convenience +# This allows you to do the following: +# +# import colors +# +# colors.BLACK +# colors.from_hex(0xc6c6c6) +globals().update( + { + n: getattr(Color, n) + for n in dir(Color) + if n.startswith("from") or n.isupper() + } +) diff --git a/pycardium/modules/py/foo.py b/pycardium/modules/py/foo.py deleted file mode 100644 index e95c53d13b2a77c60eaadbc05a6b7ac951b85269..0000000000000000000000000000000000000000 --- a/pycardium/modules/py/foo.py +++ /dev/null @@ -1,8 +0,0 @@ -# Foo Module - - -def bar(): - print("Hello from foo!") - - -X = 3.14 diff --git a/pycardium/modules/py/meson.build b/pycardium/modules/py/meson.build index 29795f41a158ad5c43b4bfbd20f108763ac924a5..27d98ec7fcefc75c3b5635d467335debb94f1ea3 100644 --- a/pycardium/modules/py/meson.build +++ b/pycardium/modules/py/meson.build @@ -1,5 +1,5 @@ python_modules = files( - 'foo.py', + 'color.py', ) frozen_modules = mpy_cross.process(python_modules)