ApelPro

sorosport_olvasás_time

Apr 11th, 2021
840
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define LED1 13
  2. #define LED2 3
  3.  
  4. char* strToLower( char *str ) {
  5.   int length = strlen( str );
  6.   for ( int i = 0; i < length; i++ ) {
  7.     str[i] = tolower( str[i] );   // toupper()
  8.   }
  9.   return str;
  10. }
  11.  
  12. void setup()
  13. {
  14.   pinMode(LED1, OUTPUT);
  15.   pinMode(LED2, OUTPUT );
  16.  
  17.   Serial.begin( 115200 );  
  18. }
  19.  
  20. char command[100];
  21. byte commandLength = 0;
  22.  
  23. char commandHandler( char* command ) {
  24.   if ( strncmp( command, "led1", 4 ) == 0 ) {
  25.    
  26.     char *ptr = strtok( command, " " );
  27.     while( ( ptr = strtok( NULL, " " ) ) != NULL ) {
  28.       if ( strcmp( ptr, "on" ) == 0 ) {
  29.         digitalWrite( LED1, HIGH );
  30.       } else if ( strcmp( ptr, "off" ) == 0 ) {
  31.          digitalWrite( LED1, LOW );
  32.       } else if ( strcmp( ptr, "toggle" ) == 0 ) {
  33.         digitalWrite( LED1, ! digitalRead( LED1 ) );
  34.       }
  35.     }
  36.   } else if ( strncmp( command, "led2", 4 ) == 0 ) {
  37.     char *ptr = strtok( command, " " );
  38.     while( ( ptr = strtok( NULL, " " ) ) != NULL ) {
  39.       if (  strcmp( ptr, "on" ) == 0 ) {
  40.         digitalWrite( LED2, HIGH );
  41.       } else if ( strcmp( ptr, "off" ) == 0 ) {
  42.         digitalWrite( LED2, LOW );
  43.       } else if ( strcmp( ptr, "toggle" ) == 0 ) {
  44.         digitalWrite( LED2, ! digitalRead( LED2 ) );
  45.       } else {
  46.         Serial.println( "Nem ismert paraméter" );
  47.       }
  48.     }
  49.   } else if ( strncmp( command, "cat", 3 ) == 0 ) {
  50.     char str[100] = "";
  51.     char *ptr = strtok( command, " " );
  52.     while( ( ptr = strtok( NULL, " " ) ) != NULL ) {
  53.         sprintf( str, "%s%s", str,ptr );
  54.     }
  55.     Serial.print( "cat=>[" ); Serial.print( str ); Serial.println( "]" );
  56.   } else if ( strncmp( command, "search", 6 ) == 0 ) {
  57.      char *ptr = strtok( command, " " );
  58.      char* param1 = strtok( NULL, " " );
  59.      char* param2 = strtok( NULL, " " );
  60.    
  61.      char* result = strstr( param1, param2 );
  62.      if ( result )
  63.         Serial.println(  );
  64.      else
  65.         Serial.println( "A keresett string (" + String( param2 ) + ") nem található ebben: " + String( param1 ) );
  66.   } else if ( strncmp( command, "copy", 4 ) == 0 ) {
  67.      char str[100];
  68.      char* ptr = strtok( command, " " );
  69.      char* param1 = strtok( NULL, " " ); // a szöveg
  70.      char* param2 = strtok( NULL, " " ); // mettől
  71.      char* param3 = strtok( NULL, " " ); // hány karaktert
  72.    
  73.      int length = strlen( param1 );
  74.    
  75.      int begin = atoi( param2 ), count = atoi( param3 );
  76.    
  77.      if ( begin > 0 && count > 0 && length >= begin + count ) {
  78.        strncpy( str, param1 + begin - 1, count );
  79.        Serial.println( str );
  80.      } else {
  81.        Serial.println( "érvénytelen hossz" );
  82.      }
  83.   }
  84. }
  85.  
  86. unsigned long lastToggle = 0;
  87.  
  88. void loop()
  89. {
  90.   if ( Serial.available() ) {
  91.     command[commandLength++] = ( char )Serial.read();
  92.     delayMicroseconds( 100 );
  93.   } else if ( commandLength > 0 ) {
  94.     command[commandLength] = 0;
  95.     Serial.print( "A beirt parancs: " ); Serial.println( command );
  96.     commandHandler( strToLower( command ) );
  97.     commandLength = 0;
  98.   }
  99.  
  100.   if ( millis() - lastToggle > 1000 ) {
  101.     lastToggle = millis();
  102.     digitalWrite( LED1, ! digitalRead( LED1 ) );   
  103.   }
  104. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×