Advertisement
ind03

DebugHardware

Feb 7th, 2021
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. // DebugHardware.h
  2.  
  3. #ifndef _DEBUGHARDWARE_h
  4. #define _DEBUGHARDWARE_h
  5.  
  6. #include <arduino.h>
  7. /*********************************************************************************
  8.  * Input:
  9.  *  Px -->  Select Pin x
  10.  *  Ax -->  Select Analog x
  11.  *  I  -->  Input
  12.  *  O  -->  Output
  13.  *  ?  -->  Query level
  14.  *  l  -->  Set low
  15.  *  h  -->  Set high
  16.  *  v  -->  Query analog value
  17.  *
  18.  ********************************************************************************/
  19. class DebugHardware
  20. {
  21.  protected:
  22.      byte pin;
  23.    
  24.  
  25.  public:
  26.     bool handle ();
  27.     bool handle (const char* action);
  28. };
  29.  
  30.  
  31. #endif
  32.  
  33.  ********************************************************************************
  34.  
  35.  
  36. // DebugHardware.cpp
  37.  
  38. #include "DebugHardware.h"
  39.  
  40. bool DebugHardware::handle ()
  41. {
  42.     if (!Serial.available ())
  43.         return false;
  44.  
  45.     char bfr[8];
  46.     Serial.readBytesUntil ('\n', bfr, sizeof (bfr));
  47.  
  48.     return handle (bfr);
  49. }
  50.  
  51. bool DebugHardware::handle (const char* action)
  52. {
  53.     switch (*action)
  54.     {
  55.         case 'P':
  56.             pin = atoi (action + 1);
  57.             Serial.print (F ("Pin "));
  58.             Serial.print (pin);
  59.             Serial.println ();
  60.             return true;
  61.  
  62.         case 'A':
  63.             pin = PIN_A0 + atoi (action + 1);
  64.             Serial.print (F ("Analog "));
  65.             Serial.print (pin);
  66.             Serial.println ();
  67.             return true;
  68.  
  69.         case 'I':
  70.             pinMode (pin, INPUT);
  71.             Serial.println (F ("INPUT"));
  72.             return true;
  73.  
  74.         case 'U':
  75.             pinMode (pin, INPUT_PULLUP);
  76.             Serial.println (F ("PULLUP"));
  77.             return true;
  78.  
  79.         case 'O':
  80.             pinMode (pin, OUTPUT);
  81.             Serial.println (F ("OUTPUT"));
  82.             return true;
  83.  
  84.         case '?':
  85.             Serial.println (digitalRead (pin) ? F ("High") : F ("Low"));
  86.             return true;
  87.  
  88.         case 'l':
  89.             digitalWrite (pin, LOW);
  90.             Serial.println (F ("Low"));
  91.             return true;
  92.         case 'h':
  93.             digitalWrite (pin, HIGH);
  94.             Serial.println (F ("High"));
  95.             return true;
  96.  
  97.         case 'v':
  98.         {
  99.             auto value = analogRead (pin);
  100.             Serial.println (value);
  101.             return true;
  102.         }
  103.         default:
  104.             Serial.println (F ("Px, I/U/O, ?, l/h, v:"));
  105.  
  106.             return false;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement