Browse Source

bidirectionnal communication, first milestone reached

phyto 6 months ago
parent
commit
6d597e59e8
5 changed files with 69 additions and 42 deletions
  1. 11
    6
      Makefile
  2. 49
    26
      essai.c
  3. 2
    4
      simulator/rdtemp/cli.ino
  4. 5
    4
      simulator/rdtemp/rdtemp.ino
  5. 2
    2
      simulator/rdtemp/storage.ino

+ 11
- 6
Makefile View File

@@ -6,16 +6,21 @@ CC	= gcc
6 6
 CCOPT	= -Wall -g 
7 7
 CLIB	= core/libdd2m-core.a viz/libdd2m-viz.a
8 8
 
9
-all:	essai fake-values
9
+all:	essai 
10 10
 
11
-# ---------------------------------------------
11
+# -------------------------------------------------------
12 12
 
13
-O	= serial/serial.o serial/funcs.o
13
+OSERIAL	= serial/serial.o serial/funcs.o
14 14
 
15
-essai:	essai.c  Makefile $(CLIB)
16
-	$(CC) ${CCOPT} $< $(CLIB) ${O} -lncurses -o $@
15
+essai.o:	essai.c  Makefile 
16
+	$(CC) ${CCOPT} $< -c 
17
+
18
+
19
+essai:	essai.o  Makefile $(CLIB)
20
+	$(CC) ${CCOPT} $< $(CLIB) ${OSERIAL} -lncurses -o $@
17 21
 
18 22
 fake-values:	fake-values.c  Makefile $(CLIB)
19 23
 	$(CC) ${CCOPT} $< $(CLIB)  -o $@
20 24
 
21
-# ---------------------------------------------
25
+# -------------------------------------------------------
26
+

+ 49
- 26
essai.c View File

@@ -5,6 +5,7 @@
5 5
 #include	<stdio.h>
6 6
 #include	<unistd.h>
7 7
 #include	<stdlib.h>
8
+#include	<ctype.h>
8 9
 #include	<string.h>
9 10
 #include	<curses.h>
10 11
 #include	<time.h>
@@ -17,7 +18,7 @@
17 18
 int	verbosity;
18 19
 
19 20
 /* --------------------------------------------------------------- */
