From a957915d1fc7d5c465e43647bdce04ca8d9e992b Mon Sep 17 00:00:00 2001 From: haller Date: Thu, 7 Apr 2005 20:27:53 +0000 Subject: Modifications des commentaires Ajout de la possibilité d'envoie des packets non fiables ajout des static_cast --- 2005/i/robert/src/proto/proto.cc | 98 ++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 39 deletions(-) (limited to '2005/i/robert/src/proto/proto.cc') diff --git a/2005/i/robert/src/proto/proto.cc b/2005/i/robert/src/proto/proto.cc index d65aa58..c0f95f7 100644 --- a/2005/i/robert/src/proto/proto.cc +++ b/2005/i/robert/src/proto/proto.cc @@ -84,9 +84,16 @@ Proto::sync(void) /// Envoie un packet void -Proto::send (const Frame & Frame) +Proto::send (const Frame & frame, bool fiable) { - frameQueue_.push(Frame); + if(fiable) + { + if(frameQueue_.empty()) + tLastSend_ =0; + frameQueue_.push(frame); + } + else + sendFrame(frame); sync(); } @@ -96,11 +103,10 @@ Proto::send (const Frame & Frame) /// 32 bits, majuscule pour signé). void Proto::send (uint8_t command, const char *format, int a0, int a1, - int a2, int a3) + int a2, int a3, bool fiable) { // Constitution de la frame Proto::Frame frame; - //int nbArg = strlen(format); frame.command = command; if (format[0] != '\0') @@ -117,13 +123,22 @@ Proto::send (uint8_t command, const char *format, int a0, int a1, } } } - send(frame); + send(frame,fiable); } +/// permet d'envoyer un packet pas fiable +void +Proto::send_pas_fiable (uint8_t command, const char *format, int a0, int a1, + int a2, int a3) +{ + send(command, format, a0, a1, a2, a3, false); +} + +/// Teste si le packet correspond au format et décode les valeurs. Utilise +/// le même format que send. bool Proto::decode (const Proto::Frame &frame) { - //bon, est-ce que c'est bien utile ça? int dummy; return decode(frame, "", dummy, dummy, dummy, dummy); } @@ -156,7 +171,7 @@ Proto::decode (const Frame &frame, const char *format, int &a0, int &a1, int &a2, int &a3) { //Teste si il y a bien le bon nombre d'argument - if (int(frame.args.size()) != argsFrameSize(format))//un cast pour virer un warning + if (static_cast(frame.args.size()) != argsFrameSize(format)) return false; // On décode et on envoie decodeArg(frame, format, a0, a1, a2, a3); @@ -193,16 +208,17 @@ Proto::getFrame(void) switch(revState_) { case 1: - currentFrame_.command = (uint8_t(hex2digit( receivedChar ))) + currentFrame_.command = (static_cast(hex2digit( receivedChar ))) << 4; revState_ = 2; break; case 2: - currentFrame_.command |= uint8_t(hex2digit( receivedChar )); + currentFrame_.command |= static_cast(hex2digit( + receivedChar )); revState_ = 3; break; case 3: - currentFrame_.args.push_back(uint8_t( + currentFrame_.args.push_back(static_cast( hex2digit( receivedChar )) << 4); revState_ = 3; break; @@ -213,7 +229,8 @@ Proto::getFrame(void) return false; } - void +/// Envoie la frame dans l'AVR +void Proto::sendFrame(const Frame & frame) { //envoyer le bang @@ -224,7 +241,7 @@ Proto::sendFrame(const Frame & frame) serial_.putchar(digit2hex(frame.command & 0x0f)); //Envoyer les arguments - for(int i = 0; i < int(frame.args.size()); i++) //le cast est pour virer un warning + for(int i = 0; i < static_cast(frame.args.size()); i++) { serial_.putchar(digit2hex(frame.args[i] >> 4)); serial_.putchar(digit2hex(frame.args[i] & 0x0f)); @@ -237,31 +254,33 @@ Proto::sendFrame(const Frame & frame) tLastSend_ = Timer::getProgramTime(); } - void +/// Remplie une frame avec un argument +void Proto::newArgFrame(Proto::Frame & frame, char format, int arg) { switch(format) { case 'b': case 'B': - frame.args.push_back(uint8_t(arg)); + frame.args.push_back(static_cast(arg)); break; case 'w': case 'W': - frame.args.push_back(uint8_t(arg >> 8)); - frame.args.push_back(uint8_t(arg)); + frame.args.push_back(static_cast(arg >> 8)); + frame.args.push_back(static_cast(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)); + frame.args.push_back(static_cast(arg >> 24)); + frame.args.push_back(static_cast(arg >> 16)); + frame.args.push_back(static_cast(arg >> 8)); + frame.args.push_back(static_cast(arg)); break; } } - int +/// Renvoie la taille necessaire du vecteur args pour un format donné +int Proto::argsFrameSize(const char *format) { int size = 0; @@ -283,6 +302,7 @@ Proto::argsFrameSize(const char *format) return size; } +/// Décode un argument void Proto::decodeArg(const Frame & frame, const char *format, int &a0, int &a1, int &a2, int &a3) { @@ -294,43 +314,43 @@ Proto::decodeArg(const Frame & frame, const char *format, int &a0, int &a1, int switch(*format) { case 'b': - temp[i] = int(frame.args[pos]); + temp[i] = static_cast(frame.args[pos]); pos++; break; case 'B': { - int8_t t = int8_t(frame.args[pos]); - temp[i] = int(t); + int8_t t = static_cast(frame.args[pos]); + temp[i] = static_cast(t); pos++; break; } case 'w': - temp[i] = int(frame.args[pos]) << 8 - |int(frame.args[pos + 1]); + temp[i] = static_cast(frame.args[pos]) << 8 + |static_cast(frame.args[pos + 1]); pos += 2; break; case 'W': { - int8_t t = int8_t(frame.args[pos]); - temp[i] = int(t) << 8 - |int(frame.args[pos + 1]); + int8_t t = static_cast(frame.args[pos]); + temp[i] = static_cast(t) << 8 + |static_cast(frame.args[pos + 1]); pos += 2; break; } case 'd': - temp[i] = int(frame.args[pos]) << 24 - |int(frame.args[pos + 1]) << 16 - |int(frame.args[pos + 2]) << 8 - |int(frame.args[pos + 3]); + temp[i] = static_cast(frame.args[pos]) << 24 + |static_cast(frame.args[pos + 1]) << 16 + |static_cast(frame.args[pos + 2]) << 8 + |static_cast(frame.args[pos + 3]); pos += 4; break; case 'D': - int8_t t = int8_t(frame.args[pos]); - temp[i] = int(t) << 24 - |int(frame.args[pos + 1]) << 16 - |int(frame.args[pos + 2]) << 8 - |int(frame.args[pos + 3]); + int8_t t = static_cast(frame.args[pos]); + temp[i] = static_cast(t) << 24 + |static_cast(frame.args[pos + 1]) << 16 + |static_cast(frame.args[pos + 2]) << 8 + |static_cast(frame.args[pos + 3]); break; } } @@ -340,7 +360,7 @@ Proto::decodeArg(const Frame & frame, const char *format, int &a0, int &a1, int a3 = temp[3]; } - bool +bool Proto::Frame::operator==(const Frame& frame) { return this->command == frame.command && this->args == frame.args; -- cgit v1.2.3