#include #include #include #include void usage(char *progname) { fprintf(stderr, "Usage: %s [options] command [args]\n", progname); fprintf(stderr, "Commands:\n"); fprintf(stderr, " reset Perform 1-wire reset and report status\n"); fprintf(stderr, " scan Scan devices on 1-wire bus and report addresses\n"); fprintf(stderr, " ds18s20 addr Read ds18s20 temperature\n"); fprintf(stderr, "Options:\n"); fprintf(stderr, " -apu Use active pull-up\n"); fprintf(stderr, " -ppm Use presence pulse masking\n"); } void readaddr(struct owaddr *addr, char *s) { int count; count = sscanf(s, "%02x.%02x%02x%02x%02x%02x%02x", &addr->family, &addr->id[0], &addr->id[1], &addr->id[2], &addr->id[3], &addr->id[4], &addr->id[5]); addr->crc = ow_crc((unsigned char *) addr, 7); if (count < 7) fprintf(stderr, "Invalid address specification: %s\n", s); } void writeaddr(const struct owaddr *addr, char *buf, int bufsize) { snprintf(buf, bufsize, "%02x.%02x%02x%02x%02x%02x%02x", addr->family, addr->id[0], addr->id[1], addr->id[2], addr->id[3], addr->id[4], addr->id[5]); } int cmd_reset() { unsigned char status; status = ow_reset(); if (status & STATUS_SD) printf("Shortcircuit detected\n"); else if (status & STATUS_PPD) printf("Presence pulse detected\n"); else printf("No presence pulse detected\n"); return (status & STATUS_PPD) ? 0 : 1; } void scan_resultfunc(struct owaddr *addr) { char buf[32]; writeaddr(addr, buf, 32); printf("Found device: %s, CRC: %02x\n", buf, addr->crc); } int cmd_scan() { int count; count = ow_bussearch(&scan_resultfunc); if (count >= 0) { printf("Total %d devices found\n", count); } else if (count == -1) { printf("No presence pulse during search\n"); } else if (count == -2) { printf("Triplet both 1\n"); } else if (count == -3) { printf("Addr CRC error\n"); } return (count > 0) ? 0 : 1; } int cmd_ds18s20(const struct owaddr *addr) { char buf[32]; writeaddr(addr, buf, 32); printf("Device %s:\n", buf); int power; ow_matchrom(addr); ow_writecommand(DS18S20_POWER); power = ow_readbit(); if (power) printf("- externally powered\n"); else printf("- parasite powered\n"); float t; t = ds18s20_temperature(addr); printf("- temperature %0.2f celsius\n", t); return (t != 85.) ? 0 : 1; } int main(int argc, char *argv[]) { int apu = 0, ppm = 0; int i; struct owaddr address; if (argc < 2) { usage(argv[0]); return -1; } /* Process options */ for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { if (strcmp(argv[i], "-apu") == 0) { apu = 1; } else if (strcmp(argv[i], "-ppm") == 0) { ppm = 1; } else { fprintf(stderr, "Invalid option %s\n", argv[i]); usage(argv[0]); return -1; } } else { break; } } ow_initialize(); ds2482_config((apu ? CONFIG_APU : 0) | (ppm ? CONFIG_PPM : 0)); /* Process commands */ // Command is at pos i if (strcmp(argv[i], "reset") == 0) { return cmd_reset(); } else if (strcmp(argv[i], "scan") == 0) { return cmd_scan(); } else if (strcmp(argv[i], "ds18s20") == 0) { if (argc < i + 2) { fprintf(stderr, "Missing argument\n"); usage(argv[0]); return -1; } readaddr(&address, argv[i + 1]); return cmd_ds18s20(&address); } else { fprintf(stderr, "Invalid command %s\n", argv[i]); usage(argv[0]); return -1; } }