remove deprecated code

This commit is contained in:
2022-03-10 12:19:52 +03:00
parent 12bd669ff6
commit a57a78adee
52 changed files with 428 additions and 2323 deletions

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.9)
project(examples)
link_libraries(mmpp)
include_directories(../)
add_executable(testmove tmcmdlnopts.c tm.c)
target_link_libraries(testmove -lusefull_macros)
install(TARGETS testmove DESTINATION "bin")
add_executable(wheels wheelscmdlnopts.c wheels.c)
target_link_libraries(wheels -lusefull_macros)
install(TARGETS wheels DESTINATION "bin")

View File

@@ -0,0 +1,63 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tmcmdlnopts.h"
#include <signal.h>
#include <stdio.h>
#include <libmmpp.h>
#include <usefull_macros.h>
// All return states of main():
enum{
RET_ALLOK = 0,
RET_NOTFOUND, // none of turrets found or didn't found seeking MCU
RET_ONLYONE, // only one turret found
RET_COMMERR, // communication error
RET_CANTINIT, // can't init turrets
RET_ERROR = 9, // uncoverable error - from libsnippets
RET_HELPCALL = 255 // user call help (or give wrong parameter[s]) - from libsnippets
};
static glob_pars *G;
/**
* We REDEFINE the default WEAK function of signal processing
*/
void __attribute__((noreturn)) signals(int sig){
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
}
if(G->pidfile) // remove unnesessary PID file
unlink(G->pidfile);
restore_console();
mmpp_close();
exit(sig);
}
int main(int argc, char **argv){
initial_setup();
signal(SIGTERM, signals); // kill (-15)
signal(SIGINT, signals); // ctrl+C
signal(SIGQUIT, SIG_IGN); // ctrl+\ .
signal(SIGTSTP, SIG_IGN); // ctrl+Z
setbuf(stdout, NULL);
G = parse_args(argc, argv);
check4running(NULL, G->pidfile);
;
signals(0);
}

View File

@@ -0,0 +1,86 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "templatecmdlnopts.h"
#include <assert.h>
#include <libmmpp.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
int quiet = 0; // less messages @ stdout
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.pidfile = "/tmp/MMPP_wheels.pid"
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_none, APTR(&help), N_("show this help")},
{"quiet", NO_ARGS, NULL, 'q', arg_none, APTR(&quiet), N_("don't show anything @screen from stdout")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), N_("PID-file name")},
{"status", NO_ARGS, NULL, 'S', arg_none, APTR(&G.getstatus), N_("get device status")},
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr;
ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
// format of help: "Usage: progname [args]\n"
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
WARNX("%d unused parameters:\n", argc);
for(int i = 0; i < argc; ++i)
printf("\t%4d: %s\n", i+1, argv[i]);
}
return &G;
}
/**
* @brief MSG show coloured message if `quiet` not set
* !! This function adds trailing '\n' to message
* @param s1 - green part of message (may be null)
* @param s2 - normal colored part of messate (may be null)
*/
void MSG(const char *s1, const char *s2){
if(quiet) return;
if(s1){
green("%s%s", s1, s2 ? ": " : "\n");
}
if(s2) printf("%s\n", s2);
}

View File

@@ -0,0 +1,40 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TMCMDLNOPTS_H__
#define TMCMDLNOPTS_H__
#include <usefull_macros.h>
/*
* here are some typedef's for global data
*/
typedef struct{
char *pidfile; // pid file name
int gettemp; // get MCU temperature
int getstatus; // get status of all devices
} glob_pars;
// default & global parameters
extern glob_pars const Gdefault;
extern int quiet;
glob_pars *parse_args(int argc, char **argv);
void MSG(const char *s1, const char *s2);
#endif // TMCMDLNOPTS_H__

264
MMPP:lib/examples/tm.c Normal file
View File

@@ -0,0 +1,264 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Motors testing tool *
******************************************************************************/
#include "tmcmdlnopts.h"
#include <signal.h>
#include <stdio.h>
#include <libmmpp.h>
#include <usefull_macros.h>
// All return states of main():
enum{
RET_ALLOK = 0,
RET_NOTFOUND, // none of MCU found or didn't found seeking MCU
RET_ONLYONE, // only one MCU found
RET_COMMERR, // communication error
RET_CANTINIT, // can't init motors: error during initiation or some of motors are moving
RET_WAITERR, // error occured during waiting procedure
RET_ERROR = 9, // uncoverable error - from libsnippets
RET_HELPCALL = 255 // user call help (or give wrong parameter[s]) - from libsnippets
};
static glob_pars *G;
/**
* We REDEFINE the default WEAK function of signal processing
*/
void __attribute__((noreturn)) signals(int sig){
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
}
if(G->pidfile) // remove unnesessary PID file
unlink(G->pidfile);
restore_console();
mmpp_close();
exit(sig);
}
static double convangle(double val){
int X = (int)(val / 360.);
val -= 360. * (double)X;
return val;
}
static void parsestatus(ttysend_status st){
if(quiet) return;
switch(st){
case SEND_ERR:
red("communication error");
break;
case SEND_ALLOK:
green("all OK");
break;
case SEND_ACTIVE:
red("motor is still moving");
break;
case SEND_TOOBIG:
red("the steps amount is too large");
break;
case SEND_ZEROMOVE:
green("already at position");
break;
case SEND_ESWITCH:
red("on end-switch and can't move further");
break;
case SEND_NEEDINIT:
red("motors aren't initialised");
break;
case SEND_NEGATMOVE:
red("try to move into negative position");
break;
case SEND_OTHER:
default:
red("other error (wrong motor/MCU number?)");
}
printf("\n");
}
static void pollstatus(){
motor_state S;
bool mvng[3] = {false, true, true}, starting = true;
int curpos[4];
double adc[6];
while(mvng[1] || mvng[2]){
for(int Nmcu = 1; Nmcu < 3; ++Nmcu){
if(!mot_getstatus(Nmcu, &S)){
mvng[Nmcu] = false;
continue;
}
if(S.state[0] == STP_SLEEP && S.state[1] == STP_SLEEP)
mvng[Nmcu] = false;
curpos[2*(Nmcu-1)+0] = S.curpos[0];
curpos[2*(Nmcu-1)+1] = S.curpos[1];
if(G->getADC){
ADC_state s;
for(int Nmcu = 1; Nmcu < 3; ++Nmcu){
if(get_ADC(Nmcu, &s)){
adc[3*(Nmcu-1)+0] = s.Vdd;
adc[3*(Nmcu-1)+1] = s.Imot;
adc[3*(Nmcu-1)+2] = s.Vmot;
}
}
}
}
if(!mvng[1] && !mvng[2]){
if(starting){
starting = false;
continue;
}
}else starting = false;
if(!quiet){
printf("1: %6d, %6d", curpos[0],curpos[1]);
if(G->getADC){
printf(", VDD=%-5.1f Imot=%-5.1f Vmot=%-5.1f", adc[0], adc[1], adc[2]);
}
printf(" 2: %6d, %6d", curpos[2],curpos[3]);
if(G->getADC){
printf(", VDD=%-5.1f Imot=%-5.1f Vmot=%-5.1f", adc[3], adc[4], adc[5]);
}
printf(" \r");
}
}
printf("\n\n");
}
int main(int argc, char **argv){
initial_setup();
signal(SIGTERM, signals); // kill (-15)
signal(SIGINT, signals); // ctrl+C
signal(SIGQUIT, SIG_IGN); // ctrl+\ .
signal(SIGTSTP, SIG_IGN); // ctrl+Z
setbuf(stdout, NULL);
G = parse_args(argc, argv);
check4running(NULL, G->pidfile);
DBG("Try to open serial %s", G->comdev);
if(mmpp_tryopen(G->comdev, G->speed)){
ERRX(_("Can't open %s with speed %d. Exit."), G->comdev, G->speed);
}
if(mot_handshake()){
WARNX(_("Controllers not found"));
signals(RET_NOTFOUND);
}
for(int i = 1; i < 3; ++i){
if(get_alive(i)){
if(!quiet) green("MCU #%d found\n", i);
}else WARNX(_("MCU #%d not found"), i);
}
if(G->stopall){
int r = stop_all();
if(r) WARNX(_("Error for %d motors of 4"), r);
else MSG("Successfully send command to stop all", NULL);
}
if(G->gettemp){
double t1, t2;
if(get_temp(&t1, &t2)){
MSG("Got MCU temp:", NULL);
if(t1 > -300.) printf("\tTMCU1=%gdegrC\n", t1);
if(t2 > -300.) printf("\tTMCU2=%gdegrC\n", t2);
}else WARNX(_("Can't get MCU temp"));
}
if(G->getstatus){
for(int N = 1; N < 3; ++N){
motor_state s;
if(mot_getstatus(N, &s)){
if(get_rst(N, true))
WARNX(_("Controller #%d was in reset state"), N);
if(!quiet) green("MCU#%d state:\n", N);
for(int i = 0; i < 2; ++i){
printf("\tSTATE[%d]=%d\n",i, s.state[i]);
printf("\tSTEPSLEFT[%d]=%d\n", i, s.stepsleft[i]);
printf("\tCURPOS[%d]=%d\n", i, s.curpos[i]);
for(int j = 0; j < 2; ++j)
printf("\tESW_STATE[%d][%d]=%d\n", i, j, s.ESW_status[i][j]);
}
}
}
}
if(G->sendraw){
char **raw = G->sendraw;
do{
MSG(_("Send raw string"), *raw);
char *got = tty_sendraw(*raw);
if(got){
MSG(_("Receive"), got);
if(quiet) printf("%s", got);
}else WARNX(_("Nothing received"));
}while(*(++raw));
}
if(G->getADC){
MSG(_("Get ADC values"), NULL);
ADC_state s;
for(int i = 1; i < 3; ++i){
if(get_ADC(i, &s)){
printf("VDD%d=%g\n", i, s.Vdd);
printf("IMOT%d=%g\n", i, s.Imot);
printf("VMOT%d=%g\n", i, s.Vmot);
}else WARNX(_("Can't get ADC values for MCU#%d"), i);
}
}
if(G->rot1angle > -999. || G->rot2angle > -999. || G->l1steps != INT_MAX || G->l2steps != INT_MAX){
// all other commands are tied with moving, so check if motors are inited
MSG("Init motors", NULL);
// move all uninitialized motors to their zero position
int ini = init_motors(); // BLOCKING call!!!
// you can use non-blocking initialisation by proper rewriting of `init_motors`
if(ini){
WARNX(_("Can't init motors: %d\n"), ini);
signals(RET_CANTINIT);
}else green("Motors are ready!\n");
ttysend_status st;
if(G->rot1angle > -999.){
double angle = convangle(G->rot1angle);
int steps = (int)((STEPSREV1/360.) * angle);
MSG("Try to rotate polaroid ...", NULL);
st = movemotor(1, 1, steps, G->absmove);
parsestatus(st);
}
if(G->rot2angle > -999.){
double angle = convangle(G->rot2angle);
int steps = (int)((STEPSREV2/360.) * angle);
MSG("Try to rotate waveplate ...", NULL);
st = movemotor(2, 1, steps, G->absmove);
parsestatus(st);
}
if(G->l1steps != INT_MAX){
MSG("Try to move polaroid stage ...", NULL);
st = movemotor(1, 0, G->l1steps, G->absmove);
parsestatus(st);
}
if(G->l2steps != INT_MAX){
MSG("Try to move waveplate stage ...", NULL);
st = movemotor(2, 0, G->l2steps, G->absmove);
parsestatus(st);
}
}
pollstatus();
if(G->reset){
int **N = G->reset;
while(*N){
if(!quiet) green("Reset controller #%d\n", **N);
reset_MCU(**N);
++N;
}
}
signals(0);
}

