From 4bf8554325d4db51f9c18fbcc8f96233a9f733df Mon Sep 17 00:00:00 2001 From: haller Date: Mon, 4 Apr 2005 09:38:21 +0000 Subject: Modification de proto - proto.hh :quelques modifications - proto.cc :Fonctions codées Quelques erreurs et ajustement à régler n'est pas encore "robot coding standards" compliant fichier testé en aucun point --- 2005/i/robert/src/proto/proto.cc | 179 ++++++++++++++++++++++----------------- 2005/i/robert/src/proto/proto.hh | 33 +++++--- 2 files changed, 122 insertions(+), 90 deletions(-) (limited to '2005/i') diff --git a/2005/i/robert/src/proto/proto.cc b/2005/i/robert/src/proto/proto.cc index 0e50bfa..15a2272 100644 --- a/2005/i/robert/src/proto/proto.cc +++ b/2005/i/robert/src/proto/proto.cc @@ -23,11 +23,15 @@ // // }}} +#include "timer/timer.hh" +#include "utils/hexa.hh" #include "proto.hh" /// Constructeur. Proto::Proto(Receiver &receiver) :receiver_(receiver) + ,tLastSend_(0) + ,revState_(0) { } @@ -46,47 +50,52 @@ void Proto::close(void) bool Proto::sync(void) { + bool reGet = true; //Récupération de la frame - if(getFrame()) + while(reGet) { - //Si la frame est un aquittement - if(currentFrame == frameQueue_.toppp()) + if(reGet = getFrame()) { - //on vire la commande de la queue - frameQueue.PAN!!!(); - //Et on envoie la suivante si elle existe - if(!frameQueue_.empty()) - sendFrame(frameQueue.PAN!!!()); + //Si la frame est un aquittement + if(currentFrame_ == frameQueue_.front()) + { + //on vire la commande de la queue + frameQueue_.pop(); + //Et on envoie la suivante si elle existe + if(!frameQueue_.empty()) + sendFrame(frameQueue_.front()); + } + //Si c'est une nouvelle commande, on l'envoie avec receive + else + receiver_.receive(currentFrame_.command, currentFrame_); } - //Si c'est une nouvelle commande, on l'envoie avec receive - else - receive(currentFrame.command, currentFrame); } //On regarde depuis combien de temps on a envoyé une commande if(!frameQueue_.empty()) { //Si on dépasse la milliseconde, on renvoie - if(timer.getProgramTime() - tLastEnd_ > 500) - sendFrame(frameQueue.topppp()); + if(Timer::getProgramTime() - tLastSend_ > 500) + sendFrame(frameQueue_.front()); } - + + return frameQueue_.empty(); } /// Envoie un packet void Proto::send (const Frame & Frame) { - frameQueue_.addChoucroute(Frame); + frameQueue_.push(Frame); sync(); } -void send (uint8_t command, const char *format, int a0, int a1, int a2, int a3) +void Proto::send (uint8_t command, const char *format, int a0, int a1, int a2, int a3) { // Constitution de la frame - Frame frame; + Proto::Frame frame; int nbArg = strlen(format); - + frame.command = command; // Conversion et saisie des aguments - + if(nbArg == 1) newArgFrame(frame, format[0], a0); if (nbArg == 2) @@ -107,12 +116,11 @@ void send (uint8_t command, const char *format, int a0, int a1, int a2, int a3) newArgFrame(frame, format[2], a2); newArgFrame(frame, format[3], a3); } - + send(frame); } -static -bool decode (const Frame &frame) +bool Proto::decode (const Proto::Frame &frame) { //On teste si des arguments sont présents(ca serait pas bon) if(!frame.args.empty()) @@ -121,7 +129,6 @@ bool decode (const Frame &frame) return true; } -static bool Proto::decode (const Frame &frame, const char *format, int &a0) { // On teste le format de la frame @@ -132,8 +139,7 @@ bool Proto::decode (const Frame &frame, const char *format, int &a0) return true; } -static -bool Proto::decode (const Frame &Frame, const char *format, int &a0, int &a1) +bool Proto::decode (const Frame &frame, const char *format, int &a0, int &a1) { // On vérifie le format de la frame if(!verifyFrame(frame, format, 2)) return false; @@ -144,8 +150,7 @@ bool Proto::decode (const Frame &Frame, const char *format, int &a0, int &a1) return true; } -static -bool Proto::decode (const Frame &Frame, const char *format, int &a0, int &a1, int &a2) +bool Proto::decode (const Frame &frame, const char *format, int &a0, int &a1, int &a2) { // On vérifie le format de la frame if(!verifyFrame(frame, format, 3)) return false; @@ -157,8 +162,7 @@ bool Proto::decode (const Frame &Frame, const char *format, int &a0, int &a1, in return true; } -static -bool Proto::decode (const Frame &Frame, const char *format, int &a0, int &a1, int &a2, int &a3) +bool Proto::decode (const Frame &frame, const char *format, int &a0, int &a1, int &a2, int &a3) { // On vérifie le format de la frame if(!verifyFrame(frame, format, 3)) return false; @@ -173,25 +177,25 @@ bool Proto::decode (const Frame &Frame, const char *format, int &a0, int &a1, in } /// Récupère les infos de l'AVR pour construire une frame -bool getFrame(void) +bool Proto::getFrame(void) { int receivedChar; - + //tant que le tampon n'est pas vide, on teste - while(receivedChar = serial_.getchar() != -1) + while((receivedChar = serial_.getchar()) != -1) { //si la donnée n'est pas erronnée if(receivedChar != 0xff) { //Si on reçoit un bang - if(receivedChar = '!') + if(receivedChar == '!') { revState_ = 1; currentFrame_.command = 0; currentFrame_.args.clear(); } //Si on reçoit le retour chariot et que on reçevait les args - if(receivedChar = '\n' && revState_ == 2) + if(receivedChar == '\n' && revState_ == 3) { revState_ = 0; return true; @@ -199,36 +203,50 @@ bool getFrame(void) //Pour les autres charactères //Si on attend la commande else if(revState_ == 1) - currentFrame_.command = receivedChar; + { + currentFrame_.command = ((uint8_t)hex2digit( receivedChar )) << 4; + revState_ = 2; + } else if(revState_ == 2) - currentFrame_.args.push_back(receivedChar); + { + currentFrame_.command |= (uint8_t)hex2digit( receivedChar ); + revState_ = 3; + } + else if(revState_ == 3) + { + currentFrame_.args.push_back(((uint8_t)hex2digit( receivedChar )) << 4); + revState_ = 3; + } //Si revState == 0 alors on jette } } return false; } -void sendFrame(Frame & frame) +void Proto::sendFrame(const Frame & frame) { //envoyer le bang - serial_.send('!'); + serial_.putchar('!'); //Envoyer la commande - serial_send(digit2hex(frame.command >> 4)); - serial_send(digit2hex(frame.command & 0x0f)); + serial_.putchar(digit2hex(frame.command >> 4)); + serial_.putchar(digit2hex(frame.command & 0x0f)); //Envoyer les arguments - for(int i = 0; i < frame.args.size(); i++) + for(int i = 0; i < (int)frame.args.size(); i++) //le cast est pour virer un warning { - serial_send(digit2hex(frame.args[i] >> 4)); - serial_send(digit2hex(frame.args[i] & 0x0f)); + serial_.putchar(digit2hex(frame.args[i] >> 4)); + serial_.putchar(digit2hex(frame.args[i] & 0x0f)); } - + //Envoyer le retour chariot - serial_.send('\n'); + serial_.putchar('\n'); + + //actualiser le timer + tLastSend_ = Timer::getProgramTime(); } -void Proto::newArgFrame(Frame & frame, char format, int arg) +void Proto::newArgFrame(Proto::Frame & frame, char format, int arg) { switch(format) { @@ -238,14 +256,14 @@ void Proto::newArgFrame(Frame & frame, char format, int arg) break; case 'w': case 'W': - frame.args.push_back((uint8_t)arg>>8); + frame.args.push_back((uint8_t)(arg>>8)); frame.args.push_back((uint8_t)arg); break; case 'd': case 'D': - frame.args.push_back((uint8_t)arg>>24); - frame.args.push_back((uint8_t)arg>>16); - frame.args.push_back((uint8_t)arg>>8); + frame.args.push_back((uint8_t)(arg>>24)); + frame.args.push_back((uint8_t)(arg>>16)); + frame.args.push_back((uint8_t)(arg>>8)); frame.args.push_back((uint8_t)arg); break; } @@ -256,7 +274,7 @@ int Proto::ArgsFrameSize(const char *format, int nbArg) int size = 0; if(nbArg == 0) nbArg = strlen(format); - + for(int i = 0; i < nbArg; i++) size += ArgSize(format[i]); return size; @@ -264,27 +282,27 @@ int Proto::ArgsFrameSize(const char *format, int nbArg) int Proto::ArgSize(char format) { - switch(format) - { - case 'b': - case 'B': - return 1; - case 'w': - case 'W': - return 2; - case 'd': - case 'D': - return 4; - default: - return 0; - } + switch(format) + { + case 'b': + case 'B': + return 1; + case 'w': + case 'W': + return 2; + case 'd': + case 'D': + return 4; + default: + return 0; + } } -bool verifyFrame(Frame &frame, const char *format, int nbArg) +bool Proto::verifyFrame(const Frame &frame, const char *format, int nbArg) { //Teste si il y a bien le bon nombre d'argument - if (strlen(format) != nbArg) return false; - if (frame.args.size() != ArgsFrameSize(format)) return false; + if ((int)strlen(format) != nbArg) return false; //Un cast pour shooter un warning + if ((int)frame.args.size() != ArgsFrameSize(format)) return false; //un cast pour virer un warning //Voir pour des test plus approffondi si possible et necessaire return true; } @@ -300,32 +318,41 @@ int Proto::decodeArg(const Frame & frame, const char *format, int numArg) ArgDecoded = (int)frame.args[beginArg]; break; case 'B': + { int8_t temp = (int8_t)frame.args[beginArg]; ArgDecoded = (int) temp; break; + } case 'w': ArgDecoded = (int)frame.args[beginArg] << 8 - |(int)frame.args[beginArg + 1]; + |(int)frame.args[beginArg + 1]; break; case 'W': + { int8_t temp1 = (int8_t)frame.args[beginArg]; ArgDecoded = (int)temp1 << 8 - |(int)frame.args[beginArg + 1]; + |(int)frame.args[beginArg + 1]; break; + } case 'd': ArgDecoded = (int)frame.args[beginArg] << 24 - |(int)frame.args[beginArg + 1] << 16 - |(int)frame.args[beginArg + 2] << 8 - |(int)frame.args[beginArg + 3]; + |(int)frame.args[beginArg + 1] << 16 + |(int)frame.args[beginArg + 2] << 8 + |(int)frame.args[beginArg + 3]; break; case 'D': int8_t temp1 = (int8_t)frame.args[beginArg]; ArgDecoded = (int)temp1 << 24 - |(int)frame.args[beginArg + 1] << 16 - |(int)frame.args[beginArg + 2] << 8 - |(int)frame.args[beginArg + 3]; + |(int)frame.args[beginArg + 1] << 16 + |(int)frame.args[beginArg + 2] << 8 + |(int)frame.args[beginArg + 3]; break; } return ArgDecoded; } + +bool Proto::Frame::operator==(const Frame& frame) +{ + return this->command == frame.command && this->args == frame.args; +} diff --git a/2005/i/robert/src/proto/proto.hh b/2005/i/robert/src/proto/proto.hh index 6c304c1..19bf376 100644 --- a/2005/i/robert/src/proto/proto.hh +++ b/2005/i/robert/src/proto/proto.hh @@ -24,6 +24,9 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // // }}} + +#include +#include #include "utils/non_copyable.hh" #include "serial/serial.hh" @@ -31,7 +34,13 @@ class Proto : public NonCopyable { class Receiver; - struct Frame; + /// Packet. + struct Frame + { + uint8_t command; + std::vector args; + bool operator==(const Frame& frame); + }; Serial serial_; Receiver &receiver_; @@ -42,11 +51,13 @@ class Proto : public NonCopyable /// Frame en cours de réception Frame currentFrame_; - int revState_ = 0; + int revState_; // Etat de la réception de la frame // 0 - Rien reçu // 1 - Bang reçu - // 2 - Commande reçu + // 2 - char 1 commande reçu + // 3 - Commande reçu & nouveau argument + // 4 - char 1 argument reçu public: /// Constructeur. @@ -85,27 +96,21 @@ class Proto : public NonCopyable /// Recoit un packet. virtual void receive (uint8_t command, const Frame &frame) = 0; }; - /// Packet. - struct Frame - { - uint8_t command; - std::vector args; - }; private: /// Récupère les infos de l'AVR pour construire une frame bool getFrame(void); /// Envoie la frame dans l'AVR void sendFrame(const Frame & frame); /// Remplie une frame avec un argument - void newArgFrame(Frame & frame, char format, int arg); + void newArgFrame(Proto::Frame & frame, char format, int arg); /// Renvoie la taille necessaire du vecteur args pour un format donné - int ArgsFrameSize(const char *format,int nbArg = 0); + static int ArgsFrameSize(const char *format,int nbArg = 0); /// Renvoie la taille necessaire du vecteur args pour 1 argument - int ArgSize(char format); + static int ArgSize(char format); /// Vérifie le format de la frame - bool verifyFrame(Frame &frame, const char *format, int nbArg); + static bool verifyFrame(const Frame &frame, const char *format, int nbArg); /// Décode un argument - int decodeArg(const Frame & frame, const char *format, int numArg); + static int decodeArg(const Frame & frame, const char *format, int numArg); }; #endif // proto_hh -- cgit v1.2.3