Sara Ojeda / Mbed 2 deprecated prueba_hsens

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Command.cpp Source File

Command.cpp

00001 #include "Command.h"
00002 #include "Log.h"
00003 #include "prueba_hsens.h"
00004 #include "Pinout.h"
00005 #include <stdio.h>                              //Para luego escribir printf, me compilaba también sin ponerlo
00006 
00007 extern prueba_hsens * sensores;                 //Exporto el objeto
00008 extern Log _;
00009 
00010 Command::Command(const string* argv, uint8_t argc)
00011     : argv(argv), argc(argc)
00012 {
00013     this->error = "";
00014     this->reader = Reader::getInstance();
00015 }
00016 
00017 Command::~Command()
00018 {
00019     delete[] this->argv;
00020 }
00021 
00022 bool Command::isHelp() const
00023 {
00024     int8_t index = -1;
00025     if (this->argc > 0) {
00026         index = 0;
00027         if (this->argc > 1) {
00028             index = 1;
00029         }
00030     }
00031     if (index != -1) {
00032         return this->argv[index] == "?" || this->argv[index] == "help";
00033     } else {
00034         return false;
00035     }
00036 }
00037 
00038 void Command::setError(const string &error)
00039 {
00040     this->error = error;
00041 }
00042 
00043 const string Command::getError() const
00044 {
00045     return this->error;
00046 }
00047 
00048 const string Command::getFirstParameter() const
00049 {
00050     return this->argc > 0 ? this->argv[0] : "";
00051 }
00052 
00053 bool Command::execute()
00054 {
00055     if (this->isHelp()) {
00056         this->printHelp();
00057         return true;
00058     } else {
00059         return false;
00060     }
00061 }
00062 
00063 void Command::printHelp() const
00064 {
00065     _.print("Available commands:").ln();
00066     _.print("  prueba                - Para combrobar el funcionamiento del sensor").ln();
00067 }
00068 
00069 ValidCommand::ValidCommand(const string* argv, uint8_t argc)
00070     : Command(argv, argc)
00071 {
00072 }
00073 
00074 InvalidCommand::InvalidCommand(const string* argv, uint8_t argc)
00075     : Command(argv, argc)
00076 {
00077 }
00078 
00079 bool InvalidCommand::execute()
00080 {
00081     if (this->isHelp()) {
00082         this->printHelp();
00083         return true;
00084     } else {
00085         _.print("Hexadecimal representation of command: ");
00086         for (uint8_t i = 0, n = this->argv[0].length(); i < n; i++) {
00087             _.print((uint8_t)this->argv[0][i], 16).print(' ');
00088         }
00089         _.ln();
00090         string error = "Invalid command: '" + this->argv[0] + "'";
00091         this->setError(error);
00092         return false;
00093     }
00094 }
00095 
00096  PruebaCommand::PruebaCommand(const string *argv, uint8_t argc)
00097      : ValidCommand(argv, argc)
00098 {
00099     //Crearia el objeto en el caso que no lo tendria hecho de forma global
00100     //Lo mejor hubiera sido crear un objeto aqui el cual lo obtendriamos de una de las partes del objeto global
00101 }
00102 
00103 bool PruebaCommand::execute()
00104 {
00105     if(ValidCommand::execute()) {
00106         return true;
00107     }else if (this->argc > 1) {
00108         if (this->argv[1] == "tension") {
00109             sensores->medir_tension();
00110             return true;
00111         }else {
00112             return false;
00113         }
00114     }
00115     this->setError("Invalid command");
00116     return false;        
00117 }
00118 
00119 void PruebaCommand::printHelp() const
00120 {
00121     _.print("Help for 'Prueba Hsens' command:").ln();
00122     _.print("tension          - Da la tensión que envía el sensor.").ln();
00123 
00124 }