| 1 | /* |
| 2 | * Copyright © 2014 Keith Packard <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along |
| 15 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 17 | */ |
| 18 | |
| 19 | #include <err.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <gelf.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdint.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <sysexits.h> |
| 26 | #include <unistd.h> |
| 27 | #include <getopt.h> |
| 28 | #include <string.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <ctype.h> |
| 31 | #include "ao-elf.h"cxx . . $ `<file>/ao-tools/lib/ao-elf.h`/ |
| 32 | #include "ccdbg.h"cxx . . $ `<file>/ao-tools/lib/ccdbg.h`/ |
| 33 | #include "cc-usb.h"cxx . . $ `<file>/ao-tools/lib/cc-usb.h`/ |
| 34 | #include "cc.h"cxx . . $ `<file>/ao-tools/lib/cc.h`/ |
| 35 | #include "ao-verbose.h"cxx . . $ `<file>/ao-tools/lib/ao-verbose.h`/ |
| 36 | |
| 37 | static const struct optioncxx . . $ option# optionscxx . . $ options.[] = { |
| 38 | { .name = "tty", .has_arg = 1, .val = 'T' }, |
| 39 | { .name = "device", .has_arg = 1, .val = 'D' }, |
| 40 | { .name = "raw", .has_arg = 0, .val = 'r' }, |
| 41 | { .name = "verbose", .has_arg = 1, .val = 'v' }, |
| 42 | { .name = "rplus", .has_arg = 1, .val = 'a' }, |
| 43 | { .name = "rminus", .has_arg = 1, .val = 'b' }, |
| 44 | { .name = "adcmax", .has_arg = 1, .val = 'm' }, |
| 45 | { 0, 0, 0, 0}, |
| 46 | }; |
| 47 | |
| 48 | static void usagecxx . . $ usage(fda57c8427c60377).(char *program) |
| 49 | { |
| 50 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "usage: %s [--verbose=<verbose>] [--device=<device>] [--tty=<tty>] [--rplus=val] [--rminus=val] [--adcmax=val] main|drogue\n", program); |
| 51 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | donecxx . . $ done(f28ec16f34030185).(struct cc_usbcxx . . $ cc_usb# *cc, int code) |
| 56 | { |
| 57 | /* cc_usb_printf(cc, "a\n"); */ |
| 58 | cc_usb_closecxx . . $ cc_usb_close(e07fbe74ad391859).(cc); |
| 59 | exitcxx . . $ exit(3ca292c338df9bb3). (code); |
| 60 | } |
| 61 | |
| 62 | struct ignitercxx . . $ igniter# { |
| 63 | struct ignitercxx . . $ igniter# *nextcxx . . $ igniter#next.; |
| 64 | char namecxx . . $ igniter#name.[512]; |
| 65 | char statuscxx . . $ igniter#status.[512]; |
| 66 | int adccxx . . $ igniter#adc.; |
| 67 | }; |
| 68 | |
| 69 | static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! |
| 70 | map_igniter_namecxx . . $ map_igniter_name(14223ed414283e68).(char *adc_name, char *igniter_name) |
| 71 | { |
| 72 | char *colon = strchrcxx . . $ strchr(2050848944e00d54).(adc_name, ':'); |
| 73 | if (!colon) |
| 74 | return falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 75 | *colon = '\0'; |
| 76 | if (strlencxx . . $ strlen(751346f8406fd082).(adc_name) == 1 && isuppercxx . . $ `<external>/17904084882347303253/ctype.h:198:10`!(adc_name[0])) { |
| 77 | igniter_name[0] = '0' + adc_name[0] - 'A'; |
| 78 | igniter_name[1] = '\0'; |
| 79 | } else { |
| 80 | strcpycxx . . $ strcpy(b7b3a83273496486).(igniter_name, adc_name); |
| 81 | } |
| 82 | return truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!; |
| 83 | } |
| 84 | |
| 85 | static const char * |
| 86 | other_igniter_namecxx . . $ other_igniter_name(fb58d2e09a2a1ce6).(const char *name) |
| 87 | { |
| 88 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, "drogue")) |
| 89 | return "apogee"; |
| 90 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(name, "apogee")) |
| 91 | return "drogue"; |
| 92 | return name; |
| 93 | } |
| 94 | |
| 95 | static struct ignitercxx . . $ igniter# * |
| 96 | igniterscxx . . $ igniters(6ac6b994ddf460a0).(struct cc_usbcxx . . $ cc_usb# *usb) |
| 97 | { |
| 98 | struct ignitercxx . . $ igniter# *head = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, **tail = &head; |
| 99 | cc_usb_printfcxx . . $ cc_usb_printf(4471868d2bd8f0b7).(usb, "t\na\nv\n"); |
| 100 | for (;;) { |
| 101 | char line[512]; |
| 102 | char name[512]; |
| 103 | char status[512]; |
| 104 | char adc_name[512]; |
| 105 | char igniter_name[512]; |
| 106 | |
| 107 | cc_usb_getlinecxx . . $ cc_usb_getline(2f8f04874cae7eae).(usb, line, sizeof (line)); |
| 108 | if (strstrcxx . . $ strstr(a713fe7b9a57cf96).(line, "software-version")) |
| 109 | break; |
| 110 | if (sscanfcxx . . $ sscanf(5e8a2c12db2ea735).(line, "Igniter: %s Status: %s", name, status) == 2) { |
| 111 | struct ignitercxx . . $ igniter# *i = calloccxx . . $ calloc(63a7bf2941bfcd7a). (1, sizeof (struct ignitercxx . . $ igniter#)); |
| 112 | strcpycxx . . $ strcpy(b7b3a83273496486).(i->namecxx . . $ igniter#name., name); |
| 113 | strcpycxx . . $ strcpy(b7b3a83273496486).(i->statuscxx . . $ igniter#status., status); |
| 114 | i->nextcxx . . $ igniter#next. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 115 | *tail = i; |
| 116 | tail = &i->nextcxx . . $ igniter#next.; |
| 117 | } |
| 118 | if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(line, "tick:", 5) == 0) { |
| 119 | char *tok; |
| 120 | char *l = line; |
| 121 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! found_igniter = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 122 | |
| 123 | while ((tok = strtokcxx . . $ strtok(b7b3a83273496486).(l, " ")) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 124 | l = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 125 | if (found_igniter) { |
| 126 | struct ignitercxx . . $ igniter# *i; |
| 127 | for (i = head; i; i = i->nextcxx . . $ igniter#next.) |
| 128 | if (!strcmpcxx . . $ strcmp(5ae29d726ae57f88).(i->namecxx . . $ igniter#name., igniter_name) || |
| 129 | !strcmpcxx . . $ strcmp(5ae29d726ae57f88).(i->namecxx . . $ igniter#name., other_igniter_namecxx . . $ other_igniter_name(fb58d2e09a2a1ce6).(igniter_name))) |
| 130 | { |
| 131 | i->adccxx . . $ igniter#adc. = atoicxx . . $ atoi(5c4306d8553b3696).(tok); |
| 132 | break; |
| 133 | } |
| 134 | found_igniter = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!; |
| 135 | } else { |
| 136 | strcpycxx . . $ strcpy(b7b3a83273496486).(adc_name, tok); |
| 137 | found_igniter = map_igniter_namecxx . . $ map_igniter_name(14223ed414283e68).(adc_name, igniter_name); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | return head; |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | free_igniterscxx . . $ free_igniters(73e5a9d6cce78ba0).(struct ignitercxx . . $ igniter# *i) { |
| 147 | struct ignitercxx . . $ igniter# *n; |
| 148 | |
| 149 | while (i) { |
| 150 | n = i->nextcxx . . $ igniter#next.; |
| 151 | freecxx . . $ free(5d22bdacc48458e8).(i); |
| 152 | i = n; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static struct ignitercxx . . $ igniter# * |
| 157 | find_ignitercxx . . $ find_igniter(9483441e5e684a20).(struct ignitercxx . . $ igniter# *i, char *name) |
| 158 | { |
| 159 | for (; i; i = i->nextcxx . . $ igniter#next.) |
| 160 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(i->namecxx . . $ igniter#name., name) == 0) { |
| 161 | printfcxx . . $ printf(581fddd72a8db8fc).("igniter %s adc %d\n", i->namecxx . . $ igniter#name., i->adccxx . . $ igniter#adc.); |
| 162 | return i; |
| 163 | } |
| 164 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 165 | } |
| 166 | |
| 167 | static const double ref_voltscxx . . $ ref_volts. = 3.3; |
| 168 | |
| 169 | static double |
| 170 | compute_voltagecxx . . $ compute_voltage(14c62aea2fd37e07).(int adc, double rplus, double rminus, int adc_max) |
| 171 | { |
| 172 | return (double) adc / (double) adc_max * ref_voltscxx . . $ ref_volts. * (rplus + rminus) / rminus; |
| 173 | } |
| 174 | |
| 175 | static int |
| 176 | do_ignitercxx . . $ do_igniter(9b7d3cb514da1dcd).(struct cc_usbcxx . . $ cc_usb# *usb, char *name, double rplus, double rminus, int adc_max) |
| 177 | { |
| 178 | struct ignitercxx . . $ igniter# *all = igniterscxx . . $ igniters(6ac6b994ddf460a0).(usb); |
| 179 | struct ignitercxx . . $ igniter# *this = find_ignitercxx . . $ find_igniter(9483441e5e684a20).(all, name); |
| 180 | double volts = -1; |
| 181 | if (!this) { |
| 182 | struct ignitercxx . . $ igniter# *i; |
| 183 | printfcxx . . $ printf(581fddd72a8db8fc).("no igniter %s found in", name); |
| 184 | for (i = all; i; i = i->nextcxx . . $ igniter#next.) |
| 185 | printfcxx . . $ printf(581fddd72a8db8fc).(" %s", i->namecxx . . $ igniter#name.); |
| 186 | printfcxx . . $ printf(581fddd72a8db8fc).("\n"); |
| 187 | free_igniterscxx . . $ free_igniters(73e5a9d6cce78ba0).(all); |
| 188 | return 0; |
| 189 | } |
| 190 | if (rplus && rminus && adc_max) { |
| 191 | volts = compute_voltagecxx . . $ compute_voltage(14c62aea2fd37e07).(this->adccxx . . $ igniter#adc., rplus, rminus, adc_max); |
| 192 | if (volts < 1 || volts > 4) { |
| 193 | printfcxx . . $ printf(581fddd72a8db8fc).("igniter %s voltage is %f, not in range of 1-4 volts\n", this->namecxx . . $ igniter#name., volts); |
| 194 | free_igniterscxx . . $ free_igniters(73e5a9d6cce78ba0).(all); |
| 195 | return 0; |
| 196 | } |
| 197 | } |
| 198 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(this->statuscxx . . $ igniter#status., "open") == 0) { |
| 199 | printfcxx . . $ printf(581fddd72a8db8fc).("igniter %s status is %s\n", this->namecxx . . $ igniter#name., this->statuscxx . . $ igniter#status.); |
| 200 | free_igniterscxx . . $ free_igniters(73e5a9d6cce78ba0).(all); |
| 201 | return 0; |
| 202 | } |
| 203 | cc_usb_printfcxx . . $ cc_usb_printf(4471868d2bd8f0b7).(usb, "i DoIt %s\n", this->namecxx . . $ igniter#name.); |
| 204 | cc_usb_synccxx . . $ cc_usb_sync(e07fbe74ad391859).(usb); |
| 205 | free_igniterscxx . . $ free_igniters(73e5a9d6cce78ba0).(all); |
| 206 | usleepcxx . . $ usleep(4c679297bf94b9eb).(200000); |
| 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | int |
| 211 | maincxx . . $ main(afcd3168fc1188a4). (int argc, char **argv) |
| 212 | { |
| 213 | char *device = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 214 | int i; |
| 215 | int c; |
| 216 | struct cc_usbcxx . . $ cc_usb# *cc = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 217 | char *tty = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 218 | int verbose = 0; |
| 219 | int ret = 0; |
| 220 | double rplus = 0.0; |
| 221 | double rminus = 0.0; |
| 222 | int adcmax = 0; |
| 223 | |
| 224 | while ((c = getopt_longcxx . . $ getopt_long(41f665c090816971).(argc, argv, "rT:D:c:s:v:a:b:m:", optionscxx . . $ options., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)) != -1) { |
| 225 | switch (c) { |
| 226 | case 'T': |
| 227 | tty = optargcxx . . $ optarg.; |
| 228 | break; |
| 229 | case 'D': |
| 230 | device = optargcxx . . $ optarg.; |
| 231 | break; |
| 232 | case 'v': |
| 233 | verbose++; |
| 234 | break; |
| 235 | case 'a': |
| 236 | rplus = strtodcxx . . $ strtod(3dfe5620a0d3160e).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 237 | break; |
| 238 | case 'b': |
| 239 | rminus = strtodcxx . . $ strtod(3dfe5620a0d3160e).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 240 | break; |
| 241 | case 'm': |
| 242 | adcmax = strtolcxx . . $ strtol(44bbb87609b212fc).(optargcxx . . $ optarg., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0); |
| 243 | break; |
| 244 | default: |
| 245 | usagecxx . . $ usage(fda57c8427c60377).(argv[0]); |
| 246 | break; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | ao_verbosecxx . . $ ao_verbose. = verbose; |
| 251 | |
| 252 | if (verbose > 1) |
| 253 | ccdbg_add_debugcxx . . $ ccdbg_add_debug(d4f767463ce0a6b3).(CC_DEBUG_BITBANGcxx . . $ `ao-tools/lib/ccdbg-debug.h:23:9`!); |
| 254 | |
| 255 | if (!tty) |
| 256 | tty = cc_usbdevs_find_by_argcxx . . $ cc_usbdevs_find_by_arg(5bd1a890f71b1efd).(device, "TeleMega-v1.0"); |
| 257 | if (!tty) |
| 258 | tty = cc_usbdevs_find_by_argcxx . . $ cc_usbdevs_find_by_arg(5bd1a890f71b1efd).(device, "TeleMetrum-v2.0"); |
| 259 | if (!tty) |
| 260 | tty = cc_usbdevs_find_by_argcxx . . $ cc_usbdevs_find_by_arg(5bd1a890f71b1efd).(device, "TeleMini-v2.0"); |
| 261 | if (!tty) |
| 262 | tty = cc_usbdevs_find_by_argcxx . . $ cc_usbdevs_find_by_arg(5bd1a890f71b1efd).(device, "EasyMega-v1.0"); |
| 263 | if (!tty) |
| 264 | tty = cc_usbdevs_find_by_argcxx . . $ cc_usbdevs_find_by_arg(5bd1a890f71b1efd).(device, "EasyMetrum-v1.0"); |
| 265 | if (!tty) |
| 266 | tty = cc_usbdevs_find_by_argcxx . . $ cc_usbdevs_find_by_arg(5bd1a890f71b1efd).(device, "EasyMini-v1.0"); |
| 267 | if (!tty) |
| 268 | tty = getenvcxx . . $ getenv(a8b8e32347182212).("ALTOS_TTY"); |
| 269 | if (!tty) |
| 270 | tty="/dev/ttyACM0"; |
| 271 | |
| 272 | cc = cc_usb_opencxx . . $ cc_usb_open(911c141cb856af5d).(tty); |
| 273 | |
| 274 | if (!cc) |
| 275 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 276 | |
| 277 | for (i = optindcxx . . $ optind.; i < argc; i++) { |
| 278 | char *name = argv[i]; |
| 279 | |
| 280 | if (!do_ignitercxx . . $ do_igniter(9b7d3cb514da1dcd).(cc, name, rplus, rminus, adcmax)) |
| 281 | ret++; |
| 282 | } |
| 283 | donecxx . . $ done(f28ec16f34030185).(cc, ret); |
| 284 | } |