diff --git a/docs/badge/usage.rst b/docs/badge/usage.rst index 4fbae55dcdb2e60b186e19fccbd949596debc758..b6b06c832894c8d585c784bf8d277a27a11331ee 100644 --- a/docs/badge/usage.rst +++ b/docs/badge/usage.rst @@ -55,11 +55,14 @@ pendrive). Open the file ```nick.json`` in a text editor and change your nick, pronouns, font sizes for nick and pronouns, and whatever else you wish. Please note that ``pronouns`` is a list, and should be formatted as such. for example: ``"pronouns": ["aa/bb", "cc/dd"],`` +If you provide a string value there, the code will also handle that, but it +will be converted to a list. For the ``nick.json`` file to appear, you must have started the Nick app at least once. -Use ``"color": "0xffffff",`` to color your name and pronouns. +Use ``"color": "0xffffff",`` to color your name and pronouns, and you can also +use the bottom three petals to change the color on the fly. When you're done editing, unmount/eject the badge from your computer (``umount`` on Linux is enough) and press the left shoulder button to exit Disk diff --git a/python_payload/apps/nick/__init__.py b/python_payload/apps/nick/__init__.py index c7f157fea7c31e79d58c601ed0f92b649e15430c..a7a4481864be3040b5032e1dfa4422f5490e294d 100644 --- a/python_payload/apps/nick/__init__.py +++ b/python_payload/apps/nick/__init__.py @@ -35,13 +35,14 @@ class Configuration: res.size = data["size"] if "font" in data and type(data["font"]) == int: res.font = data["font"] - # type checks don't look inside collections - if ( - "pronouns" in data - and type(data["pronouns"]) == list - and set([type(x) for x in data["pronouns"]]) == {str} - ): - res.pronouns = data["pronouns"] + if "pronouns" in data: + # type checks don't look inside collections + if type(data["pronouns"]) == list and set( + [type(x) for x in data["pronouns"]] + ) == {str}: + res.pronouns = data["pronouns"] + if type(data["pronouns"]) == str: + res.pronouns = data["pronouns"].split() if "pronouns_size" in data: if type(data["pronouns_size"]) == float: res.pronouns_size = int(data["pronouns_size"]) @@ -88,6 +89,13 @@ class NickApp(Application): self._filename = "/flash/nick.json" self._config = Configuration.load(self._filename) self._pronouns_serialized = " ".join(self._config.pronouns) + self._normalized_colors = self._config.to_normalized_tuple() + self.PETAL_R = 6 + self.PETAL_G = 5 + self.PETAL_B = 4 + + def to_denormalized_string(self) -> str: + return f"0x{int(self._normalized_colors[0] * 255):x}{int(self._normalized_colors[1] * 255):x}{int(self._normalized_colors[2] * 255):x}" def draw(self, ctx: Context) -> None: ctx.text_align = ctx.CENTER @@ -96,7 +104,7 @@ class NickApp(Application): ctx.font = ctx.get_font_name(self._config.font) ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill() - ctx.rgb(*self._config.to_normalized_tuple()) + ctx.rgb(*self._normalized_colors) ctx.move_to(0, 0) ctx.save() @@ -115,7 +123,6 @@ class NickApp(Application): leds.set_hsv(int(self._led), abs(self._scale_name) * 360, 1, 0.2) leds.update() - # ctx.fill() def on_exit(self) -> None: self._config.save(self._filename) @@ -130,6 +137,106 @@ class NickApp(Application): if self._led >= 40: self._led = 0 + pos_r = ins.captouch.petals[self.PETAL_R].position + pos_g = ins.captouch.petals[self.PETAL_G].position + pos_b = ins.captouch.petals[self.PETAL_B].position + + if pos_r[0] > 10000: + self._normalized_colors = ( + self._normalized_colors[0] + 1.0 / 32, + self._normalized_colors[1], + self._normalized_colors[2], + ) + + if self._normalized_colors[0] > 1.0: + self._normalized_colors = ( + 1.0, + self._normalized_colors[1], + self._normalized_colors[2], + ) + + self._config.color = self.to_denormalized_string() + + if pos_r[0] < -10000: + self._normalized_colors = ( + self._normalized_colors[0] - 1.0 / 32, + self._normalized_colors[1], + self._normalized_colors[2], + ) + + if self._normalized_colors[0] < 0.0: + self._normalized_colors = ( + 0.0, + self._normalized_colors[1], + self._normalized_colors[2], + ) + + self._config.color = self.to_denormalized_string() + + if pos_g[0] > 20000: + self._normalized_colors = ( + self._normalized_colors[0], + self._normalized_colors[1] + 1.0 / 32, + self._normalized_colors[2], + ) + + if self._normalized_colors[1] > 1.0: + self._normalized_colors = ( + self._normalized_colors[0], + 1.0, + self._normalized_colors[2], + ) + + self._config.color = self.to_denormalized_string() + + if pos_g[0] > 1000 and pos_g[0] < 15000: + self._normalized_colors = ( + self._normalized_colors[0], + self._normalized_colors[1] - 1.0 / 32, + self._normalized_colors[2], + ) + + if self._normalized_colors[1] < 0.0: + self._normalized_colors = ( + self._normalized_colors[0], + 0.0, + self._normalized_colors[2], + ) + + self._config.color = self.to_denormalized_string() + + if pos_b[0] > 10000: + self._normalized_colors = ( + self._normalized_colors[0], + self._normalized_colors[1], + self._normalized_colors[2] + 1.0 / 32, + ) + + if self._normalized_colors[2] > 1.0: + self._normalized_colors = ( + self._normalized_colors[0], + self._normalized_colors[1], + 1.0, + ) + + self._config.color = self.to_denormalized_string() + + if pos_b[0] < -10000: + self._normalized_colors = ( + self._normalized_colors[0], + self._normalized_colors[1], + self._normalized_colors[2] - 1.0 / 32, + ) + + if self._normalized_colors[2] < 0.0: + self._normalized_colors = ( + self._normalized_colors[0], + self._normalized_colors[1], + 0.0, + ) + + self._config.color = self.to_denormalized_string() + # For running with `mpremote run`: if __name__ == "__main__":