Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could create masks for each of the groups:</p> <pre><code>typedef enum { AN_TRISTATE_0 = 0x00001, AN_TRISTATE_1 = 0x00002, AN_TRISTATE_MASK = 0x0000f, AN_NOTHING = 0x00010, // Should this be 0x00000 ? AN_MOTOR_1 = 0x00100, AN_MOTOR_2 = 0x00200, AN_MOTOR_3 = 0x00400, AN_MOTOR_MASK = 0x00f00, AN_SENSOR_1 = 0x01000, AN_SENSOR_2 = 0x02000, AN_SENSOR_3 = 0x04000, AN_SENSOR_4 = 0x08000, AN_SENSOR_5 = 0x10000, AN_SENSOR_MASK = 0xff000 } adc_pin_func_t; </code></pre> <p>And then simply test a group against the mask using the <code>&amp;</code> operator:</p> <pre><code>if (a_particular_pin &amp; AN_SENSOR_MASK) { // it's a sensor pin } else if (a_particular_pin &amp; AN_MOTOR_MASK) { // it's a motor pin } </code></pre> <p>EDIT: As others have suggested using a range, then you could probably create a macro for the test, which would allow you to change how the test is performed without the need to change the code (always a good thing):</p> <pre><code>#define IS_AN_SENSOR(x) (((x) &amp; AN_SENSOR_MASK) != 0) #define IS_AN_MOTOR(x) (((x) &amp; AN_MOTOR_MASK) != 0) // etc. </code></pre> <p>and then the test becomes:</p> <pre><code>if (IS_AN_SENSOR(a_particular_pin)) { // it's a sensor pin } else if (IS_AN_MOTOR(a_particular_pin)) { // it's a motor pin } // etc </code></pre> <p>If you then needed to change to using a range then only the macros need to change (and you'd obviously need to define the range min/max):</p> <pre><code>#define IS_AN_SENSOR(x) ((x) &gt;= AN_SENSOR_START &amp;&amp; (x) &lt;= AN_SENSOR_END) // etc </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload