Skip to content

Commit

Permalink
Minor fixes to the Joystick tutorial. (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch authored Nov 8, 2024
1 parent add663c commit 354684b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions docs/peripherals/drivers/sparkfun_joystick.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# How to write a driver

We are using the [SparkFun Qwiic Joystick](https://www.sparkfun.com/products/15168) as an example of how to write a driver - in the Toit language - for a sensor. The SparkFun Qwiic Joystick is a 2-axis joystick with a single button. Using a Qwiic connector, it's very simple to get the joystick connected.
We are using the [SparkFun Qwiic Joystick](https://www.sparkfun.com/products/15168) as an example of how to write a driver for a sensor. The SparkFun Qwiic Joystick is a 2-axis joystick with a single button. Using a Qwiic connector, it's very simple to get the joystick connected.

This guide will walk you through the steps of identifying how the sensor communicates and how to write a fully working driver for it.

The completed package can be found in the [toit-qwiic-joystick repository](https://github.com/toitware/toit-qwiic-joystick).

## Approach

The Joystick features an `ATtiny85` microcontroller with a custom firmware. As described in the [Hookup Guide](https://learn.sparkfun.com/tutorials/qwiic-joystick-hookup-guide), the firmware exposes several registers. With that in mind, we're going to do the following steps:
Expand All @@ -18,7 +20,7 @@ The Joystick features an `ATtiny85` microcontroller with a custom firmware. As d

## I2C setup

We use a simple I<sup>2</sup>C setup, currently using pin `21` for `SDA` (blue) and pin `22` for `SCL` (yellow).
We use a simple I<sup>2</sup>C setup, with pin `21` for `SDA` (blue) and pin `22` for `SCL` (yellow).

```
import gpio
Expand Down Expand Up @@ -46,15 +48,17 @@ class Joystick:
registers_ = device.registers
```

The hookup guide has a table of I<sup>2</sup>C registers available in the custom firmware. At address `0x00` is the slave address assigned to the device (hard-coded to `0x20`).

<Note>

Most drivers turn on their devices in the constructor, and shut them down in a `close` method.
Drivers that can be turned on or off repeatedly should instead have `on` and `off` methods.

</Note>

The hookup guide has a table of I<sup>2</sup>C registers available in the custom firmware. At address `0x00` is the slave address assigned to the device (hard-coded to `0x20`).

For simplicity we didn't add a `close` method, but it's OK to have an empty one.

## Validate connectivity

By reading the `REG-DEFAULT-ADDRESS_` register, we can confirm the connectivity to the device is functional.
Expand All @@ -65,7 +69,7 @@ By reading the `REG-DEFAULT-ADDRESS_` register, we can confirm the connectivity
<!-- HIDDEN CODE registers_ := null -->

```
class SparkFunJoystick:
class Joystick:
static REG-DEFAULT-ADDRESS_ ::= 0x00

// ...
Expand Down Expand Up @@ -106,7 +110,7 @@ We're going to expand the driver with 3 new methods:
<!-- SKIP CODE -->

```
class SparkFunJoystick:
class Joystick:

// ...

Expand Down

0 comments on commit 354684b

Please sign in to comment.