Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
npentrel committed Jun 16, 2024
1 parent bc4fbba commit 5ea9e6b
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 303 deletions.
4 changes: 0 additions & 4 deletions docs/hexpansions/.flake8

This file was deleted.

12 changes: 6 additions & 6 deletions docs/hexpansions/eeprom.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,17 @@ The header is 32 bytes long and contains the following values:
An example implementation of the checksum algorithm in Python:

```python

def calc_checksum(header): #header assumed to be of type bytes
value=0x55
def calc_checksum(header): # header assumed to be of type bytes
value = 0x55
for b in header[1:]:
value= value ^ b
value = value ^ b
return value

header_w_checksum = header+bytes([calc_checksum(header)]) #to generate a checksum

calc_checksum(header_w_checksum) # should return 0 if checksum is correct
# to generate a checksum
header_w_checksum = header+bytes([calc_checksum(header)])

calc_checksum(header_w_checksum) # should return 0 if checksum is correct
```

The header format uses little-endian byte ordering for the individual values, e.g. 0x0123 = 0x01 as least significant byte and 0x23 as most significant byte, resulting in a value of 8961.
Expand Down
27 changes: 14 additions & 13 deletions docs/hexpansions/writing-hexpansion-apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Below is an example of how you find which port your hexpansion is plugged in to
def draw(self, ctx):
ctx.save()
clear_background(ctx)
ctx.rgb(0,1,0).move_to(-90,-40).text("Hello from your\nhexpansion!")
ctx.rgb(0, 1, 0).move_to(-90, -40).text("Hello from your\nhexpansion!")
ctx.restore()

return None
Expand Down Expand Up @@ -109,7 +109,7 @@ Below is an example of how you find which port your hexpansion is plugged in to
def draw(self, ctx):
ctx.save()
clear_background(ctx)
ctx.rgb(0,1,0).move_to(-90,-40).text(self.text)
ctx.rgb(0, 1, 0).move_to(-90, -40).text(self.text)
ctx.restore()

def scan_for_hexpansion(self):
Expand All @@ -132,7 +132,7 @@ Below is an example of how you find which port your hexpansion is plugged in to
# You can add some logic here to check the PID and VID match your hexpansion
return HexpansionConfig(port)

self.color = (1,0,0)
self.color = (1, 0, 0)
self.text = "No hexpansion found."

return None
Expand Down Expand Up @@ -181,20 +181,20 @@ Below is an example of how you find which port your hexpansion is plugged in to
# This might look weird, but we're just drawing a shape as a port indicator.
ctx.save()
ctx.font_size = 22
ctx.rgb(*colors["dark_green"]).rectangle(-120,-120, 240, 100).fill()
ctx.rgb(*colors["dark_green"]).rectangle(-120, -120, 240, 100).fill()
ctx.rgb(*colors["dark_green"]).rectangle(-120, 20, 240, 100).fill()
rotation_angle = self.menu.position*pi/3
ctx.rgb(*colors["mid_green"]).rotate(rotation_angle).rectangle(80,-120,40,240).fill()
ctx.rgb(*colors["mid_green"]).rotate(rotation_angle).rectangle(80, -120, 40, 240).fill()
prompt_message = "Select hexpansion port:"
ctx.rgb(1,1,1).rotate(-rotation_angle).move_to(0,-45).text(prompt_message)
ctx.rgb(1, 1, 1).rotate(-rotation_angle).move_to(0, -45).text(prompt_message)
ctx.restore()

else:
ctx.save()
ctx.font_size = 24
msg = "Hexpansion in port " + str(self.hexpansion_config.port)
msg_width = ctx.text_width(msg)
ctx.rgb(1,1,1).move_to(-msg_width/2,0).text(msg)
ctx.rgb(1, 1, 1).move_to(-msg_width/2, 0).text(msg)
ctx.restore()

__app_export__ = ExampleApp
Expand All @@ -206,12 +206,13 @@ In all of these examples, the `HexpansionConfig` object is used to provide infor

The `HexpansionConfig` object that you get after following the examples is where the magic all happens. It allows you to access the following:

