mirror of
https://github.com/eddyem/mmpp.git
synced 2026-03-20 08:40:59 +03:00
add base turrets managing
This commit is contained in:
63
MMPP_lib/examples/template.c
Normal file
63
MMPP_lib/examples/template.c
Normal 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);
|
||||
}
|
||||
86
MMPP_lib/examples/templatecmdlnopts.c
Normal file
86
MMPP_lib/examples/templatecmdlnopts.c
Normal 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);
|
||||
}
|
||||
40
MMPP_lib/examples/templatecmdlnopts.h
Normal file
40
MMPP_lib/examples/templatecmdlnopts.h
Normal 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__
|
||||
143
MMPP_lib/examples/wheels.c
Normal file
143
MMPP_lib/examples/wheels.c
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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 <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){
|
||||
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");
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < found; ++i){
|
||||
while(WHEEL_MOVING == wheel_getpos(&wheels[i])){
|
||||
usleep(100000);
|
||||
}
|
||||
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){
|
||||
while(G->wh_ids[Nw]) ++Nw;
|
||||
}
|
||||
if(G->gotopos){
|
||||
while(G->gotopos[Ng]) ++Ng;
|
||||
}
|
||||
if(Nw != Ng){
|
||||
WARNX(_("Amoung of `--wheel-id` should be equal to amount of `--goto`!"));
|
||||
}else{
|
||||
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{
|
||||
while(WHEEL_MOVING == wheel_getpos(w)){
|
||||
DBG("still moving");
|
||||
usleep(100000);
|
||||
}
|
||||
int curpos = wheel_getpos(w);
|
||||
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);
|
||||
}
|
||||
87
MMPP_lib/examples/wheelscmdlnopts.c
Normal file
87
MMPP_lib/examples/wheelscmdlnopts.c
Normal 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);
|
||||
}
|
||||
43
MMPP_lib/examples/wheelscmdlnopts.h
Normal file
43
MMPP_lib/examples/wheelscmdlnopts.h
Normal 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__
|
||||
Reference in New Issue
Block a user