This commit is contained in:
		
							
								
								
									
										61
									
								
								lib/CommandHandler/CommandHandler.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								lib/CommandHandler/CommandHandler.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | ||||
| #include "CommandHandler.h" | ||||
|  | ||||
| #include "Arduino.h" | ||||
| #include "ArduinoJson.h" | ||||
|  | ||||
| CommandHandler::CommandHandler(DisplayProxy *display) | ||||
| { | ||||
|     this->display = display; | ||||
| } | ||||
|  | ||||
| bool CommandHandler::parse(char *cmd) | ||||
| { | ||||
|     Serial.print("Handling: "); | ||||
|     Serial.println(cmd); | ||||
|     StaticJsonDocument<32> doc; | ||||
|     DeserializationError error = deserializeJson(doc, cmd); | ||||
|  | ||||
|     // Test if parsing succeeds. | ||||
|     if (error) | ||||
|     { | ||||
|         Serial.print(F("deserializeJson() failed: ")); | ||||
|         Serial.println(error.c_str()); | ||||
|         return false; | ||||
|     } | ||||
|  | ||||
|     const char *command = doc["command"]; | ||||
|     if (strcmp("write", command) == 0 && doc["text"].is<const char*>() && doc["x"].is<int>() && doc["y"].is<int>()) | ||||
|     { | ||||
|         const char *text = doc["text"]; | ||||
|         int x = doc["x"]; | ||||
|         int y = doc["y"]; | ||||
|  | ||||
|         this->debugWrite(x, y, text); | ||||
|         this->display->write(x, y, text); | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     if (strcmp("clear", command) == 0) | ||||
|     { | ||||
|         this->display->clear(); | ||||
|     } | ||||
|  | ||||
|     if (strcmp("offset", command) == 0) | ||||
|     { | ||||
|         int x = doc["x"]; | ||||
|         int y = doc["y"]; | ||||
|         this->display->setOffset(x, y); | ||||
|     } | ||||
|  | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| void CommandHandler::debugWrite(int x, int y, const char *text) | ||||
| { | ||||
|     Serial.print(F("Writing (")); | ||||
|     Serial.print(x); | ||||
|     Serial.print(","); | ||||
|     Serial.print(y); | ||||
|     Serial.print("): "); | ||||
|     Serial.println(text); | ||||
| } | ||||
							
								
								
									
										16
									
								
								lib/CommandHandler/CommandHandler.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								lib/CommandHandler/CommandHandler.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| #ifndef COMMANDHANDLER_H | ||||
| #define COMMANDHANDLER_H | ||||
| #include "DisplayProxy.h" | ||||
|  | ||||
| class CommandHandler | ||||
| { | ||||
| public: | ||||
|     CommandHandler(DisplayProxy *display); | ||||
|     bool parse(char *cmd); | ||||
| private: | ||||
|     void debugWrite(int x, int y, const char *text); | ||||
|     DisplayProxy *display; | ||||
|  | ||||
| }; | ||||
|  | ||||
| #endif | ||||
		Reference in New Issue
	
	Block a user
	 Jens Christian True
					Jens Christian True