mirror of
https://github.com/eddyem/IR-controller.git
synced 2026-03-20 08:40:57 +03:00
modified client.c
This commit is contained in:
@@ -31,7 +31,8 @@
|
||||
#include <stdint.h> // int types
|
||||
#include <sys/time.h> // gettimeofday
|
||||
|
||||
int zmon_period = 30; // monitor temperature each 30s
|
||||
#define BUFLEN 1024
|
||||
|
||||
double t0; // start time
|
||||
|
||||
FILE *fout = NULL; // file for messages duplicating
|
||||
@@ -70,6 +71,11 @@ void quit(int ex_stat){
|
||||
* Open & setup TTY, terminal
|
||||
*/
|
||||
void tty_init(){
|
||||
// terminal without echo
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
newt = oldt;
|
||||
newt.c_lflag &= ~(ICANON | ECHO);
|
||||
if(tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0) quit(-2);
|
||||
printf("\nOpen port...\n");
|
||||
if ((comfd = open(comdev,O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0){
|
||||
fprintf(stderr,"Can't use port %s\n",comdev);
|
||||
@@ -84,107 +90,86 @@ void tty_init(){
|
||||
tty.c_cc[VMIN] = 0; // non-canonical mode
|
||||
tty.c_cc[VTIME] = 5;
|
||||
if(ioctl(comfd,TCSETA,&tty) < 0) exit(-1); // set new mode
|
||||
// terminal without echo
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
newt = oldt;
|
||||
newt.c_lflag &= ~(ICANON | ECHO);
|
||||
if(tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0) quit(-2);
|
||||
printf(" OK\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read character from console without echo
|
||||
* @return char readed
|
||||
*/
|
||||
int read_console(){
|
||||
int rb;
|
||||
struct timeval tv;
|
||||
int retval;
|
||||
fd_set rfds;
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(STDIN_FILENO, &rfds);
|
||||
tv.tv_sec = 0; tv.tv_usec = 10000;
|
||||
retval = select(1, &rfds, NULL, NULL, &tv);
|
||||
if(!retval) rb = 0;
|
||||
else {
|
||||
if(FD_ISSET(STDIN_FILENO, &rfds)) rb = getchar();
|
||||
else rb = 0;
|
||||
}
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
||||
return rb;
|
||||
}
|
||||
|
||||
/**
|
||||
* getchar() without echo
|
||||
* wait until at least one character pressed
|
||||
* @return character readed
|
||||
*/
|
||||
int mygetchar(){ // аналог getchar() без необходимости жать Enter
|
||||
*
|
||||
int mygetchar(){
|
||||
int ret;
|
||||
do ret = read_console();
|
||||
while(ret == 0);
|
||||
return ret;
|
||||
}
|
||||
}*/
|
||||
|
||||
/**
|
||||
* Read data from TTY
|
||||
* @param buff (o) - buffer for data read
|
||||
* @param length - buffer len
|
||||
* @return amount of readed bytes
|
||||
* read both tty & console
|
||||
* @param buff (o) - buffer for messages readed from tty
|
||||
* @param length (io) - buff's length (return readed len or 0)
|
||||
* @param rb (o) - byte readed from console or -1
|
||||
* @return 1 if something was readed here or there
|
||||
*/
|
||||
size_t read_tty(uint8_t *buff, size_t length){
|
||||
ssize_t L = 0;
|
||||
fd_set rfds;
|
||||
int read_tty_and_console(char *buff, size_t *length, int *rb){
|
||||
ssize_t L;
|
||||
struct timeval tv;
|
||||
int retval;
|
||||
int sel, retval = 0;
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(STDIN_FILENO, &rfds);
|
||||
FD_SET(comfd, &rfds);
|
||||
tv.tv_sec = 0; tv.tv_usec = 500000; // wait for 500ms
|
||||
retval = select(comfd + 1, &rfds, NULL, NULL, &tv);
|
||||
if (!retval) return 0;
|
||||
if(FD_ISSET(comfd, &rfds)){
|
||||
if((L = read(comfd, buff, length)) < 1) return 0;
|
||||
tv.tv_sec = 0; tv.tv_usec = 10000;
|
||||
sel = select(comfd + 1, &rfds, NULL, NULL, &tv);
|
||||
if(sel > 0){
|
||||
if(FD_ISSET(STDIN_FILENO, &rfds)){
|
||||
*rb = getchar();
|
||||
retval = 1;
|
||||
}else{
|
||||
*rb = -1;
|
||||
}
|
||||
if(FD_ISSET(comfd, &rfds)){
|
||||
if((L = read(comfd, buff, *length)) < 1){ // disconnect or other troubles
|
||||
fprintf(stderr, "USB error or disconnected!\n");
|
||||
quit(1);
|
||||
}else{
|
||||
if(L == 0){ // USB disconnected
|
||||
fprintf(stderr, "USB disconnected!\n");
|
||||
quit(1);
|
||||
}
|
||||
*length = (size_t) L;
|
||||
retval = 1;
|
||||
}
|
||||
}else{
|
||||
*length = 0;
|
||||
}
|
||||
}
|
||||
return (size_t)L;
|
||||
return retval;
|
||||
}
|
||||
|
||||
void help(){
|
||||
printf("Use this commands:\n"
|
||||
"h\tShow this help\n"
|
||||
"q\tQuit\n"
|
||||
"t\tMonitor temperature on ZakWire\n"
|
||||
"v\tNon-verbose\n"
|
||||
"z\tGet ZakWire data\n"
|
||||
"V\tVerbose\n"
|
||||
);
|
||||
}
|
||||
|
||||
#define dup_pr(...) do{printf(__VA_ARGS__); if(fout) fprintf(fout, __VA_ARGS__);}while(0)
|
||||
|
||||
int zflag = 0; // monitor zakwire
|
||||
void con_sig(int rb){
|
||||
uint8_t cmd;
|
||||
char cmd;
|
||||
if(rb < 1) return;
|
||||
if(rb == 'q') quit(0); // q == exit
|
||||
switch(rb){
|
||||
cmd = (char) rb;
|
||||
write(comfd, &cmd, 1);
|
||||
/*switch(rb){
|
||||
case 'h':
|
||||
help();
|
||||
break;
|
||||
case 's':
|
||||
dup_pr("Stop ZakWire\n");
|
||||
zflag = 0;
|
||||
break;
|
||||
case 't':
|
||||
dup_pr("ZakWire thermal monitoring\n");
|
||||
zflag = 1;
|
||||
write(comfd, "v", 1);
|
||||
t0 = dtime() - zmon_period;
|
||||
break;
|
||||
default:
|
||||
cmd = (uint8_t) rb;
|
||||
write(comfd, &cmd, 1);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -193,11 +178,7 @@ void con_sig(int rb){
|
||||
* @param len - length of data in buffer (could be 2 or 4)
|
||||
* @return
|
||||
*/
|
||||
uint32_t get_int(uint8_t *buff, size_t len){
|
||||
/* int i;
|
||||
printf("read %zd bytes: ", len);
|
||||
for(i = 0; i < len; i++) printf("0x%x ", buff[i]);
|
||||
printf("\n");*/
|
||||
uint32_t get_int(char *buff, size_t len){
|
||||
if(len != 2 && len != 4){
|
||||
fprintf(stdout, "Bad data length!\n");
|
||||
return 0xffffffff;
|
||||
@@ -209,9 +190,42 @@ uint32_t get_int(uint8_t *buff, size_t len){
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy line by line buffer buff to file removing cmd starting from newline
|
||||
* @param buffer - data to put into file
|
||||
* @param cmd - symbol to remove from line startint (if found, change *cmd to (-1)
|
||||
* or NULL, (-1) if no command to remove
|
||||
*/
|
||||
void copy_buf_to_file(char *buffer, int *cmd){
|
||||
char *buff, *line, *ptr;
|
||||
if(!cmd || *cmd < 0){
|
||||
fprintf(fout, "%s", buffer);
|
||||
return;
|
||||
}
|
||||
buff = strdup(buffer), ptr = buff;
|
||||
do{
|
||||
if(!*ptr) break;
|
||||
if(ptr[0] == (char)*cmd){
|
||||
*cmd = -1;
|
||||
ptr++;
|
||||
if(ptr[0] == '\n') ptr++;
|
||||
if(!*ptr) break;
|
||||
}
|
||||
line = ptr;
|
||||
ptr = strchr(buff, '\n');
|
||||
if(ptr){
|
||||
*ptr++ = 0;
|
||||
//fprintf(fout, "%s\n", line);
|
||||
}//else
|
||||
//fprintf(fout, "%s", line); // no newline found in buffer
|
||||
fprintf(fout, "%s\n", line);
|
||||
}while(ptr);
|
||||
free(buff);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int rb;
|
||||
uint8_t buff[128];
|
||||
int rb, oldcmd = -1;
|
||||
char buff[BUFLEN+1];
|
||||
size_t L;
|
||||
if(argc == 2){
|
||||
fout = fopen(argv[1], "a");
|
||||
@@ -229,34 +243,19 @@ int main(int argc, char *argv[]){
|
||||
setbuf(stdout, NULL);
|
||||
t0 = dtime();
|
||||
while(1){
|
||||
rb = read_console();
|
||||
if(rb > 0) con_sig(rb);
|
||||
L = read_tty(buff, 127);
|
||||
if(L){
|
||||
buff[L] = 0;
|
||||
printf("TTY: %s\n", buff);
|
||||
if(fout) fprintf(fout, "%zd\t%s\n", time(NULL), buff);
|
||||
}
|
||||
if(zflag && dtime() - t0 > zmon_period){ // thermal monitoring
|
||||
t0 += zmon_period;
|
||||
char NUM[] = {'6', '5', '3'};
|
||||
char obuf[] = {'N', 0, 'z'};
|
||||
int i;
|
||||
double temper[3];
|
||||
for(i = 0; i < 3; i++){
|
||||
obuf[1] = NUM[i];
|
||||
if(3 != write(comfd, &obuf, 3)){
|
||||
perror("Can't write");
|
||||
quit(-1);
|
||||
}
|
||||
L = read_tty(buff, 127);
|
||||
//for(j = 0; j < 10; j++) // 10 tries to read data from USB
|
||||
// if((L = read_tty(buff, 127)) < 1) continue;
|
||||
uint32_t ans = get_int(buff, L);
|
||||
if(ans == 0xffffffff) temper[i] = -274.;
|
||||
else temper[i] = ((double)ans)/2047.*70. - 10.;
|
||||
L = BUFLEN;
|
||||
if(read_tty_and_console(buff, &L, &rb)){
|
||||
if(rb > 0){
|
||||
con_sig(rb);
|
||||
oldcmd = rb;
|
||||
}
|
||||
if(L){
|
||||
buff[L] = 0;
|
||||
printf("%s", buff);
|
||||
if(fout){
|
||||
copy_buf_to_file(buff, &oldcmd);
|
||||
}
|
||||
}
|
||||
dup_pr("%zd\t%.2f\t%.2f\t%.2f\n", time(NULL), temper[0], temper[1], temper[2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user