Beispiel #1
0
        public GpioInterruptMonitor(Gpio gpio)
        {
            _gpio = gpio;

            // Started as pull up.
            _currentState = GpioState.High;
        }
 public void WriteState(int gpioId, GpioState state)
 {
     lock (_syncRoot)
     {
         WrtieGpioState(gpioId, state);
     }
 }
 public void WriteState(int gpioId, GpioState state)
 {
     lock (_syncRoot)
     {
         var fileContent = state == GpioState.Low ? "0" : "1";
         File.WriteAllText("/sys/class/gpio/gpio" + gpioId + "/value", fileContent, Encoding.ASCII);
     }
 }
Beispiel #4
0
 protected virtual void onGpioStateChanged(GpioState state)
 {
     if (IsSubscribed(state.PinId))
     {
         saveState(state);
         Logger?.LogInformation($"[GpioStateChanged]: {state}");
     }
 }
 public void NotifyGpioStateChanged(int gpioId, GpioState oldState, GpioState newState)
 {
     GpioStateChanged?.Invoke(this, new GpioAdapterStateChangedEventArgs
     {
         GpioId   = gpioId,
         OldState = oldState,
         NewState = newState
     });
 }
Beispiel #6
0
        public void WriteState(string hostId, int gpioId, GpioState state)
        {
            if (hostId == null)
            {
                throw new ArgumentNullException(nameof(hostId));
            }

            GetAdapter(hostId).WriteState(gpioId, state);
        }
Beispiel #7
0
 /// <summary>
 /// Changes the internal state of a GPIO channel
 /// </summary>
 /// <param name="id">The GPIO ID</param>
 /// <param name="state">The state to change to</param>
 private void ChangeState(string id, GpioState state)
 {
     if (this.chanelStates.ContainsKey(id))
     {
         this.chanelStates[id] = state;
     }
     else
     {
         this.chanelStates.Add(id, state);
     }
 }
Beispiel #8
0
        override protected async Task <GpioState> readPinAsync(int pinId)
        {
            GpioState result = null;
            GpioPin   pin    = controller.GetGpioPinByBcmPinNumber(pinId);

            if (pin != null)
            {
                result       = new GpioState(pinId, 0, DateTime.Now, true);
                result.Value = await pin.ReadLevelAsync();
            }
            return(result);
        }
Beispiel #9
0
        private void DispatchGpioStateChangedEvent(string gpioHostId, int gpioId, GpioState oldState, GpioState newState)
        {
            var properties = new WirehomeDictionary
            {
                ["type"]         = "gpio_registry.event.state_changed",
                ["gpio_host_id"] = gpioHostId,
                ["gpio_id"]      = gpioId,
                ["old_state"]    = oldState.ToString().ToLowerInvariant(),
                ["new_state"]    = newState.ToString().ToLowerInvariant()
            };

            _messageBusService.Publish(properties);
        }
        override protected async Task <GpioState> readPinAsync(int pinId)
        {
            GpioState result = new GpioState(pinId, 0);
            double    dt     = (DateTime.Now - startTime).TotalMilliseconds;
            double    period = 5000;
            double    phase  = (dt % period) / period;
            double    signal = (double)GpioState.MaxValue * (0.5d + Math.Sin(phase * 2 * Math.PI) / 2d);
            double    noise  = (rnd.NextDouble() - .5) * NoiseLevel;

            result.Value = (int)(signal * (1 + noise));
            await Task.Delay(5);

            return(result);
        }
Beispiel #11
0
        public bool Poll(out GpioState oldState, out GpioState newState)
        {
            oldState = _currentState;
            newState = _gpio.Read();

            if (newState == _currentState)
            {
                return(false);
            }

            _currentState = newState;

            return(true);
        }
Beispiel #12
0
 public void Write(GpioState state)
 {
     using (var fileStream = File.OpenWrite(_valuePath))
     {
         if (state == GpioState.Low)
         {
             // ASCII '0' is DEC 48.
             fileStream.WriteByte(48);
         }
         else
         {
             // ASCII '1' is DEC 49.
             fileStream.WriteByte(49);
         }
     }
 }
Beispiel #13
0
        //public GpioStateHub(ILogger log) : base(log) { }

        public async Task Broadcast(DateTime timestamp, int pinId, bool isAnalog, int value)
        {
            GpioState st = new GpioState()
            {
                PinId = pinId, IsAnalog = isAnalog, Value = value
            };

            logger.LogDebug($"Broadcasting to all other clients ...");
            IHubCallerClients hubClients = Clients;
            IClientProxy      clients    = Clients.All;
            IClientProxy      all        = Clients.All;
            IClientProxy      others     = Clients.Others;

            logger.LogDebug($"[GPIOState.Hub]; {st}");
            await others.SendAsync("GpioState", timestamp, pinId, isAnalog, value);
        }
 protected virtual void saveState(GpioState state)
 {
     if (!ValidateState(state))
     {
         Logger?.LogWarning("Invalid GpioState, skipping save");
         return;
     }
     if (!_history.ContainsKey(state.PinId))
     {
         _history.Add(state.PinId, new List <GpioState>());
     }
     if (!SaveHistory)
     {
         _history[state.PinId].Clear();
     }
     _history[state.PinId].Add(state);
 }
        public async Task StartMonitorAsync()
        {
            if (!IsConnected)
            {
                Logger?.LogError("Can't start monitoring, not connected");
                return;
            }

            while (!cancelToken.IsCancellationRequested)
            {
                foreach (int pinId in _monitorList)
                {
                    GpioState st = await readPinAsync(pinId);

                    saveState(st);
                    Logger?.LogDebug($"[SEND] GpioState:{st}");
                    // Finally send the value:
                    await _conn.SendAsync("Broadcast", st.Timestamp, st.PinId, st.IsAnalog, st.Value);
                }
                await Task.Delay(UpdateMilliseconds, cancelToken);
            }
            await _conn.DisposeAsync();
        }
 public GpioAdapterStateChangedEventArgs(int gpioId, GpioState oldState, GpioState newState)
 {
     GpioId   = gpioId;
     OldState = oldState;
     NewState = newState;
 }
 public void WriteState(int gpio, GpioState state)
 {
     //_logger.Log(LogLevel.Information, $"FAKE SetState: GPIO = {gpio}; State = {state}");
 }
Beispiel #18
0
 public void NotifyGpioStateChanged(int gpioId, GpioState oldState, GpioState newState)
 {
     GpioStateChanged?.Invoke(this, new GpioAdapterStateChangedEventArgs(gpioId, oldState, newState));
 }
 public bool ValidateState(GpioState state)
 => IsValidPin(state?.PinId ?? InvalidPin);