📄 lcmd.c
字号:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/mman.h> #include <sys/stat.h>#include <fcntl.h>#include <unistd.h> #include <sys/socket.h> // socket();#include <sys/ioctl.h> // ioctl();#include <sys/types.h> // socket();#include <net/if.h> // struct ifreq#include <netinet/in.h>#include <math.h>#include "../atop_libs/libeeprom.h"#include <signal.h>#include <sys/wait.h>#include <sys/ipc.h>#include <sys/msg.h>#define CLEAR_DISPLAY 0x01 #define RETURN_HOME 0x02#define ENTRY_MODE_SET 0x06#define DISPLAY_ON_OFF_CONTROL 0x0E#define FUNCTION_SET 0x38 int fd2, count = 0, menu_n = 0;int op_port_number = 1;int serial_port_number = 1;unsigned char ip_tmp[4];unsigned char netmask_tmp[4];unsigned char gateway_tmp[4];int ip_flag = -1;int netmask_flag = -1;int gateway_flag = -1;int ntp_value = 0;// Message Queue#define MKEY1 1234L#define PERMS S_IRUSR|S_IWUSR#define BUFFER 255 struct msgtype { long mtype; char buffer[BUFFER + 1];};//-------------------------------------------------------------------------int atop_message_queue(int port_number) { int msgid, i = 0; struct msgtype msg; char tmp[4]; sprintf(tmp, "%d", port_number);/* if (argc != 2) { printf("Usage:%s string\n\a", argv[0]); exit(0); }*/ if ((msgid = msgget(MKEY1, PERMS)) < 0) printf("server: can't get message queue 1\n"); msg.mtype = 1; strncpy(msg.buffer, tmp, BUFFER); i = msgsnd(msgid, &msg, sizeof(struct msgtype), 0); memset(&msg, '\0', sizeof(struct msgtype));// exit(0); return 0; }//-------------------------------------------------------------------------// LCM initial;int lcm_init(void) { int fd, i = 0; unsigned char lcm[2]; do { usleep(10000); fd = open("/dev/atop_lcm", O_RDWR | O_SYNC); } while (fd < 0); // lcm[0] == command ; lcm[1] == data; usleep(50); lcm[0] = 0; lcm[1] = FUNCTION_SET; write(fd, &lcm, 2); usleep(50); lcm[0] = 0; lcm[1] = FUNCTION_SET; write(fd, &lcm, 2); usleep(50); lcm[0] = 0; lcm[1] = DISPLAY_ON_OFF_CONTROL;; write(fd, &lcm, 2); usleep(2000); lcm[0] = 0; lcm[1] = CLEAR_DISPLAY; write(fd, &lcm, 2); usleep(50); lcm[0] = 0; lcm[1] = ENTRY_MODE_SET; write(fd, &lcm, 2); usleep(50); lcm[0] = 0; lcm[1] = DISPLAY_ON_OFF_CONTROL; write(fd, &lcm, 2); usleep(2000); lcm[0] = 0; lcm[1] = CLEAR_DISPLAY; write(fd, &lcm, 2); close(fd); return 0;}//-------------------------------------------------------------------------int atop_buzzer(int i) { int fd, data; do { usleep(10000); fd = open("/dev/atop_buzzer", O_RDWR | O_SYNC); } while (fd < 0); // Buzzer on data = 1; write(fd, &data, 1); usleep(i); // Buzzer off data = 0; write(fd, &data, 1); close(fd); return 0;} //-------------------------------------------------------------------------int atop_lcm_light(int i) { int fd; unsigned char lcm[2]; do { usleep(10000); fd = open("/dev/atop_lcm", O_RDWR | O_SYNC); } while (fd < 0); // i = 1,light on; i = 0,light off. usleep(50); lcm[0] = 2; lcm[1] = i; write(fd, &lcm, 2); close(fd); return 0;}//-------------------------------------------------------------------------// Set cursor locationint atop_set_cursor(int line, int lc) { int fd, data, i = 0, wait_time; unsigned char lcm[2]; do { usleep(10000); fd = open("/dev/atop_lcm", O_RDWR | O_SYNC); } while (fd < 0); switch(line) { case 1: // set cursor to line 1, location lc. usleep(50); lcm[0] = 0; lcm[1] = 0x80 + lc; write(fd, &lcm, 2); break; case 2: // set cursor to line 2, location lc. usleep(50); lcm[0] = 0; lcm[1] = 0xC0 + lc; write(fd, &lcm, 2); break; default: break; } close(fd); return 0;}//---------------------------------------------------------------------------// LCM print out a lineint lcm_out(char lcm_out_data[], int line, int lc) { int fd, data, i = 0, wait_time; unsigned char lcm[2]; do { usleep(10000); fd = open("/dev/atop_lcm", O_RDWR | O_SYNC); } while (fd < 0); switch(line) { case 1: // set cursor to line 1, location lc. usleep(50); lcm[0] = 0; lcm[1] = 0x80 + lc; write(fd, &lcm, 2); break; case 2: // set cursor to line 2, location lc. usleep(50); lcm[0] = 0; lcm[1] = 0xC0 + lc; write(fd, &lcm, 2); break; default: break; } // print out data for(i = 0; i < 16 && lcm_out_data[i] != 0; i++) { lcm[0] = 1; lcm[1] = lcm_out_data[i]; wait_time = 5000; while(wait_time--); write(fd, &lcm, 2); } // fill space while (i < 16 ) { lcm[0] = 1; lcm[1] = ' '; wait_time = 5000; while(wait_time--); write(fd, &lcm, 2); i++; } close(fd); return 0;}//---------------------------------------------------------------------------// LCM print out a char only on a location.int lcm_out_a_char(int value, int line, int lc) { int fd, data, i = 0, wait_time; unsigned char lcm[2]; do { usleep(10000); fd = open("/dev/atop_lcm", O_RDWR | O_SYNC); } while (fd < 0); switch(line) { case 1: // set cursor to line 1, location lc. usleep(50); lcm[0] = 0; lcm[1] = 0x80 + lc; write(fd, &lcm, 2); break; case 2: // set cursor to line 2, location lc. usleep(50); lcm[0] = 0; lcm[1] = 0xC0 + lc; write(fd, &lcm, 2); break; default: break; } lcm[0] = 1; lcm[1] = value; wait_time = 5000; while(wait_time--); write(fd, &lcm, 2); close(fd); return 0;}//---------------------------------------------------------------------------// print a integerint lcm_out_int(int data, int line, int lc) { char string[18]; memset(string, 0, 18); sprintf(string, "%d", data); lcm_out(string, line, lc); return 0;}//---------------------------------------------------------------------------// Curcor on or off, 1:on, 0:off.int atop_set_cursor_on_off(int i) { int fd, data, wait_time; unsigned char lcm[2]; do { usleep(10000); fd = open("/dev/atop_lcm", O_RDWR | O_SYNC); } while (fd < 0); if (i == 0) data = 0x0C; if (i == 1) data = 0x0E; wait_time = 5000; while(wait_time--); lcm[0] = 0; lcm[1] = data; write(fd, &lcm, 2); close(fd); return 0; }//-------------------------------------------------------------------------// LCM clear display;int lcm_clear(void) { int fd, i = 0; unsigned char lcm[2]; do { usleep(10000); fd = open("/dev/atop_lcm", O_RDWR | O_SYNC); } while (fd < 0); // lcm[0] == command ; lcm[1] == data; atop_set_cursor_on_off(0); atop_lcm_light(0); usleep(2000); lcm[0] = 0; lcm[1] = CLEAR_DISPLAY; write(fd, &lcm, 2); close(fd); return 0;}//---------------------------------------------------------------------------// wait for keypadint lcm_in() { int fd, i = 0, n = 0, lcm_count = 0; char data; do { usleep(10000); fd = open("/dev/atop_keypad", O_RDWR | O_SYNC); } while (fd < 0); while(1) { usleep(100000); if(lcm_count < 500) lcm_count++; else if(lcm_count == 500) { atop_lcm_light(0); lcm_count++; } read(fd, &data, 0); switch (data) { case 0xFFFFFFFE: i = 1; goto exit; break; case 0xFFFFFFF7: i = 2; goto exit; break; case 0xFFFFFFFB: i = 3; goto exit; break; case 0xFFFFFFFD: i = 4; goto exit; break; } }exit: atop_lcm_light(1); atop_buzzer(7000); close(fd); return i;}//---------------------------------------------------------------------------// input 0~9 digitint atop_digit(int i, int location) { int data; int select = 0; while ( select != 1) { switch (select) { case 1: return -1; break; case 2: i = i - 1; if (i < 0) i = 9; break; case 3: i = i + 1; if (i > 9) i = 0; break; case 4: return i; break; } atop_set_cursor(2, location); // set cursor location atop_set_cursor_on_off(0); // set cursor hidden // show a digit, and the cursor will go to next location lcm_out_a_char((i + 0x30), 2, location); atop_set_cursor(2, location); // set cursor return original location atop_set_cursor_on_off(1); // set cursor visible select = lcm_in(); } return -1;}//---------------------------------------------------------------------------int input_address(unsigned char *data) { int i = 0; int num = 0, sum = 0, n = 0; int tmp[4]; int tmp2[4]; memset(tmp, 0, 4); n = data[0]; tmp[0] = (n % 1000) / 100; tmp[1] = (n % 100) / 10; tmp[2] = n % 10; while(i < 3) { num = atop_digit(tmp[i], i); if (num != -1) { sum += num * pow(10, 2 - i); i++; } else return 0; } tmp2[0] = sum; if (sum > 255) return -1; lcm_out_a_char('.', 2, i); i++; memset(tmp, 0, 4); n = data[1]; tmp[0] = (n % 1000) / 100; tmp[1] = (n % 100) / 10; tmp[2] = n % 10; sum = 0; while(i < 7) { num = atop_digit(tmp[i - 4], i); if (num != -1) { sum += num * pow(10, 6 - i); i++; } else return 0; } tmp2[1] = sum; if (sum > 255) return -1; lcm_out_a_char('.', 2, i); i++; memset(tmp, 0, 4); n = data[2]; tmp[0] = (n % 1000) / 100; tmp[1] = (n % 100) / 10; tmp[2] = n % 10; sum = 0; while(i < 11) { num = atop_digit(tmp[i - 8], i); if (num != -1) { sum += num * pow(10, 10 - i); i++; } else return 0; } tmp2[2] = sum; if (sum > 255) return -1; lcm_out_a_char('.', 2, i); i++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -