daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest May 16th, 2018 105 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. typedef struct {
  2.     unit8_t num;
  3.     char** name;
  4.     char** value;
  5. } HttpParams;
  6.  
  7. typedef struct {
  8.     char* name;
  9.     void (*fn)(HttpParams*);
  10. } ActionCallback;
  11.  
  12. #define MAX_CB 10
  13.  
  14. ActionCallback cb[MAX_CB];
  15. uint8_t numCb = 0;
  16.  
  17. void ShowParams(HttpParams* params) {
  18.     for (int i = 0; i < params->num; ++i) {
  19.         Serial.print(params->name[i]);
  20.         Serial.print(": ");
  21.         Serial.println(params->value[i]);
  22.     }
  23. }
  24.  
  25. void LedFunc(HttpParams* params) {
  26.     for (int i = 0; i < params->num; ++i) {
  27.         int val = strcmp(params->value[i], "LOW") == 0 ? 0 : 1;
  28.         int pin = 0;
  29.         if (strcmp(params->name[i], "P3") == 0) {
  30.             pin = 3;
  31.         } else if (strcmp(params->name[i], "P5") == 0) {
  32.             pin = 5;
  33.         }
  34.  
  35.         if (pin > 0) {
  36.             digitalWrite(pin, val);
  37.         }
  38.     }
  39. }
  40.  
  41. void registerFunc(char* key, void (*fn)(HttpParams*)) {
  42.     cb[numCb].name = key;
  43.     cb[numCb].fn = fn;
  44. }
  45.  
  46. void setup() {
  47.     Serial.begin(115200);
  48.     registerFunc("echo", &ShowParams);
  49.     registerFunc("led", &LedFunc);
  50. }
  51.  
  52. void loop() {
  53.     //stuff
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top