Exemplo n.º 1
0
        /// <summary>
        /// Builds and returns a <see cref="GPIOState"/> from the JSON
        /// string returned from the device (GET /*).
        /// </summary>
        /// <param name="json">JSON configuration/status information</param>
        /// <returns>A <see cref="GPIOState"/> containing the values from the JSON string</returns>
        internal static GPIOState FromJSONString(string json)
        {
            //TODO: This code is based off the example from the WebIOPi documentation.
            //      Need some more real-world examples to verify that this works in all cases/configurations.
            var parsedObject = new GPIOState();
            var parser       = JObject.Parse(json);

            parsedObject.UART0_Enabled = (int)parser["UART0"] == 1;
            parsedObject.I2C0_Enabled  = (int)parser["I2C0"] == 1;
            parsedObject.I2C1_Enabled  = (int)parser["I2C1"] == 1;
            parsedObject.SPI0_Enabled  = (int)parser["SPI0"] == 1;

            // pins are *not* in a json array but an object:
            // "GPIO":{
            //    "0": { "function": "IN", "value": 1},
            //    "1": { "function": "IN", "value": 1},
            // etc...
            var gpio = (JObject)parser["GPIO"];

            for (int i = 0; i < 54; i++)
            {
                var pinItem = gpio[i.ToString()];
                parsedObject.PinStatuses.Add(new PinDetails
                {
                    PinNumber = i,
                    Function  = (string)pinItem["function"],
                    Value     = (int)pinItem["value"]
                });
            }
            return(parsedObject);
        }
Exemplo n.º 2
0
 public Task <GPIOState> GetGPIOState()
 {
     return(Task.FromResult(GPIOState.FromJSONString(ExampleStatus)));
 }