Skip to content

Commit 3b6cd0e

Browse files
authored
Merge pull request #4 from madmachineio/comment-modification
Add license and modify line-wrapping
2 parents 61152e1 + e1eae7e commit 3b6cd0e

File tree

9 files changed

+353
-127
lines changed

9 files changed

+353
-127
lines changed

Sources/BH1750/BH1750.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
import SwiftIO
1515

16-
/// This is the library for the BH1750 light sensor.
16+
/// This is the library for the BH1750 light sensor
17+
/// used to measure light intensity.
1718
///
1819
/// The sensor communicates with your board via an I2C bus.
1920
/// It provides 16-bit resolution to sense the amount of ambiant light.

Sources/HCSR04/HCSR04.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,41 @@
1+
//=== HCSR04.swift --------------------------------------------------------===//
2+
//
3+
// Copyright (c) MadMachine Limited
4+
// Licensed under MIT License
5+
//
6+
// Authors: Ines Zhou
7+
// Created: 10/21/2021
8+
// Updated: 10/26/2021
9+
//
10+
// See https://madmachine.io for more information
11+
//
12+
//===----------------------------------------------------------------------===//
13+
114
import SwiftIO
215

3-
/// This is the library for HCSR40 ultrasonic sensor.
16+
/// This is the library for HCSR40 ultrasonic sensor used to measure distance.
417
///
5-
/// During its work, the sensor will send an ultrasonic signal and receive the reflected signal. You can get a round-trip time. The sound speed in the air is 343m/s. Then you can calculate the distance in-between.
18+
/// During its work, the sensor will send an ultrasonic signal and
19+
/// receive the reflected signal. You can get a round-trip time.
20+
/// The sound speed in the air is 343m/s.
21+
/// Then you can calculate the distance in-between.
622
///
7-
/// - Attention: Make sure to use the sensor that requires 3.3V, or it might return a wrong value and even do damage to your board.
23+
/// - Attention: Make sure to use the sensor that requires 3.3V,
24+
/// or it might return a wrong value and even do damage to your board.
825
final public class HCSR40 {
9-
let trig: DigitalOut
10-
let echo: DigitalIn
11-
let timeout: Float
26+
private let trig: DigitalOut
27+
private let echo: DigitalIn
28+
private let timeout: Float
1229

1330
/// Initialize an ultrasonic sensor.
14-
/// You need to use two digital pins to send and receive signal. Then you set a maximum time to wait for the signal to avoid too long a distance or any unexpected situation.
31+
/// You need to use two digital pins to send and receive signal.
32+
/// Then you set a maximum time to wait for the signal to
33+
/// avoid too long a distance or any unexpected situation.
1534
/// - Parameters:
1635
/// - trig: **REQUIRED** A DigitalOut pin used to output a pulse.
1736
/// - echo: **REQUIRED** A DigitalIn pin used to read the pulse.
18-
/// - timeout: **OPTIONAL** A preset time to wait for the response. 0.1s by default.
37+
/// - timeout: **OPTIONAL** A preset time to wait for the response.
38+
/// 0.1s by default.
1939
public init(trig: DigitalOut, echo: DigitalIn, timeout: Float = 0.1) {
2040
self.trig = trig
2141
self.echo = echo
@@ -34,15 +54,17 @@ final public class HCSR40 {
3454

3555
while !echo.read() {
3656
let currentTime = getClockCycle()
37-
let time = Float(cyclesToNanoseconds(start: start, stop: currentTime)) / 1000000000
57+
let time = Float(cyclesToNanoseconds(
58+
start: start, stop: currentTime)) / 1_000_000_000
3859
if time > timeout {
39-
return nil
60+
return nil
4061
}
4162
}
4263

4364
while echo.read() {
4465
let currentTime = getClockCycle()
45-
let time = Float(cyclesToNanoseconds(start: start, stop: currentTime)) / 1000000000
66+
let time = Float(cyclesToNanoseconds(
67+
start: start, stop: currentTime)) / 1_000_000_000
4668
distance = time * 343 / 2.0
4769
}
4870
return distance

Sources/IS31FL3731/IS31FL3731.swift

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1+
//=== IS31FL3731.swift ----------------------------------------------------===//
2+
//
3+
// Copyright (c) MadMachine Limited
4+
// Licensed under MIT License
5+
//
6+
// Authors: Andy Liu
7+
// Created: 07/29/2021
8+
// Updated: 10/26/2021
9+
//
10+
// See https://madmachine.io for more information
11+
//
12+
//===----------------------------------------------------------------------===//
13+
114
import SwiftIO
215

316
#if canImport(MadDisplay)
417
import struct MadDisplay.ColorSpace
518
#endif
619

720
/**
8-
This is the library for IS31FL3731 chip. It supports I2C communication. By default, you can use it with a 9x16 LED matrix.
9-
10-
The LED matrix has 16 rows (X0 - X15), each row has 9 LEDs (Y0 - Y8). One LED stands for one pixel. The first LED labeled X0 and Y0 is the origin. There are two ways to indicate it: 0 or (0,0). Each LED has 8-bit grayscale, that is, its brightness has 256 levels. 0 is off and 255 is full brightness.
21+
This is the library for IS31FL3731 chip. It supports I2C communication.
22+
By default, you can use it with a 9x16 LED matrix.
1123

12-
You can also regard the LED matrix as a 9x16 screen and use `MadDisplay` to display text on it.
24+
The LED matrix has 16 rows (X0 - X15), each row has 9 LEDs (Y0 - Y8).
25+
One LED stands for one pixel. The first LED labeled X0 and Y0 is the origin.
26+
There are two ways to indicate it: 0 or (0,0).
27+
Each LED has 8-bit grayscale, that is, its brightness has 256 levels.
28+
0 is off and 255 is full brightness.
1329

14-
- Attention: If you set all pixels to full brightness, the module will require too much current. So you may need to connect an external power supply instead of the computer's USB port. And if you hear buzzing from it, don't worry, it's because it works quickly to switch LED on and off.
30+
You can also regard the LED matrix as a 9x16 screen and
31+
use `MadDisplay` to display text on it.
1532

33+
- Attention: If you set all pixels to full brightness, the module will require
34+
too much current. So you may need to connect an external power supply
35+
instead of the computer's USB port. And if you hear buzzing from it,
36+
don't worry, it's because it works quickly to switch LED on and off.
1637
*/
1738

1839
public final class IS31FL3731 {
@@ -40,7 +61,7 @@ public final class IS31FL3731 {
4061
/**
4162
Initialize the module to get it ready for lighting.
4263
- Parameter i2c: **REQUIRED** The I2C interface that the module connects.
43-
- Parameter address: **OPTIONAL** The address of the module. It has a default value.
64+
- Parameter address: **OPTIONAL** The address of the module.
4465
*/
4566
public init(i2c: I2C, address: UInt8 = 0x74) {
4667

@@ -103,10 +124,12 @@ public final class IS31FL3731 {
103124
}
104125

105126
/**
106-
Light an LED by telling its number from 0 to 143. The brightness of the LED can range from 0 (off) to 255 (full brightness).
127+
Light an LED by telling its number from 0 to 143.
128+
The brightness of the LED can range from 0 (off) to 255 (full brightness).
107129

108130
- Parameter number: **REQUIRED** The location of the LED from 0 to 143.
109-
- Parameter brightness: **REQUIRED** The brightness of the specified LED. By default, the LED is set to full brightness.
131+
- Parameter brightness: **REQUIRED** The brightness of the specified LED.
132+
By default, the LED is set to full brightness.
110133
*/
111134
@inline(__always)
112135
public func writePixel(_ number: Int, brightness: UInt8 = 255) {
@@ -118,11 +141,14 @@ public final class IS31FL3731 {
118141
/**
119142
Light an LED by telling its coordinates.
120143

121-
You can know the coordinate from the labels printed on the module. The x is from 0 to 15 and y is from 0 to 8. Each LED can have 256 levels of brightness from 0 (off) to 255 (full brightness).
144+
You can know the coordinate from the labels printed on the module.
145+
The x is from 0 to 15 and y is from 0 to 8. Each LED can have 256 levels
146+
of brightness from 0 (off) to 255 (full brightness).
122147

123148
- Parameter x: **REQUIRED** The x coordinate of the LED.
124149
- Parameter y: **REQUIRED** The y coordinate of the LED.
125-
- Parameter brightness: **REQUIRED** The brightness of the specified LED. By default, the LED is set to full brightness.
150+
- Parameter brightness: **REQUIRED** The brightness of the specified LED.
151+
By default, the LED is set to full brightness.
126152
*/
127153
@inline(__always)
128154
public func writePixel(x: Int, y: Int, brightness: UInt8 = 255) {
@@ -133,16 +159,25 @@ public final class IS31FL3731 {
133159
}
134160

135161
/**
136-
Set a part of the pixels on the matrix by defining the area. The area is a rectangle determined by a starting point, width and height. Then you can set the pixels to any brightness to get a unique lighting effect.
162+
Set a part of the pixels on the matrix by defining the area.
163+
The area is a rectangle determined by a starting point, width and height.
164+
Then you can set the pixels to any brightness to get a unique
165+
lighting effect.
137166

138-
- Parameter x: **REQUIRED** The horizontal line of the matrix to decide the start point, from 0 to 15.
139-
- Parameter y: **REQUIRED** The vertical line of the matrix to decide the start point, from 0 to 8.
140-
- Parameter width: **REQUIRED** The number of pixels in the horizontal direction.
141-
- Parameter height: **REQUIRED** The number of pixels in the vertical direction.
142-
- Parameter brightness: **REQUIRED** The brightness level of all the pixels. Each byte stands for the brightness of each pixel
167+
- Parameter x: **REQUIRED** The horizontal line of the matrix to decide
168+
the start point, from 0 to 15.
169+
- Parameter y: **REQUIRED** The vertical line of the matrix to decide
170+
the start point, from 0 to 8.
171+
- Parameter width: **REQUIRED** The number of pixels horizontally.
172+
- Parameter height: **REQUIRED** The number of pixels vertically.
173+
- Parameter brightness: **REQUIRED** The brightness level of all pixels.
174+
Each byte stands for the brightness of each pixel.
143175
*/
144-
public func writeBitmap(x: Int, y: Int, width: Int, height: Int, data: [UInt8]) {
145-
guard x < self.width && y < self.height && width >= 1 && height >= 1 else {
176+
public func writeBitmap(x: Int, y: Int, width: Int,
177+
height: Int, data: [UInt8]
178+
) {
179+
guard x < self.width && y < self.height
180+
&& width >= 1 && height >= 1 else {
146181
return
147182
}
148183

@@ -176,7 +211,8 @@ public final class IS31FL3731 {
176211

177212

178213
/// Set all pixels to a specified brightness.
179-
/// - Parameter brightness: **REQUIRED** The value between 0 and 255 to set the brightness. By default, all pixels are set to 0.
214+
/// - Parameter brightness: **REQUIRED** The value between 0 and 255
215+
/// to set the brightness. By default, all pixels are set to 0.
180216
public func fill(_ brightness: UInt8 = 0x00) {
181217
var data = [UInt8](repeating: brightness, count: 144 + 1)
182218
data[0] = pwmRegOffset

Sources/LCD1602/LCD1602.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
//=== LCD1602.swift -------------------------------------------------------===//
2+
//
3+
// Copyright (c) MadMachine Limited
4+
// Licensed under MIT License
5+
//
6+
// Authors: Andy Liu
7+
// Created: 06/13/2021
8+
// Updated: 10/26/2021
9+
//
10+
// See https://madmachine.io for more information
11+
//
12+
//===----------------------------------------------------------------------===//
13+
114
import SwiftIO
215

316
final public class LCD1602 {
@@ -67,9 +80,11 @@ final public class LCD1602 {
6780
private var entryModeConfig: EntryMode
6881
private var shiftModeConfig: ShiftMode
6982

70-
public init(_ i2c: I2C, address: UInt8 = 0x3E, columns: UInt8 = 16, rows: UInt8 = 2, dotSize: UInt8 = 8) {
83+
public init(_ i2c: I2C, address: UInt8 = 0x3E,
84+
columns: UInt8 = 16, rows: UInt8 = 2, dotSize: UInt8 = 8) {
7185

72-
guard (columns > 0) && (rows == 1 || rows == 2) && (dotSize == 8 || dotSize == 10) else {
86+
guard (columns > 0) && (rows == 1 || rows == 2)
87+
&& (dotSize == 8 || dotSize == 10) else {
7388
fatalError("LCD1602 parameter error, init failed")
7489
}
7590

Sources/LIS3DH/LIS3DH.swift

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,49 @@
1+
//=== LIS3DH.swift --------------------------------------------------------===//
2+
//
3+
// Copyright (c) MadMachine Limited
4+
// Licensed under MIT License
5+
//
6+
// Authors: Andy Liu
7+
// Created: 05/11/2021
8+
// Updated: 10/26/2021
9+
//
10+
// See https://madmachine.io for more information
11+
//
12+
//===----------------------------------------------------------------------===//
13+
114
import SwiftIO
215

3-
/// This is the library for the LIS3DH accelerometer. You can use the sensor to measure the accelerations in x, y, and z-axes.
16+
/// This is the library for the LIS3DH accelerometer.
17+
/// You can use the sensor to measure the accelerations in x, y, and z-axes.
418
///
5-
/// The acceleration describes the change of velocity with time, usually measured in m/s^2. The sensor measures it by detecting the force. It can sense gravity and measure inertial force caused by movement. They will change the internal capacitance of the sensor, thus change the voltage in the circuit.
19+
/// The acceleration describes the change of velocity with time,
20+
/// usually measured in m/s^2. The sensor measures it by detecting the force.
21+
/// It can sense gravity and measure inertial force caused by movement.
22+
/// They will change the internal capacitance of the sensor,
23+
/// thus change the voltage in the circuit.
624
///
7-
/// The sensor supports I2C and SPI protocol. It will give raw readings between -32768 and 32767 (16-bit resolution). The acceleration has direction so you will get positive or negative values. The calculation of acceleration depends on the selected scaling range: ±2, ±4, ±8 or ±16g. The raw reading will be mapped according to the range.
25+
/// The sensor supports I2C and SPI protocol.
26+
/// It will give raw readings between -32768 and 32767 (16-bit resolution).
27+
/// The acceleration has direction so you will get positive or negative values.
28+
/// The calculation of acceleration depends on the selected scaling range:
29+
/// ±2, ±4, ±8 or ±16g. The raw reading will be mapped according to the range.
830
final public class LIS3DH {
931

1032

11-
/// The ranges of the measurement. It will decide how the raw reading is calculated.
33+
/// The ranges of the measurement.
1234
public enum GRange: UInt8 {
35+
/// The acceleration is from -2g to 2g. It is the default setting.
1336
case g2 = 0
37+
/// The acceleration is from -4g to 4g.
1438
case g4 = 0b0001_0000
39+
/// The acceleration is from -8g to 8g.
1540
case g8 = 0b0010_0000
41+
/// The acceleration is from -16g to 16g.
1642
case g16 = 0b0011_0000
1743
}
1844

1945

20-
/// The supported data rate for the sensor.
46+
/// The supported data rate for the sensor, 400Hz by default.
2147
public enum DataRate: UInt8 {
2248
case powerDown = 0
2349
case Hz1 = 0b0001_0000
@@ -59,7 +85,7 @@ final public class LIS3DH {
5985
/// Initialize the sensor using I2C communication.
6086
/// - Parameters:
6187
/// - i2c: **REQUIRED** The I2C interface that the sensor connects.
62-
/// - address: **OPTIONAL** The device address of the sensor. It has a default value.
88+
/// - address: **OPTIONAL** The device address of the sensor.
6389
public init(_ i2c: I2C, address: UInt8 = 0x18) {
6490
self.i2c = i2c
6591
self.address = address
@@ -77,13 +103,15 @@ final public class LIS3DH {
77103
}
78104

79105

80-
/// Get the device ID from the sensor. It can be used to test if the sensor is connected.
106+
/// Get the device ID from the sensor.
107+
/// It can be used to test if the sensor is connected.
81108
/// - Returns: The device ID.
82109
public func getDeviceID() -> UInt8 {
83110
return readRegister(.WHO_AM_I)
84111
}
85112

86-
/// Set the scaling range of the sensor. The supported ranges are ±2, ±4, ±8 and ±16g.
113+
/// Set the scaling range of the sensor.
114+
/// The supported ranges are ±2, ±4, ±8 and ±16g.
87115
/// - Parameter newRange: The selected `GRange`.
88116
public func setRange(_ newRange: GRange) {
89117
gRange = newRange
@@ -132,11 +160,13 @@ final public class LIS3DH {
132160
}
133161

134162

135-
/// Read x, y, z acceleration values represented in g (9.8m/s^2) within the selected range.
163+
/// Read x, y, z acceleration values represented in g (9.8m/s^2)
164+
/// within the selected range.
136165
/// - Returns: 3 float within the selected g range.
137166
public func readValue() -> (x: Float, y: Float, z: Float) {
138167
let (ix, iy, iz) = readRawValue()
139-
var value: (x: Float, y: Float, z: Float) = (Float(ix), Float(iy), Float(iz))
168+
var value: (x: Float, y: Float, z: Float) =
169+
(Float(ix), Float(iy), Float(iz))
140170

141171
value.x = value.x / gCoefficient
142172
value.y = value.y / gCoefficient

0 commit comments

Comments
 (0)