百度人脸产品套件-壁虎 3.继电器控制
goJhou 发布于2019-11-01 22:31 浏览:471 回复:2
0
收藏

本篇功能演示

https://jhou-bucket.oss-cn-shanghai.aliyuncs.com/2019-10-18%20%E5%A3%81%E8%99%8E%E6%B5%8B%E8%AF%84/2019-10-25%20%E7%BB%A7%E7%94%B5%E5%99%A8%E6%8E%A7%E5%88%B6/1.mp4

 

本章开篇前,我对上一章的部分代码进行了重构

我将单例模式放到了BaseController中

现在BaseController如下:

[AddINotifyPropertyChangedInterface]
    public class BaseController : INotifyPropertyChanged where T:new()
    {
        private static T instance;

        public static T Instance
        {
            get
            {
                if (instance == null)
                    instance = new T();
                return instance;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string PropertyName="")
        {
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

 

LightController如下:

public class LightController : BaseController
    {
        public LightController()
        {
            Brightness = 0;
        }

        private readonly string IOPath = "/sys/class/leds/wled/brightness";

        private int _brightness = 0;

        public int Brightness
        {
            get => _brightness;
            set
            {
                try
                {
                    value = value & 0xff;

                    byte[] b = Encoding.UTF8.GetBytes(value.ToString());

                    using (FileStream fs = new FileStream(IOPath, FileMode.Create))
                    {
                        fs.Write(b, 0, b.Length);
                    }

                    _brightness = value;
                }
                catch (Exception ex)
                {
                    Debugger.Break();
                }
            }
        }
    }

 

正篇开始

本篇打算继上一篇的进度,继续进行外设的测试,接下去测试两组继电器。位于壁虎的最下侧接线端上。

点我查看外设物理定义 ( https://ai.baidu.com/docs#/Face_Kit_Gekko_Interface/dce538a9 )

 

那本篇主要还是测试软件,因为继电器切换是有哒哒声,所以控制成功与否一听就知道啦
(继电器开机会重置(瞬开一次),注意默认开机后是关闭态,也就是外设接口不通的状态)

首先,仿照LightController书写RelayController

public class RelayController : BaseController
    {
        private readonly string Relay1Path = "/sys/exgpio/relay1";
        private readonly string Relay2Path = "/sys/exgpio/relay2";

        private bool relay1IsOpen = false;
        public bool Relay1IsOpen
        {
            get => relay1IsOpen;
            set
            {
                try
                {
                    if (relay1IsOpen == value)
                        return;

                    string target = "0";
                    if (value)
                        target = "1";

                    byte[] b = Encoding.UTF8.GetBytes(target.ToString());

                    using (FileStream fs = new FileStream(Relay1Path, FileMode.Create))
                    {
                        fs.Write(b, 0, b.Length);
                    }
                    relay1IsOpen = value;
                }
                catch
                {
                    Debugger.Break();
                }
            }
        }

        private bool relay2IsOpen = false;
        public bool Relay2IsOpen
        {
            get => relay2IsOpen;
            set
            {
                try
                {
                    if (relay2IsOpen == value)
                        return;

                    string target = "0";
                    if (value)
                        target = "1";

                    byte[] b = Encoding.UTF8.GetBytes(target.ToString());

                    using (FileStream fs = new FileStream(Relay2Path, FileMode.Create))
                    {
                        fs.Write(b, 0, b.Length);
                    }
                    relay2IsOpen = value;
                }
                catch
                {
                    Debugger.Break();
                }
            }
        }

        ~RelayController()
        {
            Relay1IsOpen = Relay2IsOpen = false;
        }
    }

 

然后开始重构UI部分

后台代码添加一个新的控制器

 

public partial class MainPage : ContentPage
    {
        public LightController LightController => LightController.Instance;

        public RelayController RelayController => RelayController.Instance;
        public MainPage()
        {
            InitializeComponent();
        }
    }

 

前端则重新对UI进行重构,添加新的UI。这里用switch来表达继电器的开关状态


    
        
            
            
        
        
            
            
                
                    
                    
                
                
            
            
            
            
                
                    
                    
                
                
                    
                    
                
                
                
                
                
            
        

        
        
            
            
        
    

因为与灯光控制雷同,本篇的篇幅就稍微简单粗暴一些~

收藏
点赞
0
个赞
共2条回复 最后由goJhou回复于2019-11-06 19:50
#3goJhou回复于2019-11-06 19:50:25
#2 费芮网络回复
有python代码吗

安卓套件喔

0
#2费芮网络回复于2019-11-06 18:08:26

有python代码吗

0
TOP
切换版块