I amusing C# UWP and i am storing sensor data to a database. What I would like to do is send the Sensor name instead of the GPIO Number.

So on pin_valuechanged()

  • I like to assign the pinNumber a name.
  • I can have this preconfigured set of pins and names somewhere (maybe a constant file?)

I am not sure how can I say that GPIO 5 is "door" and GPIO 6 is "windows"

The information will stored something like this

 Sensor      State 
 Door        Open 
 Windows     Closed
  • use a dictionary to link pin number with a more suitable name. the dictionary would have as its key the pin number and the value associated or linked to that pin would be the value for the key. see stackoverflow.com/questions/12169443/… Nov 21 '17 at 3:52

This is more of a programming question and it can apply to any good object orientated programming language - Which are most modern full stack languages

I suggest finding and reading something about "Good Object Orientated Programming" to help you avoid basic mistakes and broaden your understanding of the options available.

There are many ways to map values to each other. I will provide you with and abstract code class, that I (a professional .NET Developer) would use.

To create a lookup method

  • a static (one instance in entire program)
  • dictionary "like" (not a real dictionary but the only way to have compile time defined set of constants which is extremely fast for looking up values after compilation)(you can only change these values by recompiling - if you need dynamic lookup then using a Dictionary or Hashset is the preferred way)

So a basic lookup code would look like this

public static string GetValueByGPIO(int PinNumber)
    switch (PinNumber)
        case 5: return "Door";
        case 6: return "Windows";
        default: return "NA";

You can call that method from anywhere in your program due to it being marked as static - That is a very simple example and you can actually return classes ready to be consumed by your database saving code

So something like this is more reusable and follows basic SOLID principles

 public class SensorStateEntity
      public int PinNumber { get; set; }
      public string Name { get; set; }
      public bool IsOpen { get; set; }

  public static SensorStateEntity GetValueByGPIO(int PinNumber, bool state)

     switch (PinNumber)
        case 5: return new SensorStateEntity() { Name = "Door", PinNumber = PinNumber, IsOpen = state };
        case 6: return new SensorStateEntity() { Name = "Windows", PinNumber = PinNumber, IsOpen = state };
            return new SensorStateEntity() { Name = "NA", PinNumber = PinNumber, IsOpen = state };

And unless you are hitting some kind of performance issue there is absolutely no reason to micromanage the code in terms of is this the most efficient or not. C# compiler and the IL (Intermediate Language) does a fantastic job at super optimising code (a team of highly skilled programmers out numbering the skills of entire stack exchange made sure of that) - So things like caching static lookups, managing memory, efficiency is all done as best as possible during compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.