adafruit / Adafruit_TinyUSB_Arduino

Arduino library for TinyUSB
MIT License
465 stars 120 forks source link

Add i2c-tiny-usb adapter example #264

Closed hathach closed 1 year ago

hathach commented 1 year ago

based on suggestion in #252, this PR add example to implement i2c-tiny-usb adapter using vendor API that works with Linux driver. close #252

UPDATE: This works so well, that I am considering to make it as part of built-in class driver of this library (tentative)

/* This sketch demonstrates how to use tinyusb vendor interface to implement
 * i2c-tiny-usb adapter to use with Linux
 *
 * Reference:
 * - https://github.com/torvalds/linux/blob/master/drivers/i2c/busses/i2c-tiny-usb.c
 * - https://github.com/harbaum/I2C-Tiny-USB
 *
 * Requirement:
 * - Install i2c-tools with
 *    sudo apt install i2c-tools
 *
 * How to test example:
 * - Compile and flash this sketch on your board with an i2c device, it should enumerated as
 *    ID 1c40:0534 EZPrototypes i2c-tiny-usb interface
 *
 * - Run "i2cdetect -l" to find our bus ID e.g
 *    i2c-8 i2c         i2c-tiny-usb at bus 003 device 039  I2C adapter
 *
 * - Run "i2cdetect -y 8" to scan for on-board device (8 is the above bus ID)
 *         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
      00:                         -- -- -- -- -- -- -- --
      10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
      70: -- -- -- -- -- -- -- 77

   - You can then interact with sensor using following commands:
     i2cget i2cset i2cdump i2ctransfer or using any driver/tools that work on i2c device.
 */
hathach commented 1 year ago

@ita1024 one thing I still haven't tested is reading actual sensor data. I have lots of sensor, but I don't quite sure which tools/driver/script that can be used to e.g print out temperature from sensor. Do you have any suggestion ?

ita1024 commented 1 year ago

@hathach It works on my computer

I created an Arduino project using the three new files that you provided, then uploaded the result successfully to an Adafruit Trinket M0.

$ ls ~/Arduino/offi2c
Adafruit_WireUSB.cpp  Adafruit_WireUSB.h  offi2c.ino

The following Python example uses the sht31 sensor:

#! /usr/bin/env python3
#
# before running, install required libraries:
# pip3 install adafruit-circuitpython-sht31d
# pip3 install adafruit-extended-bus

import adafruit_sht31d, adafruit_extended_bus, time
sensor = adafruit_sht31d.SHT31D(adafruit_extended_bus.ExtendedI2C(22))
while 1:
        print(sensor.temperature, sensor.relative_humidity)
        time.sleep(1)

After connecting the trinket to my computer, the temperature and humidity are detected correctly:

# dmesg -T
[Wed Mar 15 22:50:44 2023] i2c-tiny-usb 1-1.1:1.2: version 1.00 found at bus 001 address 005
[Wed Mar 15 22:50:44 2023] i2c i2c-22: connected i2c-tiny-usb device
# ./sht.py
20.802319371328295 46.86198214694438
20.802319371328295 46.8711375600824
20.818341344319833 46.91386282139315
20.802319371328295 46.927595941100176
20.78629739833677 46.857404440375372

For other i2c sensors, you will need a corresponding application or library (in Python, C, ...). There is no generic solution.

hathach commented 1 year ago

@ita1024 thank you, I am able to get it running with BME280, with extended bus we could reuse lots of Adafruit CircuitPython driver lib !! Considering to make it as part of built-in class driver.

hathach commented 1 year ago

merged as it is for now (as example) since I have to switch to other works, will re-visit to make it as part of class driver later on

ita1024 commented 1 year ago

Thank you for the additional sensor example in the documentation, and for having this merged! :heart: