Skip to content

Commit

Permalink
Auto-generate devices listing (#337)
Browse files Browse the repository at this point in the history
* Auto-generate devices listing

* change namespace, do not throw NRE when outside of repo

* handle root directory correctly
  • Loading branch information
krwq authored Mar 18, 2019
1 parent 983fbe4 commit b7f0358
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/devices/Bmx280/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# BMx280 Sensors
# BMx280 - Digital Pressure Sensors BMP280/BME280

## Summary

Expand Down
9 changes: 1 addition & 8 deletions src/devices/Buzzer/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@

# Buzzer


# Buzzer - Piezo Buzzer Controller

## Summary

This device binding allows playing certain tones using piezo buzzer. It uses PWM with 50% duty cycle and various frequencies.

Piezo buzzers with three pins supported as well as piezo buzzers with two pins.



## Device Family

This binding was tested on two types of piezo buzzers. First type of buzzer has two pins *vcc* and *gnd*. Second type of buzzers has addition *signal* pin.



## Binding Notes

Expand Down
2 changes: 1 addition & 1 deletion src/devices/CharacterLcd/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Character LCD Display
# Character LCD (Liquid Crystal Display)

## Summary
This device binding is meant to work with character LCD displays which use a HD44780 compatible controller. Almost all character LCDs fall into this category. Simple wrappers for 16x2 and 20x4 variants are included.
Expand Down
2 changes: 1 addition & 1 deletion src/devices/Dhtxx/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# DHTxx Sensors
# DHTxx - Digital-Output Relative Humidity & Temperature Sensor Module

## Summary

Expand Down
2 changes: 1 addition & 1 deletion src/devices/Mcp23xxx/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Microchip Mcp23xxx
# Mcp23xxx - I/O Expander

## Summary
The MCP23XXX device family provides 8/16-bit, general purpose parallel I/O expansion for I2C or SPI applications. These devices include a range of addressing schemes and I/O configurations including pull-up resistors, polarity inverting, and interrupts.
Expand Down
2 changes: 1 addition & 1 deletion src/devices/Mpr121/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MPR121
# MPR121 - Proximity Capacitive Touch Sensor Controller

## Summary
The 12-channels I2C proximity capacitive touch sensor controller.
Expand Down
2 changes: 1 addition & 1 deletion src/devices/Pca95x4/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Pca95x4
# Pca95x4 - I2C GPIO Expander

## Summary
The PCA95x4 provides 8 bits of General Purpose parallel Input/Output (GPIO) expansion for I2C-bus applications.
Expand Down
2 changes: 1 addition & 1 deletion src/devices/Pca9685/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Pca9685
# Pca9685 - I2C PWM Driver

## Summary

Expand Down
66 changes: 38 additions & 28 deletions src/devices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,45 @@ Our vision: the majority of .NET bindings are written completely in .NET languag

## Binding Index

* [ADS1115 -- Analog-to-Digital Converter](Ads1115/README.md)
* [ADXL345 -- SPI Accelerometer](Adxl345/README.md)
* [AGS01DB -- MEMS VOC Gas Sensor ASG01DB](Ags01db/README.md)
* [BMx280 -- Digital Pressure Sensors BMP280/BME280 ](Bmx280/README.md)
<devices>
* [Pca95x4 - I2C GPIO Expander](Pca95x4/README.md)
* [Using MCP3008 (10-bit Analog to Digital Converter)](Mcp3008/README.md)
* [BMO055 Sensors](Bno055/README.md)
* [GoPiGo3](GoPiGo3/README.md)
* [DS3231 - Realtime Clock](Ds3231/README.md)
* [nRF24L01 - Single Chip 2.4 GHz Transceiver](Nrf24l01/README.md)
* [Using Max7219 (LED Matrix driver)](Max7219/README.md)
* [Si7021 - Temperature & Humidity Sensor](Si7021/README.md)
* [Ws28xx LED drivers](Ws28xx/README.md)
* [28BYJ-48 Stepper Motor 5V 4-Phase 5-Wire & ULN2003 Driver Board](Uln2003/README.md)
* [LPS25H - Piezoresistive pressure and thermometer sensor](Lps25h/README.md)
* [HTS221 - Capacitive digital sensor for relative humidity and temperature ](Hts221/README.md)
* [Mcp23xxx - I/O Expander](Mcp23xxx/README.md)
* [HC-SR501 - PIR Motion Sensor](Hcsr501/README.md)
* [BMx280 - Digital Pressure Sensors BMP280/BME280](Bmx280/README.md)
* [SHT3x - Temperature & Humidity Sensor](Sht3x/README.md)
* [Pca9685 - I2C PWM Driver](Pca9685/README.md)
* [Solomon Systech Ssd1306](Ssd1306/README.md)
* [HC-SR04 - Ultrasonic Ranging Module](Hcsr04/README.md)
* [LM75 - Digital Temperature Sensor](Lm75/README.md)
* [MPR121 - Proximity Capacitive Touch Sensor Controller](Mpr121/README.md)
* [Software PWM](SoftPwm/README.md)
* [DC Motor Controller](DCMotor/README.md)
* [LSM9DS1 - 3D accelerometer, gyroscope and magnetometer](Lsm9Ds1/README.md)
* [ADS1115 - Analog to Digital Converter](Ads1115/README.md)
* [AGS01DB - MEMS VOC Gas Sensor](Ags01db/README.md)
* [Cpu Temperature](CpuTemperature/README.md)
* [Sense HAT](SenseHat/README.md)
* [BrickPi3](BrickPi3/README.md)
* [Buzzer -- Piezo Buzzer Controller](Buzzer/README.md)
* [CharacterLcd -- Liquid Crystal Display](CharacterLcd/README.md)
* [DC Motor](DCMotor/README.md)
* [Dhtxx -- Digital-Output Relative Humidity & Temperature Sensor Module](Dhtxx/README.md)
* [DS3231 -- Realtime Clock](Ds3231/README.md)
* [HC-SR04 -- Ultrasonic Ranging Module](Hcsr04/README.md)
* [HC-SR501 -- PIR Motion Sensor](Hcsr501/README.md)
* [HMC5883L -- 3-Axis Digital Compass](Hmc5883l/README.md)
* [LM75 -- Digital Temperature Sensor](Lm75/README.md)
* [Max7219 -- LED Matrix Driver](Max7219/README.md)
* [MAX44009 -- Ambient Light Sensor](Max44009/README.md)
* [Mcp23xxx -- I/O Expander](Mcp23xxx/README.md)
* [Mcp3008 -- Analog-to-Digital Converter](Mcp3008/README.md)
* [Mpr121 -- Proximity Capacitive Touch Sensor Controller](Mpr121/README.md)
* [nRF24L01 -- Single chip 2.4 GHz Transceiver](Nrf24l01/README.md)
* [Pca95x4 -- I2C GPIO Expander](Pca95x4/README.md)
* [Pca9685 -- I2C PWM Driver](Pca9685/README.md)
* [Pcx857x -- I2C GPIO Expander](Pcx857x/README.md)
* [Servo -- Servomotor Controller](Servo/README.md)
* [SHT3x -- Humidity and Temperature Sensor](Sht3x/README.md)
* [Si7021 -- Temperature & Humidity Sensor](Si7021/README.md)
* [SoftPwm -- Software PWM](SoftPwm/README.md)
* [Ssd1306 -- OLED Display Controller](Ssd1306/README.md)
* [Uln2003 -- Driver board for stepper motor](Uln2003/README.md)
* [NXP/TI PCx857x](Pcx857x/README.md)
* [Character LCD (Liquid Crystal Display)](CharacterLcd/README.md)
* [HMC5883L - 3 Axis Digital Compass](Hmc5883l/README.md)
* [MAX44009 - Ambient Light Sensor](Max44009/README.md)
* [Buzzer - Piezo Buzzer Controller](Buzzer/README.md)
* [Servomotor](Servo/README.md)
* [ADXL345 - Accelerometer](Adxl345/README.md)
* [DHTxx - Digital-Output Relative Humidity & Temperature Sensor Module](Dhtxx/README.md)
</devices>

## Binding Distribution

Expand Down
4 changes: 4 additions & 0 deletions tools/device-listing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Device listing generator

Run `dotnet run` on this project to auto-generate description in `src/devices/README.md`.

135 changes: 135 additions & 0 deletions tools/device-listing/device-listing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.IO;
using System.Text;
using System.Threading;

namespace Iot.Tools.DeviceListing
{
class Program
{
static void Main(string[] args)
{
string repoRoot = FindRepoRoot(Environment.CurrentDirectory);

if (repoRoot == null)
{
Console.WriteLine("Error: not in a git repository");
return;
}

string devices = Path.Combine(repoRoot, "src", "devices");

var deviceListing = new StringBuilder();

foreach (string directory in Directory.EnumerateDirectories(devices))
{
if (IsIgnoredDevice(directory))
{
continue;
}

string readme = Path.Combine(directory, "README.md");
if (File.Exists(readme))
{
string title = GetTitle(readme);
if (title == null)
{
Console.WriteLine($"Directory `{directory}` contains readme file without title on the first line.");
continue;
}

deviceListing.AppendLine($"* [{title}]({GetRelativePathSimple(readme, devices)})");
}
else
{
Console.WriteLine($"Directory `{directory}` does not have a README.md file.");
}
}

ReplacePlaceholder(Path.Combine(devices, "README.md"), "devices", deviceListing.ToString());
}

private static string FindRepoRoot(string dir)
{
if (!string.IsNullOrEmpty(dir))
{
if (Directory.Exists(Path.Combine(dir, ".git")))
{
return dir;
}
else
{
DirectoryInfo parentDir = new DirectoryInfo(dir).Parent;
return parentDir == null ? null : FindRepoRoot(parentDir.FullName);
}
}

return null;
}

private static string GetTitle(string readmePath)
{
string[] lines = File.ReadAllLines(readmePath);
if (lines[0].StartsWith("# "))
{
return lines[0].Substring(2);
}

return null;
}

// simple means it won't try to use ".."
private static string GetRelativePathSimple(string path, string parentPath)
{
if (path.StartsWith(parentPath))
{
int i = parentPath.Length;
if (path[i] == '/' || path[i] == '\\')
{
i++;
}

return path.Substring(i);
}
else
{
throw new Exception($"No common path between `{path}` and `{parentPath}`");
}
}

private static bool IsIgnoredDevice(string path)
{
string dirName = new DirectoryInfo(path).Name;
return dirName == "Common" || dirName == "Units";
}

private static void ReplacePlaceholder(string filePath, string placeholderName, string newContent)
{
string fileContent = File.ReadAllText(filePath);

string startTag = $"<{placeholderName}>";
string endTag = $"</{placeholderName}>";

int startIdx = fileContent.IndexOf(startTag);
int endIdx = fileContent.IndexOf(endTag);

if (startIdx == -1 || endIdx == -1)
{
throw new Exception($"`{startTag}` not found in `{filePath}`");
}

startIdx += startTag.Length;

File.WriteAllText(
filePath,
fileContent.Substring(0, startIdx) +
Environment.NewLine +
newContent +
fileContent.Substring(endIdx));
}
}
}
8 changes: 8 additions & 0 deletions tools/device-listing/device-listing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

</Project>

0 comments on commit b7f0358

Please sign in to comment.