View File

@@ -0,0 +1,104 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tmcmdlnopts.h"
#include <assert.h>
#include <libmmpp.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
int quiet = 0; // less messages @ stdout
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.comdev = "/dev/ttyUSB0"
,.pidfile = "/tmp/MMPP_con.pid"
,.speed = BAUD_RATE
,.rot1angle = -1000.
,.rot2angle = -1000.
,.l1steps = INT_MAX
,.l2steps = INT_MAX
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_none, APTR(&help), N_("show this help")},
{"quiet", NO_ARGS, NULL, 'q', arg_none, APTR(&quiet), N_("don't show anything @screen from stdout")},
{"comdev", NEED_ARG, NULL, 'd', arg_string, APTR(&G.comdev), N_("terminal device filename")},
{"baudrate",NEED_ARG, NULL, 'b', arg_int, APTR(&G.speed), N_("TTY baudrate")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), N_("PID-file name")},
{"stopall", NO_ARGS, NULL, 's', arg_none, APTR(&G.stopall), N_("stop all motors")},
{"gettemp", NO_ARGS, NULL, 't', arg_none, APTR(&G.gettemp), N_("get MCU temperature")},
{"status", NO_ARGS, NULL, 'S', arg_none, APTR(&G.getstatus), N_("get device status")},
{"sendraw", MULT_PAR, NULL, 'W', arg_string, APTR(&G.sendraw), N_("send raw command (you can use this flag several times)")},
{"getADC", NO_ARGS, NULL, 'A', arg_none, APTR(&G.getADC), N_("get ADC values for both MCUs")},
{"absmove", NO_ARGS, NULL, 'a', arg_none, APTR(&G.absmove), N_("absolute move (without this flag moving is relative)")},
{"rot1", NEED_ARG, NULL, 'R', arg_double, APTR(&G.rot1angle), N_("rotate polaroid to given angle")},
{"rot2", NEED_ARG, NULL, 'r', arg_double, APTR(&G.rot2angle), N_("rotate lambda/4 to given angle")},
{"lin1", NEED_ARG, NULL, 'L', arg_int, APTR(&G.l1steps), N_("move polaroid linear stage to N steps")},
{"lin2", NEED_ARG, NULL, 'l', arg_int, APTR(&G.l2steps), N_("move wave-plate linear stage to N steps")},
{"reset", MULT_PAR, NULL, 'E', arg_int, APTR(&G.reset), N_("reset given mcu (may be included several times)")},
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr;
ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
// format of help: "Usage: progname [args]\n"
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
WARNX("%d unused parameters:\n", argc);
for(int i = 0; i < argc; ++i)
printf("\t%4d: %s\n", i+1, argv[i]);
}
return &G;
}
/**
* @brief MSG show coloured message if `quiet` not set
* !! This function adds trailing '\n' to message
* @param s1 - green part of message (may be null)
* @param s2 - normal colored part of messate (may be null)
*/
void MSG(const char *s1, const char *s2){
if(quiet) return;
if(s1){
green("%s%s", s1, s2 ? ": " : "\n");
}
if(s2) printf("%s\n", s2);
}

