Crates.io | tm1637-gpio-driver |
lib.rs | tm1637-gpio-driver |
version | 2.0.6 |
source | src |
created_at | 2020-07-03 11:41:03.980154 |
updated_at | 2022-10-02 14:08:13.33155 |
description | Generic GPIO driver for the TM1637 micro controller, primarily for educational purpose. Doesn't need std-lib and you can choose any GPIO interface/library you want. |
homepage | https://github.com/phip1611/generic-tm1637-gpio-driver-rust |
repository | https://github.com/phip1611/generic-tm1637-gpio-driver-rust |
max_upload_size | |
id | 260974 |
size | 72,399 |
Generic GPIO driver for the TM1637 microcontroller, primarily for educational purpose. For example, the TM1637 built into the 4-digit 7-segment display by AZ-Delivery (Link). Generic means that it is not dependent on a specific GPIO interface. You can choose the GPIO interface/library on your own.
[dependencies]
tm1637-gpio-driver = "<insert latest version>"
# or if you need no_std
tm1637-gpio-driver = { version = "<insert latest version>", default-features = false }
use std::thread::sleep;
use std::time::Duration;
use tm1637_gpio_driver::gpio_api::setup_gpio_cdev;
use tm1637_gpio_driver::TM1637Adapter;
// example that definitely works on Raspberry Pi
fn main() {
// use any GPIO pin you want. This is the number of the pin on the board.
let (clk_pin, dio_pin) = (18, 23);
let bit_delay_fn = Box::from(|| sleep(Duration::from_micros(10)));
let tm1637display = setup_gpio_cdev(clk_pin, dio_pin, bit_delay_fn, "/dev/gpiochip0");
// display "1 2 3 4"
let data: [u8; 4] = [
TM1637Adapter::encode_digit(1),
TM1637Adapter::encode_digit(2),
TM1637Adapter::encode_digit(3),
TM1637Adapter::encode_digit(4),
];
tm1637display.write_segments_raw(&data, 0);
}
no_std
This crate is #![no_std]
An allocator is necessary on embedded systems because extern crate alloc
(core library) is used.
In #![no_std]
you have to disable the default features in Cargo.toml
.
This driver works with other displays too if they use a TM1637 micro controller with the same I2C-like serial bus protocol specified in the data sheet.
I created this library/driver for fun and to learn new things!
See this demo (gif) I made with my Raspberry Pi using regular GPIO pins:
Moving Text:
Time with blinking double point:
This was my first time writing a (super simple basic) kind of a device driver. As of now I'm not that much experienced with micro controllers. After some time I understood how it works by looking at the data sheet. Have a look into my code too! I tried to make as many comments as possible.
You can find code examples in the github repository!
My driver/library is not dependent on a specific GPIO interface.
You can use crates.io: wiringpi or crates.io: gpio
for example. I strongly recommend crates.io: gpio_cdev.
I tested them on my Raspberry Pi. My TM1637Adapter
needs functions/closures
as parameters. These functions are wrappers to write High/Low to the desired Pins.
There are also utility functions on top of the driver in the module fourdigit7segdis
for the 4-digit
7-segment display. You can use them, learn from them or just write your own functions on top of the driver.
To add this driver to your project just add the crate to your Rust project.
As I already said this crate is independent from a specific strategy to access GPIO. But I provide several setup functions for different strategies as listed below (all of them need standard library). To use them activate on of the features in your Cargo.toml:
gpio-api-gpio_cdev
tm1637_gpio_driver::gpio_cdev::setup_gpio_cdev()
gpio-api-gpio
tm1637_gpio_driver::gpio_api::setup_gpio()
gpio-api-sysfs_gpio
tm1637_gpio_driver::sysfs_gpio::setup_sysfs_gpio()
gpio-api-wiringpi
tm1637_gpio_driver::sysfs_gpio::setup_wiringpi()
Probably no! Although I can't test it because I don't have an Arduino or another similar device. This should work on every device where you can write a Rust program for. Since this lib uses no standard library this should work on embedded devices. If you use it let me know what things you've built!
But yes, it was only tested using regular GPIO pins on my Raspberry Pi running Rasbperry Pi OS so far.
I'm Philipp :) Feel free to contribute on Github or message me on Twitter (https://twitter.com/phip1611)!
Special thanks to the creator of the driver for the Arduino. His/her (? - don't know) driver for the Arduino platform helped me to understand how the TM1637 micro controller works. With this work and my effort I put into understanding the data sheet I could make this driver. I also learned a lot about serial data transfer and the I2C-like serial bus protocol used by the TM1637.
I don't use any of the code. It just gave me some inspiration.
TM1637Adapter::new
is not too short.
100µs on Raspberry Pi should be totally fine (but 1µs worked also for me)sudo usermod -a -G gpio <your-user-name>
thread::sleep()
as your bit delay function then you gonna have a problem when it comes
to a few micro seconds: the operating system (or better to say the hardware) is not fast enough
for switching threads in that short period of timeThe MSRV is 1.52.1
.
There is another library on crates.io for the TM1637: https://github.com/igelbox/tm1637-rs It uses the "embedded-hal"-crate and takes another approach. Check this out too. :)
I don't know if "driver" is the right word for this because it is not tied to the operating system. But the display is a device and my library can talk with it .. so yes.. basically a driver, right?