| Object | Description | Example Usage |
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| `HexpansionConfig.port` | The port number your hexpansion is connected to. | |
| `HexpansionConfig.pin[]` | A list of 4 `Pin` objects. These are the high-speed, direct GPIO pins for this hexpansion port. | [See MicroPython Docs](https://docs.micropython.org/en/latest/library/machine.Pin.html) |
| `HexpansionConfig.ls_pin[]` | A list of 5 `ePin` objects for this hexpansion port. These are the emulated, low-speed GPIO pins for this hexpansion port. | [See eGPIO](../tildagon-apps/reference/badge-hardware.md#egpio) |
| `HexpansionConfig.i2c` | The dedicated `I2C` object for this hexpansion port. | [See I2C](../tildagon-apps/reference/badge-hardware.md#i2c) |
<!-- prettier-ignore -->
| Object | Description | Example Usage |
| ------ | ----------- | ------------- |
| `HexpansionConfig.port` | The port number your hexpansion is connected to. | |
| `HexpansionConfig.pin[]` | A list of 4 `Pin` objects. These are the high-speed, direct GPIO pins for this hexpansion port. | [See MicroPython Docs](https://docs.micropython.org/en/latest/library/machine.Pin.html) |
| `HexpansionConfig.ls_pin[]` | A list of 5 `ePin` objects for this hexpansion port. These are the emulated, low-speed GPIO pins for this hexpansion port. | [See eGPIO](../tildagon-apps/reference/badge-hardware.md#egpio) |
| `HexpansionConfig.i2c` | The dedicated `I2C` object for this hexpansion port. | [See I2C](../tildagon-apps/reference/badge-hardware.md#i2c) |

### Pin vs ePin

Expand Down
40 changes: 24 additions & 16 deletions docs/tildagon-apps/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ class ExampleApp(app.App):
def update(self, delta):
if self.button_states.get(BUTTON_TYPES["CANCEL"]):
# The button_states do not update while you are in the background.
# Calling clear() ensures the next time you open the app, it stays open.
# Without it the app would close again immediately.
# Calling clear() ensures the next time you open the app, it stays
# open. Without it the app would close again immediately.
self.button_states.clear()
self.minimise()

def draw(self, ctx):
ctx.save()
ctx.rgb(0.2,0,0).rectangle(-120,-120,240,240).fill()
ctx.rgb(1,0,0).move_to(-80,0).text("Hello world")
ctx.rgb(0.2, 0, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(1, 0, 0).move_to(-80, 0).text("Hello world")
ctx.restore()


__app_export__ = ExampleApp
```

Expand Down Expand Up @@ -171,8 +172,8 @@ Afterwards, you'll learn how to [update state while an app is minimized](#update
self.counter = self.counter + 1

def draw(self, ctx):
ctx.rgb(0,0.2,0).rectangle(-120,-120,240,240).fill()
ctx.rgb(0,1,0).move_to(-80,0).text(str(self.counter))
ctx.rgb(0, 0.2, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(0, 1, 0).move_to(-80, 0).text(str(self.counter))

__app_export__ = RightButtonCounterApp
```
Expand All @@ -195,8 +196,8 @@ Afterwards, you'll learn how to [update state while an app is minimized](#update
self.counter = self.counter + 1

def draw(self, ctx):
ctx.rgb(0,0.2,0).rectangle(-120,-120,240,240).fill()
ctx.rgb(0,1,0).move_to(-80,0).text(str(self.counter))
ctx.rgb(0, 0.2, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(0, 1, 0).move_to(-80, 0).text(str(self.counter))

__app_export__ = RightButtonCounterApp
```
Expand Down Expand Up @@ -225,8 +226,8 @@ Afterwards, you'll learn how to [update state while an app is minimized](#update
self.counter = self.counter + 1

def draw(self, ctx):
ctx.rgb(0,0.2,0).rectangle(-120,-120,240,240).fill()
ctx.rgb(0,1,0).move_to(-80,0).text(str(self.counter))
ctx.rgb(0, 0.2, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(0, 1, 0).move_to(-80, 0).text(str(self.counter))

__app_export__ = RightButtonCounterApp
```
Expand All @@ -244,6 +245,7 @@ import app

from events.input import Buttons, BUTTON_TYPES


class TimeCounterApp(app.App):
def __init__(self):
self.button_states = Buttons(self)
Expand All @@ -254,13 +256,14 @@ class TimeCounterApp(app.App):
self.button_states.clear()
self.minimise()

def background_update():
def background_update(self):
# if self.button_states.get(BUTTON_TYPES["RIGHT"]):
self.counter = self.counter + 1

def draw(self, ctx):
ctx.rgb(0,0.2,0).rectangle(-120,-120,240,240).fill()
ctx.rgb(0,1,0).move_to(-80,0).text(str(self.counter))
ctx.rgb(0, 0.2, 0).rectangle(-120, -120, 240, 240).fill()
ctx.rgb(0, 1, 0).move_to(-80, 0).text(str(self.counter))


__app_export__ = TimeCounterApp
```
Expand All @@ -280,14 +283,15 @@ import random
from app_components import clear_background
from events.input import Buttons, BUTTON_TYPES


class Rectangle(object):
def __init__(self):
self.r = random.random()
self.g = random.random()
self.b = random.random()

def draw(self, ctx):
ctx.rgb(self.r, self.g, self.b).rectangle(-60,-60,120,120).fill()
ctx.rgb(self.r, self.g, self.b).rectangle(-60, -60, 120, 120).fill()


class OverlaysApp(app.App):
Expand Down Expand Up @@ -315,6 +319,7 @@ class OverlaysApp(app.App):
clear_background(ctx)
self.draw_overlays(ctx)


__app_export__ = OverlaysApp
```

Expand Down Expand Up @@ -354,9 +359,11 @@ class BasicApp(app.App):
# Create a yes/no dialogue, add it to the overlays
dialog = YesNoDialog("Change the colour?", self)
self.overlays = [dialog]
# Wait for an answer from the dialogue, and if it was yes, randomise colour
# Wait for an answer from the dialogue, and if it was yes,
# randomise colour
if await dialog.run(render_update):
self.color = (random.random(), random.random(), random.random())
self.color = (
random.random(), random.random(), random.random())

# Remove the dialogue and re-render
self.overlays = []
Expand All @@ -371,6 +378,7 @@ class BasicApp(app.App):

self.draw_overlays(ctx)


__app_export__ = BasicApp
```

Expand Down
12 changes: 8 additions & 4 deletions docs/tildagon-apps/examples/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class FilmScheduleApp(app.App):
# When we load, grab all the API data in JSON format
# Requests will automatically convert this to a python dict
# for us, it really is that good!
self.schedule = requests.get("https://emffilms.org/schedule.json").json()
self.schedule = requests.get(
"https://emffilms.org/schedule.json").json()
self.button_states = Buttons(self)
# Setup lists to hold our film titles and timings
main_menu_items = []
Expand All @@ -56,7 +57,8 @@ class FilmScheduleApp(app.App):
self.notification = None

def select_handler(self, item, position):
self.notification = Notification('Showing at ' + self.timings[position] + '!')
self.notification = Notification(
'Showing at ' + self.timings[position] + '!')

def back_handler(self):
self.button_states.clear()
Expand All @@ -67,7 +69,6 @@ class FilmScheduleApp(app.App):
if self.notification:
self.notification.update(delta)


def draw(self, ctx):
clear_background(ctx)
# Display the menu on the device
Expand All @@ -76,6 +77,7 @@ class FilmScheduleApp(app.App):
if self.notification:
self.notification.draw(ctx)


__app_export__ = FilmScheduleApp
```

Expand Down Expand Up @@ -136,6 +138,7 @@ def FetchWeather():
print("Error fetching weather data")
raise Exception("Error fetching weather data")


class WeatherType:
id: int
main: str
Expand Down Expand Up @@ -172,6 +175,7 @@ class WeatherInfo:
def human_readable(self):
return f"{self.weather.main}, {round(self.temp, 1)}°C"


class WeatherApp(app.App):
text: str
connected: bool
Expand Down Expand Up @@ -217,7 +221,7 @@ class WeatherApp(app.App):
try:
weather = FetchWeather()
self.text = weather.human_readable()
except:
except Exception:
self.text = "API failure.\nTry again."


Expand Down
30 changes: 19 additions & 11 deletions docs/tildagon-apps/examples/detect-hexpansion.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ from machine import I2C
from app_components import clear_background
from events.input import Buttons, BUTTON_TYPES
from system.eventbus import eventbus
from system.hexpansion.events import HexpansionRemovalEvent, HexpansionInsertionEvent
from system.hexpansion.events import \
HexpansionRemovalEvent, HexpansionInsertionEvent
from system.hexpansion.util import read_hexpansion_header, detect_eeprom_addr


class ExampleApp(app.App):
def __init__(self):
self.button_states = Buttons(self)
self.text = "No hexpansion found."
self.color = (1,0,0)
self.color = (1, 0, 0)
self.scan_for_hexpansion()

eventbus.on(HexpansionInsertionEvent, self.handle_hexpansion_insertion, self)
eventbus.on(HexpansionRemovalEvent, self.handle_hexpansion_removal, self)
eventbus.on(
HexpansionInsertionEvent,
self.handle_hexpansion_insertion,
self)
eventbus.on(
HexpansionRemovalEvent,
self.handle_hexpansion_removal,
self)

def handle_hexpansion_insertion(self, event):
self.scan_for_hexpansion()
Expand All @@ -39,8 +47,8 @@ class ExampleApp(app.App):
def draw(self, ctx):
ctx.save()
clear_background(ctx)
x,y,z = self.color
ctx.rgb(x,y,z).move_to(-90,-40).text(self.text)
x, y, z = self.color
ctx.rgb(x, y, z).move_to(-90, -40).text(self.text)
ctx.restore()

def scan_for_hexpansion(self):
Expand All @@ -60,21 +68,21 @@ class ExampleApp(app.App):
continue
else:
print("Read header: " + str(header))
self.text = "Hexp. found.\nvid: {}\npid: {}\nat port: {}".format(hex(header.vid), hex(header.pid), port)
self.text = "Hexp. found.\nvid: {}\npid: {}\nat port: {}".format(
hex(header.vid), hex(header.pid), port)
found = True

# Swap 0xCAFE with your EEPROM header vid
# Swap 0xCAFF with your EEPROM header pid
if (header.vid is 0xCAFE) and (header.pid is 0xCAFF):
if (header.vid == 0xCAFE) and (header.pid == 0xCAFF):
print("Found the desired hexpansion in port " + str(port))
self.color = (0,1,0)
self.color = (0, 1, 0)
else:
print()
if not found:
self.color = (1,0,0)
self.color = (1, 0, 0)
self.text = "No hexpansion found."


return None
```

Expand Down
Loading

0 comments on commit 5ea9e6b

Please sign in to comment.