View File

@@ -0,0 +1,51 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TMCMDLNOPTS_H__
#define TMCMDLNOPTS_H__
#include <usefull_macros.h>
/*
* here are some typedef's for global data
*/
typedef struct{
char *comdev; // TTY device
char *pidfile; // pid file name
int gettemp; // get MCU temperature
int stopall; // stop all motors
int speed; // TTY speed
int getstatus; // get status of all devices
char **sendraw; // send raw command[s]
int getADC; // get ADC values
double rot1angle; // rotator 1 angle
double rot2angle; // rotator 2 angle
int l1steps; // move linear stage 1 (polaroid) for N steps
int l2steps; // move linear stage 2 (L/4) for N steps
int absmove; // absolute move (to given position from zero-esw)
int **reset; // reset given MCU's
} glob_pars;
// default & global parameters
extern glob_pars const Gdefault;
extern int quiet;
glob_pars *parse_args(int argc, char **argv);
void MSG(const char *s1, const char *s2);
#endif // TMCMDLNOPTS_H__

149
MMPP:lib/examples/wheels.c Normal file
View File

@@ -0,0 +1,149 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/******************************************************************************
* Wheels testing tool *
******************************************************************************/
#include "wheelscmdlnopts.h"
#include <signal.h>
#include <stdio.h>
#include <libmmpp.h>
#include <usefull_macros.h>
// All return states of main():
enum{
RET_ALLOK = 0,
RET_NOTFOUND, // none of turrets found or didn't found seeking MCU
RET_ONLYONE, // only one turret found
RET_COMMERR, // communication error
RET_CANTINIT, // can't init turrets
RET_ERROR = 9, // uncoverable error - from libsnippets
RET_HELPCALL = 255 // user call help (or give wrong parameter[s]) - from libsnippets
};
static glob_pars *G;
static wheel_descr *wheels;
static int found;
/**
* We REDEFINE the default WEAK function of signal processing
*/
void __attribute__((noreturn)) signals(int sig){
if(sig){
signal(sig, SIG_IGN);
DBG("Get signal %d, quit.\n", sig);
}
if(G->pidfile) // remove unnesessary PID file
unlink(G->pidfile);
restore_console();
del_wheels(wheels, found);
exit(sig);
}
/**
* @brief find_wheel_by_ID - find wheel by ID
* @param w - array of wheel descriptors
* @param N - length of w
* @param ID - sought wheel ID
* @return NULL if no such wheel or pointer to it
*/
wheel_descr *find_wheel_by_ID(wheel_descr *w, int N, char ID){
for(int i = 0; i < N; ++i){
if(w[i].ID == ID) return &w[i];
}
return NULL;
}
int main(int argc, char **argv){
initial_setup();
signal(SIGTERM, signals); // kill (-15)
signal(SIGINT, signals); // ctrl+C
signal(SIGQUIT, SIG_IGN); // ctrl+\ .
signal(SIGTSTP, SIG_IGN); // ctrl+Z
setbuf(stdout, NULL);
G = parse_args(argc, argv);
check4running(NULL, G->pidfile);
wheel_error werr;
found = find_wheels(&wheels, &werr);
if(found == 0) ERRX(_("No wheels found"));
else green("Found %d wheels\n", found);
if(werr != WHERR_ALLOK){
WARNX(_("Got wheel error: %d"), werr);
signals(1);
}
if(G->getstatus){
for(int i = 0; i < found; ++i){
green("Wheel #%d:\n", i);
printf("\tserial: %s\n", wheels[i].serial);
printf("\tID: %c\n", wheels[i].ID);
printf("\tname: %s\n", wheels[i].name);
printf("\tmaxpos: %d\n\n", wheels[i].maxpos);
}
}
if(G->gohome){ // non-blocking moving to home position
for(int i = 0; i < found; ++i){
if(!wheel_home(&wheels[i])) WARNX(_("Can't move wheel %c to home position"), wheels[i].ID);
else{
green("Wheel %c is moving to home position\n");
}
}
// now we can wait until all wheels reach home position
for(int i = 0; i < found; ++i){
while(WHEEL_MOVING == wheel_getpos(&wheels[i])){
usleep(100000);
}
// we should check current position because wheel can be blocked and don't move
if(wheel_getpos(&wheels[i]) != 1)
WARNX(_("Wheel %c didn't reach home position"), wheels[i].ID);
}
}
int Nw = 0, Ng = 0;
if(G->wh_ids){ // count arguments of --wheel-id
while(G->wh_ids[Nw]) ++Nw;
}
if(G->gotopos){ // count arguments of --goto
while(G->gotopos[Ng]) ++Ng;
}
if(Nw != Ng){ // it's better to write --goto after each --wheel-id
WARNX(_("Amoung of `--wheel-id` should be equal to amount of `--goto`!"));
}else{ // here is an example of searching wheel by its ID and blocking moving
for(int i = 0; i < Nw; ++i){
char ID = *G->wh_ids[i];
DBG("id: %c, goto: %d", ID, *G->gotopos[i]);
wheel_descr *w = find_wheel_by_ID(wheels, found, ID);
if(!w) WARNX(_("No wheel with ID %c found!"), ID);
else{
int pos = *G->gotopos[i];
if(!move_wheel(w, pos)){
WARNX(_("Can't rotate wheel %c to position %d"), ID, pos);
wheel_clear_err(w);
}else{ // wait until wheel is moving
while(WHEEL_MOVING == wheel_getpos(w)){
DBG("still moving");
usleep(100000);
}
int curpos = wheel_getpos(w); // poll again to check current position
if(curpos != pos) WARNX(_("Wheel %c can't reach position %d, current position: %d"), ID, pos, curpos);
else green("Wheel %c is on position %d\n", ID, pos);
}
}
};
}
;
signals(0);
}

