/sw/in_progress/wind_tunnel/main.c

https://github.com/AshuLara/lisa · C · 128 lines · 90 code · 32 blank · 6 comment · 9 complexity · 4f8b43c863af9d09e52d6f1a8e269602 MD5 · raw file

  1. #include <glib.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <Ivy/ivy.h>
  8. #include <Ivy/ivyglibloop.h>
  9. #include "serial_port.h"
  10. #define BUF_SIZE 512
  11. #define AC_ID 2
  12. static struct SerialPort* sp;
  13. static GIOChannel* ioc;
  14. static void configure_term(struct termios *termios, speed_t *speed) {
  15. /* input modes */
  16. termios->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INPCK|ISTRIP|INLCR|IGNCR
  17. |ICRNL |IUCLC|IXON|IXANY|IXOFF|IMAXBEL);
  18. termios->c_iflag |= IGNPAR;
  19. /* control modes*/
  20. termios->c_cflag &= ~(CSIZE|PARENB|CRTSCTS|PARODD|HUPCL);
  21. termios->c_cflag |= CREAD|CS8|CSTOPB|CLOCAL;
  22. /* local modes */
  23. termios->c_lflag &= ~(ISIG|ICANON|IEXTEN|ECHO|FLUSHO|PENDIN);
  24. termios->c_lflag |= NOFLSH;
  25. /* speed */
  26. *speed = B9600;
  27. }
  28. static void dump_buf ( int len, char* buf) {
  29. static gchar buf2[BUF_SIZE];
  30. static int cur_len = 0;
  31. int i;
  32. for (i=0; i<len; i++) {
  33. if (buf[i] != '\r') {
  34. if (buf[i] != 'T' && buf[i] != '.'){
  35. buf2[cur_len] = buf[i];
  36. cur_len++;
  37. }
  38. }
  39. else {
  40. int force;
  41. buf2[cur_len] = 0;
  42. g_message("buf2: %s",buf2);
  43. sscanf(buf2,"%d",&force);
  44. IvySendMsg("%d X_FORCE %d", AC_ID, force);
  45. cur_len = 0;
  46. }
  47. }
  48. //g_message("read %s and %s",buf,buf2);
  49. }
  50. static gboolean on_serial_data_received(GIOChannel *source,
  51. GIOCondition condition,
  52. gpointer data) {
  53. gchar* _buf;
  54. GError* _err = NULL;
  55. GIOStatus st = g_io_channel_read_line(source, &_buf, NULL, NULL, &_err);
  56. switch (st) {
  57. case G_IO_STATUS_AGAIN :
  58. g_message("again");
  59. break;
  60. case G_IO_STATUS_NORMAL:
  61. g_message("normal [%s]", _buf);
  62. // faire un truc
  63. g_free(_buf);
  64. break;
  65. default:
  66. g_message("something else");
  67. }
  68. #if 0
  69. static gchar buf[BUF_SIZE];
  70. gsize len;
  71. g_io_channel_read_chars(source, buf, BUF_SIZE, &len, NULL);
  72. dump_buf(len, buf);
  73. #endif
  74. return TRUE;
  75. }
  76. gboolean timeout_callback(gpointer data) {
  77. const char* msg = "GT\n";
  78. gsize bw;
  79. g_io_channel_write_chars(ioc, msg, 3, &bw, NULL);
  80. g_io_channel_flush(ioc, NULL);
  81. return TRUE;
  82. }
  83. int main ( int argc, char** argv) {
  84. sp = serial_port_new();
  85. serial_port_open(sp, "/dev/ttyUSB1", configure_term);
  86. ioc = g_io_channel_unix_new(sp->fd);
  87. g_io_channel_set_encoding(ioc, NULL, NULL);
  88. g_io_channel_set_flags (ioc,G_IO_FLAG_NONBLOCK, NULL );
  89. g_io_add_watch (ioc, G_IO_IN, on_serial_data_received, NULL);
  90. GMainLoop *ml = g_main_loop_new(NULL, FALSE);
  91. IvyInit ("Wind_Tunnel", "Wind_Tunnel READY", NULL, NULL, NULL, NULL);
  92. IvyStart("127.255.255.255");
  93. g_timeout_add(500, timeout_callback, NULL);
  94. g_main_loop_run(ml);
  95. return 0;
  96. }