From 2092461fd47d0cbab749529ba18ee894e1fe6e50 Mon Sep 17 00:00:00 2001 From: schodet Date: Sun, 10 Apr 2005 00:29:15 +0000 Subject: test_proto version pas light. Add: Proto::wait. --- 2005/i/robert/src/proto/proto.cc | 10 ++++ 2005/i/robert/src/proto/proto.hh | 4 ++ 2005/i/robert/src/proto/test_proto.cc | 95 ++++++++++++++++++++++++++++++----- 3 files changed, 96 insertions(+), 13 deletions(-) (limited to '2005/i') diff --git a/2005/i/robert/src/proto/proto.cc b/2005/i/robert/src/proto/proto.cc index 3a35ec3..20727a2 100644 --- a/2005/i/robert/src/proto/proto.cc +++ b/2005/i/robert/src/proto/proto.cc @@ -179,6 +179,16 @@ Proto::decode (const Frame &frame, const char *format, int &a0, return true; } +/// Attend que des caractères soit disponible pendant un temps maximum en +/// milliseconde et les traite. Renvois le résultat de sync (). Attention, +/// fonction bloquante, n'utiliser que pour les tests. +bool +Proto::wait (int timeout/*-1*/) +{ + serial_.wait (timeout); + return sync (); +} + /// Récupère les infos de l'AVR pour construire une frame bool Proto::getFrame(void) diff --git a/2005/i/robert/src/proto/proto.hh b/2005/i/robert/src/proto/proto.hh index 5753fe0..0a2099b 100644 --- a/2005/i/robert/src/proto/proto.hh +++ b/2005/i/robert/src/proto/proto.hh @@ -95,6 +95,10 @@ class Proto : public NonCopyable static bool decode (const Frame &frame, const char *format, int &a0, int &a1, int &a2, int &a3); //@} + /// Attend que des caractères soit disponible pendant un temps maximum en + /// milliseconde et les traite. Renvois le résultat de sync (). Attention, + /// fonction bloquante, n'utiliser que pour les tests. + bool wait (int timeout = -1); public: /// Les clients de Proto doivent dériver de Receiver. class Receiver diff --git a/2005/i/robert/src/proto/test_proto.cc b/2005/i/robert/src/proto/test_proto.cc index c5bcd0d..faec030 100644 --- a/2005/i/robert/src/proto/test_proto.cc +++ b/2005/i/robert/src/proto/test_proto.cc @@ -23,37 +23,106 @@ // // }}} #include "proto.hh" +#include "timer/timer.hh" #include -#include +#include +#include +#include -class chier : public Proto::Receiver +/// Classe de test pour proto. +class TestProto : public Proto::Receiver { - void receive(char command, const Proto::Frame &frame) + void receive (char command, const Proto::Frame &frame) { - std::cout << "J'ai reçu en commande:" << command << std::endl; - std::cout << "J'ai reçu en arg:" << std::endl; - for(std::vector::const_iterator it = frame.args.begin(); it != frame.args.end(); it++) - std::cout << static_cast(*it) << std::endl; + std::cout << command << ' '; + std::copy (frame.args.begin (), frame.args.end (), + std::ostream_iterator (std::cout, " ")); + std::cout << std::endl; } }; +/// Affiche un memo de suntaxe. +void +syntax (void) +{ + std::cout << "test_proto - test la classe de protocol série.\n" + "Syntaxe : test_proto <...>\n" + " s envois une commande\n" + " w attend pendant un nombre de millisecondes\n" + " ? affiche cet écran d'aide\n" + << std::endl; +} + int main (int argc, char **argv) { try { - std::cout << "chier partout!!" << std::endl; + int i; + if (argc < 2) + { + syntax (); + return 1; + } + TestProto testProto; + Proto proto (testProto); + proto.open (argv[1]); + i = 2; + while (i < argc) + { + bool reliable = true; + switch (argv[i][0]) + { + case 'S': + reliable = false; + // no break; + case 's': + { + if (i + 2 >= argc) + throw std::runtime_error ("syntax error"); + unsigned a; + int arg[4]; + char c = argv[++i][0]; + const char *format = argv[++i]; + if (i + static_cast (strlen (format)) >= argc) + throw std::runtime_error ("syntax error"); + for (a = 0; a < 4 && a < strlen (format); a++) + arg[a] = atoi (argv[++i]); + proto.send (c, format, arg[0], arg[1], arg[2], arg[3], + reliable); + } + break; + case 'w': + { + int stop, t; + if (i + 1 >= argc) + throw std::runtime_error ("syntax error"); + stop = atoi (argv[++i]) + Timer::getProgramTime (); + t = Timer::getProgramTime (); + while (t < stop) + { + std::cout << std::endl; + proto.wait (stop - t); + t = Timer::getProgramTime (); + } + break; + } + case '?': + proto.close (); + syntax (); + return 0; + default: + throw std::runtime_error ("syntax error"); + } + i++; + } } catch (const std::exception &e) { std::cerr << e.what () << std::endl; + syntax (); return 1; } - - chier ch; - Proto chieri(ch); - chieri.open("-"); - chieri.send('a', "bb", 3, 4); return 0; } -- cgit v1.2.3