View File

@@ -0,0 +1,87 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wheelscmdlnopts.h"
#include <assert.h>
#include <libmmpp.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
int quiet = 0; // less messages @ stdout
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.pidfile = "/tmp/MMPP_wheels.pid"
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
{"help", NO_ARGS, NULL, 'h', arg_none, APTR(&help), N_("show this help")},
{"quiet", NO_ARGS, NULL, 'q', arg_none, APTR(&quiet), N_("don't show anything @screen from stdout")},
{"pidfile", NEED_ARG, NULL, 'p', arg_string, APTR(&G.pidfile), N_("PID-file name")},
{"status", NO_ARGS, NULL, 's', arg_none, APTR(&G.getstatus), N_("get device status")},
{"goto", MULT_PAR, NULL, 'g', arg_int, APTR(&G.gotopos), N_("go to given position")},
{"wheel-id",MULT_PAR, NULL, 'I', arg_string, APTR(&G.wh_ids), N_("name wheel by ID")},
{"home", NO_ARGS, NULL, 'H', arg_none, APTR(&G.gohome), N_("rotate all wheels to home position")},
end_option
};
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr;
ptr = memcpy(&G, &Gdefault, sizeof(G)); assert(ptr);
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
WARNX("%d unused parameters:\n", argc);
for(int i = 0; i < argc; ++i)
printf("\t%4d: %s\n", i+1, argv[i]);
}
return &G;
}
/**
* @brief MSG show coloured message if `quiet` not set
* !! This function adds trailing '\n' to message
* @param s1 - green part of message (may be null)
* @param s2 - normal colored part of messate (may be null)
*/
void MSG(const char *s1, const char *s2){
if(quiet) return;
if(s1){
green("%s%s", s1, s2 ? ": " : "\n");
}
if(s2) printf("%s\n", s2);
}

View File

@@ -0,0 +1,43 @@
/*
* This file is part of the libmmpp project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef TMCMDLNOPTS_H__
#define TMCMDLNOPTS_H__
#include <usefull_macros.h>
/*
* here are some typedef's for global data
*/
typedef struct{
char *pidfile; // pid file name
int gettemp; // get MCU temperature
int getstatus; // get status of all devices
char **wh_ids; // array of wheel ids
int **gotopos; // rotate wheels to given position
int gohome; // turn all wheels to home state
} glob_pars;
// default & global parameters
extern glob_pars const Gdefault;
extern int quiet;
glob_pars *parse_args(int argc, char **argv);
void MSG(const char *s1, const char *s2);
#endif // TMCMDLNOPTS_H__