comparison loadtools/buzplay.c @ 86:684eddecbc62

fc-buzplay play command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 27 Oct 2016 06:51:42 +0000
parents
children
comparison
equal deleted inserted replaced
85:c5766d12360d 86:684eddecbc62
1 /*
2 * The actual functionality of fc-buzplay is implemented here.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <stdlib.h>
11
12 cmd_play(argc, argv)
13 char **argv;
14 {
15 FILE *f;
16 char linebuf[256], *cp, *num1, *num2;
17 int lineno;
18 char *targv[4];
19 u_long n1, n2, total_ms;
20 int rc, timeout;
21
22 f = fopen(argv[1], "r");
23 if (!f) {
24 perror(argv[1]);
25 return(-1);
26 }
27 printf("Uploading the melody to the target\n");
28 targv[0] = "I";
29 targv[1] = 0;
30 tpinterf_make_cmd(targv);
31 if (tpinterf_send_cmd() < 0) {
32 fclose(f);
33 return(-1);
34 }
35 rc = tpinterf_pass_output(1);
36 if (rc) {
37 fclose(f);
38 return(rc);
39 }
40 targv[0] = "E";
41 targv[3] = 0;
42 total_ms = 0;
43 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
44 cp = index(linebuf, '\n');
45 if (!cp) {
46 fprintf(stderr, "%s line %d: missing newline\n",
47 argv[1], lineno);
48 fclose(f);
49 return(-1);
50 }
51 for (cp = linebuf; isspace(*cp); cp++)
52 ;
53 if (*cp == '\0' || *cp == '#')
54 continue;
55 if (!isdigit(*cp)) {
56 inv: fprintf(stderr, "%s line %d: unexpected content\n",
57 argv[1], lineno);
58 fclose(f);
59 return(-1);
60 }
61 for (num1 = cp; *cp && !isspace(*cp); cp++)
62 if (!isdigit(*cp))
63 goto inv;
64 if (isspace(*cp))
65 *cp++ = '\0';
66 while (isspace(*cp))
67 cp++;
68 if (!isdigit(*cp))
69 goto inv;
70 for (num2 = cp; *cp && !isspace(*cp); cp++)
71 if (!isdigit(*cp))
72 goto inv;
73 if (isspace(*cp))
74 *cp++ = '\0';
75 while (isspace(*cp))
76 cp++;
77 if (*cp != '\0' && *cp != '#')
78 goto inv;
79 n1 = strtoul(num1, 0, 10);
80 n2 = strtoul(num2, 0, 10);
81 if (n1 > 255) {
82 fprintf(stderr,
83 "%s line %d: the tone number is out of range\n",
84 argv[1], lineno);
85 fclose(f);
86 return(-1);
87 }
88 if (n2 < 1 || n2 > 0xFFFF) {
89 fprintf(stderr,
90 "%s line %d: the duration number is out of range\n",
91 argv[1], lineno);
92 fclose(f);
93 return(-1);
94 }
95 /* send it to the target */
96 targv[1] = num1;
97 targv[2] = num2;
98 tpinterf_make_cmd(targv);
99 if (tpinterf_send_cmd() < 0) {
100 fclose(f);
101 return(-1);
102 }
103 rc = tpinterf_pass_output(1);
104 if (rc) {
105 fclose(f);
106 return(rc);
107 }
108 /* account for the duration */
109 total_ms += n2 * 5;
110 }
111 fclose(f);
112 if (!total_ms) {
113 fprintf(stderr, "%s is empty!\n", argv[1]);
114 return(-1);
115 }
116 printf("Requesting play of the uploaded melody on the target\n");
117 targv[0] = "P";
118 targv[1] = 0;
119 tpinterf_make_cmd(targv);
120 if (tpinterf_send_cmd() < 0)
121 return(-1);
122 timeout = total_ms / 1000 + 2;
123 return tpinterf_pass_output(timeout);
124 }