@@ -67,6 +67,7 @@ Pido::Format outFormat = Pido::FormatDecimal;
6767bool isAverage = false ;
6868bool isDifferential = false ;
6969std::string converterStr;
70+ int blinkDelay = 1000 ;
7071
7172/* private functions ======================================================== */
7273void mode (int argc, char *argv[]);
@@ -87,6 +88,7 @@ void cwrite (int argc, char *argv[]);
8788void cread (int argc, char *argv[]);
8889void cmode (int argc, char *argv[]);
8990void ctoggle (int argc, char *argv[]);
91+ void cblink (int argc, char *argv[]);
9092
9193Pin *getPin (char *c_str);
9294void usage ();
@@ -121,13 +123,13 @@ main (int argc, char **argv) {
121123 {" cwrite" , cwrite},
122124 {" cread" , cread},
123125 {" cmode" , cmode},
124- {" ctoggle" , ctoggle}
125-
126+ {" ctoggle" , ctoggle},
127+ { " cblink " , cblink}
126128 };
127129
128130 try {
129131 /* Traitement options ligne de commande */
130- while ( (opt = getopt (argc, argv, " gs1Dhfvwxmadc:" )) != -1 ) {
132+ while ( (opt = getopt (argc, argv, " gs1Dhfvwxmadc:b: " )) != -1 ) {
131133
132134 switch (opt) {
133135
@@ -186,6 +188,13 @@ main (int argc, char **argv) {
186188 converterStr = optarg;
187189 break ;
188190
191+ case ' b' :
192+ blinkDelay = stoi (string (optarg));
193+ if (blinkDelay < 2 ) {
194+ blinkDelay = 2 ;
195+ }
196+ break ;
197+
189198 default :
190199 /* An invalid option has been used, exit with code EXIT_FAILURE */
191200 exit (EXIT_FAILURE);
@@ -483,22 +492,21 @@ blink (int argc, char *argv[]) {
483492 throw Exception (Exception::PinNumberExpected);
484493 }
485494 else {
486- int period = 1000 ;
487495
488496 gpio.setReleaseOnClose (true );
489497
490498 pin = getPin (argv[optind]);
491499 if (paramc > 1 ) {
492500
493- period = stoi (string (argv[optind + 1 ]));
494- if (period < 1 ) {
495- period = 1 ;
496- cout << " Warning: Pin " << pin->name () << " the period has been set to " << period << " ms (min.) !" << endl;
501+ blinkDelay = stoi (string (argv[optind + 1 ]));
502+ if (blinkDelay < 2 ) {
503+ blinkDelay = 2 ;
504+ cout << " Warning: Pin " << pin->name () << " the period has been set to " << blinkDelay << " ms (min.) !" << endl;
497505 }
498506 }
499507
500508 pin->setMode (Pin::ModeOutput);
501- period /= 2 ;
509+ blinkDelay /= 2 ;
502510
503511 // sig_handler() intercepte le CTRL+C
504512 signal (SIGINT, sig_handler);
@@ -507,7 +515,7 @@ blink (int argc, char *argv[]) {
507515
508516 while (!should_exit.load ()) {
509517 pin->toggle ();
510- clk.delay (period );
518+ clk.delay (blinkDelay );
511519 }
512520 }
513521}
@@ -899,6 +907,85 @@ ctoggle (int argc, char *argv[]) {
899907 }
900908}
901909
910+
911+ /* -----------------------------------------------------------------------------
912+ cblink "-c <converter[:parameters]>" <chan> [period]
913+ Blinks the given pin on/off (explicitly sets the pin to output)
914+ */
915+ void
916+ cblink (int argc, char *argv[]) {
917+ std::unique_ptr<Converter> conv (Converter::factory (converterStr));
918+
919+ if (conv->type () != Converter::GpioExpander || ( (conv->flags () & Converter::hasToggle) == 0 )) {
920+
921+ throw Exception (Exception::ConverterUnknown, converterStr);
922+ }
923+ else {
924+ int paramc = (argc - optind);
925+ Clock clk;
926+ int chan = -1 ;
927+ Converter::Mode mode = Converter::NoMode;
928+
929+ if (paramc > 0 ) {
930+
931+ chan = stoi (string (argv[optind]));
932+ }
933+
934+ conv->setDebug (debug);
935+ if (!conv->open ()) {
936+
937+ throw Exception (Exception::ConverterOpenError, conv->deviceName ());
938+ }
939+ conv->setEnable (true );
940+
941+ if (chan >= 0 ) {
942+
943+ mode = conv->mode (chan); // backup current mode
944+ }
945+
946+ if (!conv->setMode (Converter::DigitalOutput, chan)) {
947+
948+ throw Exception (Exception::ConverterModeError, conv->deviceName ());
949+ }
950+
951+ blinkDelay = (blinkDelay / 2 ) - 1 ;
952+
953+ // sig_handler() intercepte le CTRL+C
954+ signal (SIGINT, sig_handler);
955+ signal (SIGTERM, sig_handler);
956+ cout << " Press Ctrl+C to abort ..." << endl;
957+
958+ while (!should_exit.load ()) {
959+ if (!conv->toggle (chan)) {
960+
961+ throw Exception (Exception::ConverterWriteError, conv->deviceName ());
962+ }
963+ clk.delay (blinkDelay);
964+ }
965+
966+ // restore
967+ if (chan >= 0 ) {
968+ // If a channel was specified, write the last value to it
969+ if (!conv->writeChannel (false , chan)) {
970+
971+ throw Exception (Exception::ConverterWriteError, conv->deviceName ());
972+ }
973+ if (!conv->setMode (mode, chan)) {
974+
975+ throw Exception (Exception::ConverterModeError, conv->deviceName ());
976+ }
977+ }
978+ else {
979+ // Write the last value to all channels
980+ if (!conv->write (0 )) {
981+
982+ throw Exception (Exception::ConverterWriteError, conv->deviceName ());
983+ }
984+ }
985+ }
986+ }
987+
988+
902989/* -----------------------------------------------------------------------------
903990 wfi <pin> <rising/falling/both> [timeout_ms]
904991 This set the given pin to the supplied interrupt mode: rising, falling or
@@ -1241,7 +1328,7 @@ usage () {
12411328 cout << " Write the given boolean value (0 or 1) to the specified pin (output)." << endl;
12421329 cout << " toggle <pin>" << endl;
12431330 cout << " Change the state of a GPIO pin; 0 to 1, or 1 to 0 (output)." << endl;
1244- cout << " blink <pin> [ period]" << endl;
1331+ cout << " blink [-b period] <pin> " << endl;
12451332 cout << " Blink the specified pin on/off (explicitly sets the pin to output)." << endl;
12461333 cout << " wfi <pin> <rising/falling/both> [timeout_ms]" << endl;
12471334 cout << " Wait for the interrupt to occur. This is a non-busy wait." << endl;
0 commit comments