> Complete documentation index: [llms.txt](/llms.txt). Markdown versions of all pages are available by appending `.md` to page URLs.

# Nova SDK

> Python SDK for the Binho Nova host adapter (`binho-python-package`).

:::note
This SDK (`binho-python-package`) is Binho Nova's primary SDK. It is not part of the
CosmicSDK/Python SDK transition, which applies to Supernova and Pulsar only.

:::

`binho` is the Python library for the Binho Nova. It includes a command line interface, an interactive shell and example scripts.

## Getting Started

Install the library with pip:

```sh
pip install binho
```

Import the library to use Nova in a script:

```python
from binho import binhoHostAdapter
```

Then initialize a connection to the binho device as such:

```python
# grab the first device found the system finds
binho = binhoHostAdapter()
```

When working on setups with multiple devices, specify the device to connect to in the following 3 ways:

1. grab the device with a specific index\
   `binho = binhoHostAdapter(index=0)`
2. or get the device using the COM port\
   `binho = binhoHostAdapter(port=targetComport)`
3. or get the device using the deviceID number\
   `binho = binhoHostAdapter(deviceID = targetDeviceID)`

At this point it's possible to control the device as desired. Examples of common use cases are included in this library and are briefly discussed below. When finished with the device, close out the connection with the following:

```python
binho.close()
```

That's all there is to it. The example scripts are introduced below, but it may also make sense to review the new Command line interface as well, as it may be possible to achieve the task without writing any code at all.

## Example Scripts

Take a look in the [/binho/examples](https://github.com/binhollc/binho-python-package/tree/main/binho/examples) folder of the codebase (hosted on github) to see example scripts which demonstrate how to use this library in scripts that automate Nova. These example scripts feature a lot of commentary and serve as a tutorial for using this library.

**Basic Examples:**

* [Hello World LED](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/00_hello_world_led.py) This example shows the basics of establishing a connection with the device and setting its LED to BLUE. It's a boring example, but introduces the typical script structure and basics of working with this library.
* [Digital IO and PWM](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/01_digitalio_and_pwm.py) This example shows how to use IO pins for digital input and output, as well as configuring hardware PWM.
* [Analog IO, DAC, and ADC](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/02_analogio_dac_and_adc.py) This example shows how to use IO pins for analog input (using the ADC) and output (using the DAC).
* [SPI Communication](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/03_spi_communication.py) This example shows how to configure the device for SPI communication and transfer data. This example can be run by connecting SDI and SDO signals to loopback the data.
* [I2C Communication](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/04_i2c_communication.py) This example demonstrates how to configure the device for I2C communication and perform reads and writes. It also shows how to scan the bus, and perform read register and write register commands. Adapt it to any I2C device.
* [1-Wire Communication](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/05_1wire_communication.py) This example demonstrates how to configure the device for (Dallas / Maxim) 1-Wire communication by writing to and reading from a [DS24B33+ 1-Wire EEPROM](https://www.mouser.com/ProductDetail/Maxim-Integrated/DS24B33%2b/?qs=%2F%2FkzJz%252Bz9F%2F59wgCoS63UQ%3D%3D). The command set for most 1Wire EEPROMs is similar so this example may work with other devices too.

**Advanced Examples:**

* [I2C EEPROM Demo](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/10_i2c_eeprom_demo.py) - Read and write common I2C EEPROMs, load from and save to .bin and .hex files, and verify against them.
* [SPI Flash Demo](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/20_spi_flash_demo.py) - Read and write common SPI flash devices.
* [SPI Flash SFDP](https://github.com/binhollc/binho-python-package/blob/main/binho/examples/21_spi_flash_sfdp.py) - Discover the parameters of a SPI Flash device without any prior knowledge by reading the JEDEC ID and SPI Flash Discoverable Parameters.

## Command Line Interface

The installation of this library also includes the new command line interface which makes it possible to perform a lot of common functions without needing to write any code. The format of the commands is as follows:

```sh
binho <<subcommand>> [arguments]
```

Each command has their own unique arguments, but all commands except 'info' support the following:

* `-h, --help`: prints the list of arguments
* `-v, --verbose`: display more details on the console during execution
* `-d, --device <deviceID>`: connect to the device with the provided deviceID number
* `-p, --port <commport>`: connect to the device on the provided COM port
* `-i, --index <i>`: connect to the device at index i

_Note that only one of `-d`, `-p`, or `-i` arguments can be supplied to any command._

**Device Management Subcommands**

* **`binho info`** This command can be used to find all Novas connected to the PC and get their associated information such as serial number, COM port, and firmware version. It will also indicate if a device is in DAPLink or Bootloader mode as well.
* **`binho dfu`** Updates the device firmware automatically, or enters Bootloader mode.

**IO Subcommands**

* **`binho gpio`**\
  This command can be used to take digital readings or drive IO pins as digital outputs. The device does not retain its state between consecutive runs of this command. As such, a subsequent run will overwrite any previous pin configurations.
* **`binho dac`** This command can be used to set the DAC output to a given voltage or raw 10bit value.
* **`binho adc`**\
  This command can be used to take readings from IO pins using the analog inputs (ADC).
* **`binho pwm`**\
  This command can be used to configure the IO pins to be used as PWM outputs.

**Protocol Subcommands**

* **`binho i2c`**\
  This command can be used to perform I2C Scans, Reads, and Writes.
* **`binho eeprom`**\
  This command can be used to read from and write to common I2C EEPROM devices.
* **`binho spi`**\
  This command can be used to perform SPI transfers.
* **`binho 1wire`** This command can be used to communicate with 1-Wire devices.

**DAPLink Subcommands**

Binho Nova can be used to program and debug microcontrollers by operating in DAPLink mode.

* **`binho daplink`**\
  Use this command to switch Nova into and out of DAPLink mode.
* **`binho flasher`**\
  While in DAPLink mode, this command can be used to program bin/hex files into microcontrollers.

**Misc. Subcommands**

* **`binho shell`**\
  This command can be used to open up a connection to the device and begin an interactive shell.
* **`binho custom`**\
  This command is a template for custom commands, which extend the command line for custom tasks. See the implementation [here](https://github.com/binhollc/binho-python-package/blob/main/binho/commands/binho_custom.py).

## Development

To work on the library from source:

#### Installation

```sh
python3 setup.py install
```

#### Building Docs

Build the documentation:

```sh
pip3 install .[dev]
docs\make.bat html
```

## Additional Resources

The package on PyPI:

[https://pypi.org/project/binho/](https://pypi.org/project/binho/)

The source code on GitHub:

[https://github.com/binhollc/binho-python-package](https://github.com/binhollc/binho-python-package)