MadDrivers

0.0.10

MadMachine drivers for sensors and other devices based on SwiftIO
madmachineio/MadDrivers

What's New

MadDrivers 0.0.10

2021-12-09T06:25:05Z

🚀 New Drivers

  • ADXL345
  • BMP280
  • DS3231
  • MPU6050
  • PCF8523
  • PCF8563

MadDrivers

build Discord twitter

This MadDrivers library, based on SwiftIO, provides an easy way to use all kinds of devices with your boards. You could directly use the related class to read or write data and don't need to understand the communication details.

Note: This library aims to allow you to program the devices easily, so some uncommon errors or rare situations aren't be considered. The SwiftIO library can give the messages about the communication and which error occurs if it fails. If you need more detailed results about the device status to ensure security, you can download and modify the code according to your need.

Drivers

You can find some frequently-used hardware here:

Type Device Communication
Accelerometer ADXL345 I2C/SPI
LIS3DH I2C/SPI
Color VEML6040 I2C
DAC MCP4725 I2C
Dispaly
IS31FL3731 I2C
LCD1602 I2C
ST7789 SPI
Gyroscope MPU6050 I2C
Light BH1750 I2C
Pressure BMP280 I2C/SPI
RTC DS3231 I2C
PCF8523 I2C
PCF8563 I2C
Temperature & Humidity DHTxx GPIO
SHT3x I2C
Ultrasonic HCSR04 GPIO

We will keep adding more drivers. And your contributions are welcome!

Use drivers

Take the library SHT3x for example:

  1. Create an executable project. You can refer to this tutorial.

  2. Open the project and open the file package.swift.

    The MadDrivers has already been added to your project by default. You could use all libraries in it. It's better to specify the specific library to reduce the build time for your project. Change the statement

    .product(name: "MadDrivers", package: "MadDrivers") to

    .product(name: "SHT3x", package: "MadDrivers") as shown below.

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
    name: "sht3x",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/madmachineio/SwiftIO.git", .upToNextMajor(from: "0.0.1")),
        .package(url: "https://github.com/madmachineio/MadBoards.git", .upToNextMajor(from: "0.0.1")),
        .package(url: "https://github.com/madmachineio/MadDrivers.git", .upToNextMajor(from: "0.0.1")),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages this package depends on.
        .target(
            name: "sht3x",
            dependencies: [
                "SwiftIO",
                "MadBoards",
                // use specific library would speed up the compile procedure
                .product(name: "SHT3x", package: "MadDrivers")
            ]),
        .testTarget(
            name: "sht3xTests",
            dependencies: ["sht3x"]),
    ]
)
  1. Now, you can write code for your project. In the file main.swift, import the SHT3x, then you could use everything in it to communicate with the sensor.
import SwiftIO
import MadBoard
import SHT3x

let i2c = I2C(Id.I2C1)
let sensor = SHT3x(i2c)

while true {
    let temperature = sensor.readCelsius()
    let humidity = sensor.readHumidity()
    print(temperature)
    print(humidity)
    sleep(ms: 1000)
}

Try examples

At first, you could try demo projects in the folder Examples.

In the Examples folder, there are folders for different devices. Each folder may have one or several projects to help you get started with each device.

├── Examples
│   ├── ADXL345
│   │   ├── ReadXYZ
│   ├── DS3232
│   │   ├── Alarm
│   │   ├── ReadTime
│   │   ├── Timer
│   ├── IS31FL3731
│   ├── ├── BreathingLED
│   ├── ├── DrawPixels
│   ├── ├── Frame
│   ├── ├── ScrollingText
│   ├── ...

You could download the whole folder and try the examples. Here is a tutorial about how to run the projects on your board.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

Last updated: Mon Apr 29 2024 20:47:07 GMT-0900 (Hawaii-Aleutian Daylight Time)