20
-int affiche_valeurs(int sfd, int nbloops)
21
+int traite_les_messages(int sfd, int nbloops)
21 22
 {
22 23
 int		idx, foo, key;
23 24
 char		ligne[200];
@@ -27,56 +28,75 @@ FILE		*fp;
27 28
 time_t		temps,
28 29
 		old_temps = (time_t)0L;
29 30
 
30
-
31
-/* XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX */
32 31
 fp = fopen("serial/foo.dat", "a");
33 32
 if (NULL==fp) {
34
-	fprintf(stderr, "***** error fopen datafile *****\n");
33
+	fprintf(stderr, "*** error fopen datafile ***\n");
35 34
 	return -1;
36 35
 	}
37
-/* XXX XXX XXX XXX XXX XXX XXX XXX XXX XXX */
38 36
 
39 37
 for (idx=0; idx<nbloops; idx++) {
40 38
 
41 39
 	if (kbhit()) {
42
-		message("!!! KEY !!!");
43
-		sleep(2);
40
+		static char valid_k[] = "+-";
41
+		key = getch();
42
+
43
+		/* if it is a valid key, send it to the Arduino */
44
+		if (NULL!=strchr(valid_k, key)) {
45
+			foo = putbyte(sfd, key);
46
+			fprintf(stderr, "put byte %02X -> %d\n",
47
+						key, foo);
48
+			}
49
+		else	{
50
+			sprintf(ligne, "Key 0x%02X invalid ", key);
51
+			fprintf(stderr, "%s\n", ligne);
52
+			aff_message(ligne); sleep(1); aff_message("");
53
+			}
44 54
 		}
45 55
 
46
-	foo = getline_to(sfd, ligne, 100, 0);
47 56
 
48
-	if (*ligne == 'M') {
49
-		message(ligne);
50
-		}
51
-	
57
+	/* here we are waiting for a frame from the
58
+	   Arduino. In the future, we may have an
59
+	   finely tunned event-synth system here */
60
+	foo = getline_to(sfd, ligne, 100, 0);
52 61
 #if DEBUG_LEVEL
53 62
 	if (foo) fprintf(stderr, "get values -> %d\n", foo);
54 63
 #endif
55 64
 
56
-	foo = parse4_Ivalues(ligne, 'T', Idatas);
65
+	switch (*ligne) {
66
+
67
+	case 'M':
68
+	case 'L':
69
+		aff_message(ligne);
70
+		break;
71
+
72
+	case 'T':	
73
+		foo = parse4_Ivalues(ligne, 'T', Idatas);
57 74
 #if DEBUG_LEVEL
58
-	if (foo) fprintf(stderr, "parse I val -> %d\n", foo); 
75
+		if (foo) fprintf(stderr, "parse I val -> %d\n", foo); 
59 76
 #endif
77
+		values2temperature(Idatas, Fdatas);
60 78
 
61
-	values2temperature(Idatas, Fdatas);
79
+		for (foo=0; foo<3; foo++) {
80
+			sprintf(ligne, "%4d", Idatas[foo]);
81
+			minidigit_affstr(stdscr, 12+(12*foo), 8, ligne);
82
+			aff7segs_float(stdscr, 8+(12*foo), 55, Fdatas[foo]);
83
+			}
62 84
 
63
-	for (foo=0; foo<3; foo++) {
64
-		sprintf(ligne, "%4d", Idatas[foo]);
65
-		minidigit_affstr(stdscr, 12+(12*foo), 8, ligne);
66
-		aff7segs_float(stdscr, 8+(12*foo), 55, Fdatas[foo]);
67
-		}
85
+	/* here we are loging all that temps values */
68 86
 
69
-	/* here we are trying to log all that temps values */
70
-	if (NULL!=fp) {	
71 87
 		temps = time(NULL);
72 88
 		if ((temps-old_temps) > 50) {
73 89
 			fprintf(fp, "%ld %f %f %f %f\n", temps,
74
-				Fdatas[0], Fdatas[1], Fdatas[2], Fdatas[3]);
90
+				Fdatas[0], Fdatas[1],
91
+				Fdatas[2], Fdatas[3]);
75 92
 			fflush(fp);
76 93
 			old_temps = temps;
77 94
 			}
95
+		break;			/* case 'T' */
78 96
 		}
79 97
 
98
+	fflush(stderr);			/* really WTF? here */
99
+
80 100
 	}
81 101
 
82 102
 fclose(fp);
@@ -102,7 +122,7 @@ exit(0);
102 122
 
103 123
 int main(int argc, char *argv[])
104 124
 {
105
-int		opt, foo;
125
+int		opt;
106 126
 int		serial_in;
107 127
 char		*device = "/dev/ttyACM0";
108 128
 
@@ -115,21 +135,24 @@ while ((opt = getopt(argc, argv, "d:hv")) != -1) {
115 135
 		}
116 136
 	}
117 137
 
138
+printf("\n**** %s **** compiled the %s at %s ***\n",
139
+		argv[0], __DATE__, __TIME__);
118 140
 
119 141
 serial_in = prepare_UART(device, 9600);
120 142
 if (serial_in < 0) {
121
-	fprintf(stderr, "%s : open device : error %d on %s\n",
143
+	fprintf(stderr, "\n%s : open device : error %d on %s\n",
122 144
 				argv[0], serial_in, device);
123 145
 	exit(1);
124 146
 	}
125 147
 
148
+sleep(1);
126 149
 
127 150
 initscr();
128 151
 nonl();         cbreak();       noecho();
129 152
 keypad(stdscr, TRUE);           /* acces aux touches 'curseur' */
130 153
 fond_ecran(" Demonstrator ");
131 154
 
132
-affiche_valeurs(serial_in, 50000000);
155
+traite_les_messages(serial_in, 50000000);
133 156
 
134 157
 /*
135 158
  *      plop, on a fini, il faut restaurer la console

+ 2
- 4
simulator/rdtemp/cli.ino View File

@@ -10,8 +10,6 @@
10 10
 #define DEBUG 0
11 11
 /* -------------------------------------------------- */
12 12
 /* some interesting macros */
13
-#define prt(a)      Serial.print(a)
14
-#define prtln(a)    Serial.println(a)
15 13
 
16 14
 /* -------------------------------------------------- */
17 15
 char waitkey(char echo)
@@ -78,7 +76,7 @@ static void clihelp()
78 76
  prtln("r\tread config");
79 77
  prtln("w\twrite config");
80 78
  prtln("T\ttest relay");
81
- prtln("0/1\tcontrole frigo");
79
+ prtln("+/-\tcontrole frigo");
82 80
  prtln("h\thexdump config");
83 81
 }
84 82
 /* --------------------------------------------------------------- */
@@ -148,7 +146,7 @@ do  {
148 146
   prt("$ ");
149 147
   ret = readline(line,TLINE);
150 148
 #if DEBUG > 1
151
-  hexdump((unsigned char *)line, ret);
149
+  prt("readline -> "); hexdump((unsigned char *)line, ret);
152 150
 #endif
153 151
   key = *(sptr = strtok(line, " "));
154 152
 

+ 5
- 4
simulator/rdtemp/rdtemp.ino View File

@@ -10,6 +10,9 @@
10 10
 #define RELAIS_FRIGO    42
11 11
 #define RELAIS_VENTILO  40
12 12
 
13
+#define prt(a)      Serial.print(a)
14
+#define prtln(a)    Serial.println(a)
15
+
13 16
 
14 17
 typedef struct {
15 18
   unsigned short    magic;
@@ -73,9 +76,7 @@ void updatevalues(short *ptr)
73 76
       delay(50);
74 77
       }
75 78
     }
76
-    
77 79
   for (foo=0; foo<NBVAL; foo++) { ptr[foo] /= NB_PASSE; }
78
-  
79 80
   digitalWrite(LED_BUILTIN, LOW);
80 81
 }
81 82
 /* -------------------------------------------------- */
@@ -125,13 +126,13 @@ void loop() {
125 126
           controle_frigo(0);
126 127
           break;
127 128
       default:
128
-          Serial.print("M bad control code");
129
+          prtln("M bad control code");
129 130
           break;          
130 131
       }
131 132
  } 
132 133
  delay(DELAI);
133 134
 
134
- /* ETERNEL LOOP HERE */
135
+ /* ETERNAL LOOP END HERE */
135 136
 }
136 137
 
137 138
 /* -------------------------------------------------- */

+ 2
- 2
simulator/rdtemp/storage.ino View File

@@ -27,7 +27,7 @@ prtln(">>> read config");
27 27
 magic = 0;
28 28
 
29 29
 EEPROM.get(0, magic);
30
-prt("magic is "); prtln(magic);
30
+prt("M magic is "); prtln(magic);
31 31
 if (0xfde9 != magic) return -1;
32 32
 EEPROM.get(0, *where);
33 33
 return -2;
@@ -61,7 +61,7 @@ prtln("");
61 61
 prt("Delay     : ");    prtln(what->delai);
62 62
 prt("Temp mini : ");    prtln(what->temp_mini);
63 63
 prt("Temp maxi : ");    prtln(what->temp_maxi);
64
-
64
+prt("Control   : ");    prtln(what->control);
65 65
 return -1;
66 66
 }
67 67
 /* --------------------------------------------------------------- */

Loading…
Cancel
Save