NewLife/SmartA2

增加例程:蜂鸣器、USB电源、按键测试
智能大石头 authored at 2023-10-27 23:39:57
9c940db
Tree
1 Parent(s) 3d2a3a3
Summary: 11 changed files with 189 additions and 1 deletions.
Added +20 -0
Added +16 -0
Added +16 -0
Added +15 -0
Added +20 -0
Added +16 -0
Modified +2 -0
Added +16 -0
Added +14 -0
Added +20 -0
Modified +34 -1
Added +20 -0
diff --git a/Samples/BuzzerTest/BuzzerTest.csproj b/Samples/BuzzerTest/BuzzerTest.csproj
new file mode 100644
index 0000000..7c5a8fd
--- /dev/null
+++ b/Samples/BuzzerTest/BuzzerTest.csproj
@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net7.0</TargetFramework>
+    <Company>新生命开发团队</Company>
+    <Copyright>©2002-2023 新生命开发团队</Copyright>
+    <VersionPrefix>1.0</VersionPrefix>
+    <VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
+    <Version>$(VersionPrefix).$(VersionSuffix)</Version>
+    <FileVersion>$(Version)</FileVersion>
+    <AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
+    <Deterministic>false</Deterministic>
+    <OutputPath>..\..\Bin\BuzzerTest</OutputPath>
+    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+</Project>
Added +16 -0
diff --git a/Samples/BuzzerTest/OutputPort.cs b/Samples/BuzzerTest/OutputPort.cs
new file mode 100644
index 0000000..d8ed44f
--- /dev/null
+++ b/Samples/BuzzerTest/OutputPort.cs
@@ -0,0 +1,16 @@
+namespace SmartA2;
+
+/// <summary>输出口</summary>
+public class OutputPort
+{
+    /// <summary>文件路径</summary>
+    public String FileName { get; set; }
+
+    /// <summary>读取开关值</summary>
+    /// <returns></returns>
+    public Boolean Read() => File.ReadAllText(FileName)?.Trim() == "1";
+
+    /// <summary>写入开关值</summary>
+    /// <param name="value"></param>
+    public void Write(Boolean value) => File.WriteAllText(FileName, value ? "1" : "0");
+}
\ No newline at end of file
Added +16 -0
diff --git a/Samples/BuzzerTest/Program.cs b/Samples/BuzzerTest/Program.cs
new file mode 100644
index 0000000..5cc76c7
--- /dev/null
+++ b/Samples/BuzzerTest/Program.cs
@@ -0,0 +1,16 @@
+using SmartA2;
+
+var buzzer = new OutputPort { FileName = "/dev/buzzer" };
+
+for (var i = 0; i < 5; i++)
+{
+    // 响
+    buzzer.Write(true);
+
+    Thread.Sleep(500);
+
+    // 不响
+    buzzer.Write(false);
+
+    Thread.Sleep(500);
+}
\ No newline at end of file
Added +15 -0
diff --git a/Samples/KeyTest/InputPort.cs b/Samples/KeyTest/InputPort.cs
new file mode 100644
index 0000000..3a5d234
--- /dev/null
+++ b/Samples/KeyTest/InputPort.cs
@@ -0,0 +1,15 @@
+namespace SmartA2;
+
+/// <summary>输入口</summary>
+public class InputPort
+{
+    /// <summary>文件路径</summary>
+    public String FileName { get; set; }
+
+    FileStream _stream;
+    Stream GetStream() => _stream ??= File.OpenRead(FileName);
+
+    /// <summary>读取开关值</summary>
+    /// <returns></returns>
+    public Boolean Read() => GetStream().ReadByte() == '1';
+}
\ No newline at end of file
Added +20 -0
diff --git a/Samples/KeyTest/KeyTest.csproj b/Samples/KeyTest/KeyTest.csproj
new file mode 100644
index 0000000..c88657f
--- /dev/null
+++ b/Samples/KeyTest/KeyTest.csproj
@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net7.0</TargetFramework>
+    <Company>新生命开发团队</Company>
+    <Copyright>©2002-2023 新生命开发团队</Copyright>
+    <VersionPrefix>1.0</VersionPrefix>
+    <VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
+    <Version>$(VersionPrefix).$(VersionSuffix)</Version>
+    <FileVersion>$(Version)</FileVersion>
+    <AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
+    <Deterministic>false</Deterministic>
+    <OutputPath>..\..\Bin\KeyTest</OutputPath>
+    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+</Project>
Added +16 -0
diff --git a/Samples/KeyTest/Program.cs b/Samples/KeyTest/Program.cs
new file mode 100644
index 0000000..d3822a9
--- /dev/null
+++ b/Samples/KeyTest/Program.cs
@@ -0,0 +1,16 @@
+using SmartA2;
+
+var key = new InputPort { FileName = "/dev/key" };
+
+var f = false;
+for (var i = 0; i < 100; i++)
+{
+    var rs = key.Read();
+    if (rs != f)
+    {
+        f = rs;
+        Console.WriteLine(f ? "按下" : "松开");
+    }
+
+    Thread.Sleep(100);
+}
\ No newline at end of file
Modified +2 -0
diff --git a/Samples/LedTest/Program.cs b/Samples/LedTest/Program.cs
index 96cdb4a..ffc1766 100644
--- a/Samples/LedTest/Program.cs
+++ b/Samples/LedTest/Program.cs
@@ -4,10 +4,12 @@ var led = new OutputPort { FileName = "/dev/led" };
 
 while (true)
 {
+    // 灭
     led.Write(false);
 
     Thread.Sleep(500);
 
+    // 亮
     led.Write(true);
 
     Thread.Sleep(500);
Added +16 -0
diff --git a/Samples/UsbPowerTest/OutputPort.cs b/Samples/UsbPowerTest/OutputPort.cs
new file mode 100644
index 0000000..d8ed44f
--- /dev/null
+++ b/Samples/UsbPowerTest/OutputPort.cs
@@ -0,0 +1,16 @@
+namespace SmartA2;
+
+/// <summary>输出口</summary>
+public class OutputPort
+{
+    /// <summary>文件路径</summary>
+    public String FileName { get; set; }
+
+    /// <summary>读取开关值</summary>
+    /// <returns></returns>
+    public Boolean Read() => File.ReadAllText(FileName)?.Trim() == "1";
+
+    /// <summary>写入开关值</summary>
+    /// <param name="value"></param>
+    public void Write(Boolean value) => File.WriteAllText(FileName, value ? "1" : "0");
+}
\ No newline at end of file
Added +14 -0
diff --git a/Samples/UsbPowerTest/Program.cs b/Samples/UsbPowerTest/Program.cs
new file mode 100644
index 0000000..b5961a0
--- /dev/null
+++ b/Samples/UsbPowerTest/Program.cs
@@ -0,0 +1,14 @@
+using SmartA2;
+
+// USB口电源控制,可用于控制外部USB设备上电。如风扇、灯光、水泵等
+var usb = new OutputPort { FileName = "/dev/usbpwr" };
+
+// 上电
+usb.Write(true);
+
+Thread.Sleep(500);
+
+// 断电
+usb.Write(false);
+
+Thread.Sleep(500);
Added +20 -0
diff --git a/Samples/UsbPowerTest/UsbPowerTest.csproj b/Samples/UsbPowerTest/UsbPowerTest.csproj
new file mode 100644
index 0000000..5558dec
--- /dev/null
+++ b/Samples/UsbPowerTest/UsbPowerTest.csproj
@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>net7.0</TargetFramework>
+    <Company>新生命开发团队</Company>
+    <Copyright>©2002-2023 新生命开发团队</Copyright>
+    <VersionPrefix>1.0</VersionPrefix>
+    <VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
+    <Version>$(VersionPrefix).$(VersionSuffix)</Version>
+    <FileVersion>$(Version)</FileVersion>
+    <AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
+    <Deterministic>false</Deterministic>
+    <OutputPath>..\..\Bin\UsbPowerTest</OutputPath>
+    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
+    <ImplicitUsings>enable</ImplicitUsings>
+    <Nullable>enable</Nullable>
+  </PropertyGroup>
+
+</Project>
Modified +34 -1
diff --git a/SmartA2.sln b/SmartA2.sln
index 865b719..a996b22 100644
--- a/SmartA2.sln
+++ b/SmartA2.sln
@@ -22,6 +22,20 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{3DB3
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LedTest", "Samples\LedTest\LedTest.csproj", "{B1632DC0-9AFF-4034-9234-32B9A0B8F2A4}"
 EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "1.LED测试", "1.LED测试", "{315309F2-7427-4EDF-9854-105B2E24C684}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "2.蜂鸣器测试", "2.蜂鸣器测试", "{8825FC53-DDAF-4F1E-8F3C-38A85C19731A}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3.USB电源测试", "3.USB电源测试", "{8862FCEB-8460-4E40-B8D5-A91EA1F2E3BA}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4.按键测试", "4.按键测试", "{A3BF37DF-1078-41A2-82DF-4AE854096DF5}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuzzerTest", "Samples\BuzzerTest\BuzzerTest.csproj", "{88DF6E42-8776-4073-89BB-26666888BB5D}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UsbPowerTest", "Samples\UsbPowerTest\UsbPowerTest.csproj", "{A027B945-184C-4EDD-95E1-038586B60FFD}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KeyTest", "Samples\KeyTest\KeyTest.csproj", "{6BE3AB6D-88A6-4493-B6BB-C725882B991D}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -44,12 +58,31 @@ Global
 		{B1632DC0-9AFF-4034-9234-32B9A0B8F2A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{B1632DC0-9AFF-4034-9234-32B9A0B8F2A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{B1632DC0-9AFF-4034-9234-32B9A0B8F2A4}.Release|Any CPU.Build.0 = Release|Any CPU
+		{88DF6E42-8776-4073-89BB-26666888BB5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{88DF6E42-8776-4073-89BB-26666888BB5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{88DF6E42-8776-4073-89BB-26666888BB5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{88DF6E42-8776-4073-89BB-26666888BB5D}.Release|Any CPU.Build.0 = Release|Any CPU
+		{A027B945-184C-4EDD-95E1-038586B60FFD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{A027B945-184C-4EDD-95E1-038586B60FFD}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{A027B945-184C-4EDD-95E1-038586B60FFD}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{A027B945-184C-4EDD-95E1-038586B60FFD}.Release|Any CPU.Build.0 = Release|Any CPU
+		{6BE3AB6D-88A6-4493-B6BB-C725882B991D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{6BE3AB6D-88A6-4493-B6BB-C725882B991D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{6BE3AB6D-88A6-4493-B6BB-C725882B991D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{6BE3AB6D-88A6-4493-B6BB-C725882B991D}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 	EndGlobalSection
 	GlobalSection(NestedProjects) = preSolution
-		{B1632DC0-9AFF-4034-9234-32B9A0B8F2A4} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
+		{B1632DC0-9AFF-4034-9234-32B9A0B8F2A4} = {315309F2-7427-4EDF-9854-105B2E24C684}
+		{315309F2-7427-4EDF-9854-105B2E24C684} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
+		{8825FC53-DDAF-4F1E-8F3C-38A85C19731A} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
+		{8862FCEB-8460-4E40-B8D5-A91EA1F2E3BA} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
+		{A3BF37DF-1078-41A2-82DF-4AE854096DF5} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0}
+		{88DF6E42-8776-4073-89BB-26666888BB5D} = {8825FC53-DDAF-4F1E-8F3C-38A85C19731A}
+		{A027B945-184C-4EDD-95E1-038586B60FFD} = {8862FCEB-8460-4E40-B8D5-A91EA1F2E3BA}
+		{6BE3AB6D-88A6-4493-B6BB-C725882B991D} = {A3BF37DF-1078-41A2-82DF-4AE854096DF5}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {323831A1-A95B-40AB-B9AD-36A0BC10C2CB}