#include #include #include #include #include #include #include "howard.h" #define MAX_STR 255 // Benestar sound meter //#define MIC_VID (0x1630) //#define MIC_PID (0x05dc) #define MIC_VID (0x8817) #define MIC_PID (0x2109) #define CMD_CAPTURE (0xb3) const char *rangestr[] = { "30-130", "30-80", "50-100", "60-110", "80-130", }; #define FLAG_DBCMODE (0x10) #define FLAG_FASTMODE (0x40) #define E_TIMEOUT -1 #define E_BADREAD -2 // ANSI stuff #define BOLD "\x1b[1m" #define ITALIC "\x1b[3m" #define STRIKE "\x1b[9m" #define PLAIN "\x1b[0m" #define UNDERLINE "\x1b[4m" #define RED "\x1b[31m" #define MAGENTA "\x1b[35m" #define GREEN "\x1b[32m" #define YELLOW "\x1b[33m" #define BLUE "\x1b[34m" #define CYAN "\x1b[36m" #define GREY "\x1b[2;37m" void colprintf( char *prefix, const char *col, char *format, va_list *args ) { printf("%s%s%s %s%s",col,BOLD,prefix,PLAIN,col); vprintf(format, *args); printf("%s\n",PLAIN); } void info( char* format, ... ) { va_list args; va_start(args, format); colprintf( "*", CYAN, format, &args); va_end( args ); } void err( char* format, ... ) { va_list args; va_start(args, format); colprintf( "ERROR:", RED, format, &args); va_end( args ); } void warn( char* format, ... ) { va_list args; va_start(args, format); colprintf( "WARNING:", YELLOW, format, &args); va_end( args ); } int hextoint(char *hex) { int base=16; if (strstr(hex, "0x") == hex) { base=0; } return strtol(hex, NULL, base); } int readresult(hid_device *dev, uint8_t *retbuf) { int totbytes = 0; int wantbytes = 8; time_t starttime; int timeoutms = 2000; starttime = time(NULL); while (totbytes != wantbytes) { int thisbytes = 0; thisbytes = hid_read_timeout(dev, &retbuf[totbytes], wantbytes + 1 - totbytes, timeoutms); if (thisbytes < 0) { err("Incomplete read from usb (got %d bytes, want %d bytes): %ls",totbytes,wantbytes, hid_error(dev)); return E_BADREAD; } totbytes += thisbytes; if (time(NULL) - starttime > (timeoutms*1000)) { warn("Timeout reading from usb"); return E_TIMEOUT; } } return 0; } void showlevel(uint8_t *buf) { uint16_t decibels; uint8_t flags, rangeidx; time_t now; char *longformat = "%12s: %ld\n"; char *strformat = "%12s: %s\n"; char *dbformat = "%12s: %4.2f %s\n"; decibels = buf[0] << 8 | buf[1]; flags = buf[2]; rangeidx = buf[2] & 0xf; now = time(NULL); printf(longformat, "Time", now); printf(dbformat, "Level", (double)decibels/10.0, flags & FLAG_DBCMODE ? "dBC" : "dBA"); printf(strformat, "Mode", flags & FLAG_FASTMODE ? "Fast" : "Slow"); printf(strformat, "Range", rangeidx > 0x4 ? "unknown" : rangestr[rangeidx]); } int main(int argc, char *argv[]) { //unsigned char buf[65]; //wchar_t wstr[MAX_STR]; //hid_device *handle; uint8_t capture_cmd[8] = { CMD_CAPTURE }; uint8_t buf[8]; int res; int i; enum mode_enum { M_PROBE, M_TEST } mode = M_PROBE; hid_device *dev = NULL; // handle args for (i=1; i