//---------------------------------------------------------------------- // Enter a decimal string and convert it to an unsigned long // Prompt again if not within min/max inclusive. // int EnterNum(char *msg, int numchars, long *value, long min, long max) { short tmp,cnt,isneg=FALSE; char ch; // check for abort if (key_abort()) return FALSE; // loop while not in correct range do { printf("%s (%ld): ",msg,*value); // loop for each character read cnt = 0; for (;;) { ch = (char)getkeystroke(); // check for abort if (key_abort()) return FALSE; // negative flag if (ch == '-') { if (!isneg) { isneg = TRUE; printf("-"); cnt++; } } // backspace if (ch == 0x08) { if (cnt) { if (isneg && (cnt == 1)) isneg = FALSE; else *value /= 10; printf("%c %c",ch,ch); cnt--; } } // escape if (ch == 0x1B) { printf(" Aborted\n\n"); return FALSE; } // enter if (ch == 0x0A) { printf("\n"); break; } // number else if ((ch >= '0') && (ch <= '9')) { if (cnt == 0) *value = 0; if (cnt < numchars) { printf("%c",ch); tmp = ch - 0x30; *value *= 10; *value += tmp; cnt++; } } } if (isneg) *value = -*value; } while ((*value < min) || (*value > max)); return TRUE; }
//---------------------------------------------------------------------- // Enter a string // Leave the same if no characters entered // int EnterString(char *msg, char *buf, int min, int max) { int len,deflen; char ch,defbuf[80]; // check for abort if (key_abort()) return -1; // remember the start length deflen = strlen(buf); if (deflen < 80) sprintf(defbuf,"%s",buf); /* rma: fixed format warning */ else defbuf[0] = 0; // prompt if (deflen < 30) printf("%s (%s): ",msg,buf); else printf("%s (%s):\n",msg,buf); // loop to get the file len = 0; for (;;) { // get a key ch = (char)getkeystroke(); // check for abort if (key_abort()) return -1; // check the backspace key if (ch == 0x08) { if (len > 0) { // got a valid backspace len--; printf("\x008 \x008"); } } // escape key else if (ch == 0x1B) { printf("\n"); return -1; } // caraige return if (ch == 0x0A) { // check for special case (keep default) if ((len == 0) && (deflen > min)) { sprintf(buf,"%s",defbuf); printf("\n"); return deflen; } else if (len < min) continue; else { printf("\n"); return len; } } // valid key else if (len < max) { printf("%c",ch); // record the char buf[len++] = ch; } } }
//---------------------------------------------------------------------- // This is the Main routine for debit // int main(short argc, char **argv) { char msg[200]; int portnum = 0; float Vdd,Vad; double humid,temp; int i; int numbat,cnt=0; uchar famvolt[MAXDEVICES][8]; // check for required port name if (argc != 2) { sprintf(msg,"1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n"); printf("%s",msg); return 0; } if((portnum = owAcquireEx(argv[1])) < 0) { printf("Failed to acquire port.\n"); return 0; } else { do { numbat = FindDevices(portnum,&famvolt[0],SBATTERY_FAM,MAXDEVICES); if(numbat == 0) { if(cnt > 1000) { cnt = 0; printf("No humidity buttons found.\n"); } else { cnt++; } } else { for(i=0;i<numbat;i++) { Vdd = ReadAtoD(portnum,TRUE,&famvolt[0][0]); if(Vdd > 5.8) { Vdd = (float)5.8; } else if(Vdd < 4.0) { Vdd = (float) 4.0; } Vad = ReadAtoD(portnum,FALSE,&famvolt[0][0]); temp = Get_Temperature(portnum,&famvolt[0][0]); humid = (((Vad/Vdd) - 0.16)/0.0062)/(1.0546 - 0.00216 * temp); if(humid > 100) { humid = 100; } else if(humid < 0) { humid = 0; } printf("\n"); printf("The humidity is: %4.4f\n", humid); printf("Given that the temp was: %2.2f\n", temp); printf("and the volt supply was: %2.2f\n", Vdd); printf("with the volt output was: %2.2f\n", Vad); printf("\n"); }//for loop } }while(!key_abort()); owRelease(portnum); printf("Port released.\n"); } return 1; }
//---------------------------------------------------------------------- // Main Test for the DS2450 - 1-Wire Quad A/D Converter // int main(int argc, char **argv) { char msg[45]; int NumDevices = 0; int i = 0; int start_address = 0x8; int end_address = 0x11; float prslt[4]; uchar ctrl[16]; int try_overdrive=0; int portnum=0; //------------------------------------------------------ // Introduction header printf("\n/---------------------------------------------\n"); printf(" Channels A to D Application - V2.00\n" " The following is a test to excersize a\n" " DS2450 - 1-Wire Quad A/D Converter \n\n"); printf(" Press any CTRL-C to stop this program.\n\n"); printf(" Output [Serial Number(s) ... Channel 'A' Value ... Channel 'B' Value ... \n" " ... Channel 'C' Value ... Channel 'D' Value] \n\n"); // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if ((portnum = owAcquireEx(argv[1])) < 0) { OWERROR_DUMP(stdout); exit(1); } // success printf("Port opened: %s\n",argv[1]); // Find the device(s) NumDevices = FindDevices(portnum, &FamilySN[0], 0x20, MAXDEVICES); if (NumDevices>0) { printf("\n"); printf("Device(s) Found: \n"); for (i = 0; i < NumDevices; i++) { PrintSerialNum(FamilySN[i]); printf("\n"); if (SetupAtoDControl(portnum, FamilySN[i], &ctrl[0], &msg[0])) { printf("A/D settings found\n %s\n", msg); } else printf("\n\n\n ERROR, device set up unsuccessful!\n"); if (WriteAtoD(portnum, try_overdrive, FamilySN[i], &ctrl[0], start_address, end_address)) { printf("\nA/D settings written"); } else printf("\n\n\n ERROR, device not found!\n"); } } // (stops on CTRL-C) do { // read the current channels for (i = 0; i < NumDevices; i++) { printf("\n\n"); PrintSerialNum(FamilySN[i]); if (!DoAtoDConversion(portnum, try_overdrive, FamilySN[i])) { printf("\nError doing conversion, verify device present: %d\n", (int)owVerify(portnum,FALSE)); } if (ReadAtoDResults(portnum, try_overdrive, FamilySN[i], &prslt[0], &ctrl[0])) { int c = 0; for (c = 0; c < 4; c++) { printf(" %1.3f ", prslt[c]); } } else { printf("\nError reading channel, verify device present: %d\n", (int)owVerify(portnum,FALSE)); } } } while (!key_abort()); // release the 1-Wire Net owRelease(portnum); printf("Closing port %s.\n", argv[1]); exit(0); return 0; }
//---------------------------------------------------------------------- // Main Test for DS1920/DS1820 temperature measurement // int main(int argc, char **argv) { float current_temp; int i = 0; int NumDevices=0; int portnum = 0; //---------------------------------------- // Introduction header printf("\n/---------------------------------------------\n"); // printf(" Temperature application DS1920/DS1820 - Version 1.00 \n" // " The following is a test to excersize a DS1920/DS1820.\n" // " Temperature Find and Read from a: \n" // " DS1920/DS1820 (at least 1)\n\n"); printf(" Press any CTRL-C to stop this program.\n\n"); printf(" Output [Serial Number(s) ........ Temp1(F)] \n\n"); // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if((portnum = owAcquireEx(argv[1])) < 0) { OWERROR_DUMP(stdout); exit(1); } // success printf("Port opened: %s\n",argv[1]); // Find the device(s) NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES); if (NumDevices>0) { printf("\n"); printf("Device(s) Found: \n"); for (i = 0; i < NumDevices; i++) { PrintSerialNum(FamilySN[i]); printf("\n"); } printf("\n\n"); // (stops on CTRL-C) do { // read the temperature and print serial number and temperature for (i = 0; i < NumDevices; i++) { if (ReadTemperature(portnum, FamilySN[i],¤t_temp)) { PrintSerialNum(FamilySN[i]); // printf(" %5.1f \n", current_temp * 9 / 5 + 32); printf(" %5.1f \n", current_temp ); // converting temperature from Celsius to Fahrenheit } else printf(" Error reading temperature, verify device present:%d\n", (int)owVerify(portnum, FALSE)); } printf("\n"); } while (!key_abort()); } else printf("\n\n\nERROR, device DS1920/DS1820 not found!\n"); // release the 1-Wire Net owRelease(portnum); printf("Closing port %s.\n", argv[1]); exit(0); return 0; }
/* * main for testing */ int main (int argc, char **argv) { Bool success = true; UInt8 i; UInt8 x; SInt32 portNumber = 0; UInt8 numBatteryDevices; UInt8 numIODevices; UInt8 batteryDeviceArray[MAX_BATTERY_DEVICES][NUM_BYTES_IN_SERIAL_NUM]; UInt8 ioDeviceArray[MAX_IO_DEVICES][NUM_BYTES_IN_SERIAL_NUM]; UInt8 pageBuffer[DS2408_MAX_BYTES_IN_CHANNEL_ACCESS]; SInt16 current; double temperature; UInt16 voltage; SInt16 offsetCalibration; setDebugPrintsOn(); setProgressPrintsOn(); if (argc == 2) { portNumber = oneWireStartBus (argv[1]); } else { portNumber = oneWireStartBus (ONEWIRE_PORT); } if (portNumber < 0) { printDebug ("Failed to acquire port.\n"); success = false; } else { numBatteryDevices = FindDevices (portNumber, &batteryDeviceArray[0], FAMILY_SBATTERY, MAX_BATTERY_DEVICES); numIODevices = FindDevices (portNumber, &ioDeviceArray[0], FAMILY_PIO, MAX_IO_DEVICES); if (numBatteryDevices == 0) { success = false; ASSERT_ALWAYS_STRING ("No battery monitoring devices found.\n"); } else { printProgress ("Found %d battery monitoring devices.\n", numBatteryDevices); for (i = 0; (i < numBatteryDevices) && success; i++) { UInt32 longOne1 = 0xFFFFFF01; UInt32 longOne2 = 0xFFFFFF02; UInt16 capacity = 1000; printProgress ("Writing time %lu and capacity %d.\n", longOne1, capacity); success = writeTimeCapacityDS2438 (portNumber, &batteryDeviceArray[i][0], &longOne1, &capacity, false); if (success) { printProgress ("Writing something to DISC/EOC page.\n"); pageBuffer[0] = 0xFF; pageBuffer[1] = 0xFF; pageBuffer[2] = 0xFF; pageBuffer[3] = 0xFF; pageBuffer[4] = 0xFF; pageBuffer[5] = 0xFF; pageBuffer[6] = 0xFF; pageBuffer[7] = 0xFF; success = writeNVPageDS2438 (portNumber, &batteryDeviceArray[i][0], 2, &pageBuffer[0], DS2438_NUM_BYTES_IN_PAGE); if (success) { printProgress ("Writing something to CCA and DCA registers.\n"); success = writeNVChargeDischargeDS2438 (portNumber, &batteryDeviceArray[i][0], &longOne1, &longOne2); if (success) { printProgress ("Writing something to user data pages.\n"); for (x = 0; (x < DS2438_NUM_USER_DATA_PAGES) && success; x++) { pageBuffer[0] = x + 1; pageBuffer[1] = x + 2; pageBuffer[2] = x + 3; pageBuffer[3] = x + 4; pageBuffer[4] = x + 5; pageBuffer[5] = x + 6; pageBuffer[6] = x + 7; pageBuffer[7] = x + 8; success = writeNVUserDataDS2438 (portNumber, &batteryDeviceArray[i][0], x, &pageBuffer[0], (x + 1) * 2); } if (success) { #if 0 printProgress ("Calibrating: MAKE SURE NO CURRENT GOING THROUGH RSENS!\n"); success = performCalDS2438 (portNumber, &batteryDeviceArray[i][0], &offsetCalibration); printProgress ("New offset %d.\n", offsetCalibration); #endif } } } } } /* Check that the configurations are setup correctly and if * not write in the values we need and flush them to NV so * that they will be correct next time */ for (i = 0; (i < numBatteryDevices) && success; i++) { UInt8 config; success = readNVConfigThresholdDS2438 (portNumber, &batteryDeviceArray[i][0], &config, PNULL); if (success && ((config | DS2438_IAD_IS_ENABLED) == 0 || (config | DS2438_CA_IS_ENABLED) == 0 || (config | DS2438_EE_IS_ENABLED) == 0)) { config = DS2438_IAD_IS_ENABLED | DS2438_CA_IS_ENABLED | DS2438_EE_IS_ENABLED; success = writeNVConfigThresholdDS2438 (portNumber, &batteryDeviceArray[i][0], &config, PNULL); } } } /* numBatteryDevices > 0 */ if (numIODevices == 0) { success = false; ASSERT_ALWAYS_STRING ("No IO devices found.\n"); } else { printProgress ("Found %d IO devices.\n", numIODevices); for (i = 0; (i < numIODevices) && success; i++) { UInt8 controlByte = 0; printProgress ("Disable test mode.\n"); success = disableTestModeDS2408 (portNumber, &ioDeviceArray[i][0]); if (success) { printProgress ("Reset activity latches.\n"); success = resetActivityLatchesDS2408 (portNumber, &ioDeviceArray[i][0]); if (success) { controlByte &= ~(DS2408_DEVICE_HAS_POWER_ON_RESET | DS2408_RSTZ_IS_STROBE); controlByte |= (DS2408_SEARCH_IS_AND | DS2408_SEARCH_IS_ACTIVITY_LATCHED); printProgress ("Setup the control registers.\n"); success = writeControlRegisterDS2408 (portNumber, &ioDeviceArray[i][0], controlByte); if (success) { printProgress ("Setup the conditional search channel selection mask registers.\n"); success = writeCSChannelSelectionMaskRegisterDS2408 (portNumber, &ioDeviceArray[i][0], 0x18); if (success) { printProgress ("Setup the conditional search channel priority selection registers.\n"); success = writeCSChannelPolaritySelectionRegisterDS2408 (portNumber, &ioDeviceArray[i][0], 0x28); if (success) { UInt8 outputByte = 0x80; printProgress ("Set P7 of each device to be high.\n"); success = channelAccessWriteDS2408 (portNumber, &ioDeviceArray[i][0], &outputByte); } } } } } } } /* numIODevices > 0 */ if (success) { do { printProgress("---- IO Devices ---- \n"); for (i = 0; (i < numIODevices) && success; i++) { printProgress ("Device %d, address %.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", i, ioDeviceArray[i][0], ioDeviceArray[i][1], ioDeviceArray[i][2], ioDeviceArray[i][3], ioDeviceArray[i][4], ioDeviceArray[i][5], ioDeviceArray[i][6], ioDeviceArray[i][7]); success = readControlRegisterDS2408 (portNumber, &ioDeviceArray[i][0], &pageBuffer[0]); if (success) { printProgress (" status reg: 0x%.2x", pageBuffer[0]); success = readPIOLogicStateDS2408 (portNumber, &ioDeviceArray[i][0], &pageBuffer[0]); if (success) { printProgress (", IO reg: 0x%.2x", pageBuffer[0]); success = readPIOOutputLatchStateRegisterDS2408 (portNumber, &ioDeviceArray[i][0], &pageBuffer[0]); if (success) { UInt8 bytesRead; printProgress (", IO O/P latch reg: 0x%.2x", pageBuffer[0]); bytesRead = channelAccessReadDS2408 (portNumber, &ioDeviceArray[i][0], &pageBuffer[0], DS2408_MAX_BYTES_IN_CHANNEL_ACCESS); if (bytesRead > 0) { printProgress (", IO %d times: ", bytesRead); for (x = 0; x < bytesRead; x++) { printProgress (" 0x%.2x", pageBuffer[x]); } success = readCSChannelSelectionMaskRegisterDS2408 (portNumber, &ioDeviceArray[i][0], &pageBuffer[0]); if (success) { printProgress (", CS select reg: 0x%.2x", pageBuffer[0]); success = readCSChannelPolaritySelectionRegisterDS2408 (portNumber, &ioDeviceArray[i][0], &pageBuffer[0]); if (success) { printProgress (", CS polarity reg: 0x%.2x.\n", pageBuffer[0]); } } } } } } printProgress("\n"); } /* IO Devices for loop */ printProgress("---- Battery Devices ---- \n"); for (i = 0; (i < numBatteryDevices) && success; i++) { printProgress ("Device %d, address %.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n", i, batteryDeviceArray[i][0], batteryDeviceArray[i][1], batteryDeviceArray[i][2], batteryDeviceArray[i][3], batteryDeviceArray[i][4], batteryDeviceArray[i][5], batteryDeviceArray[i][6], batteryDeviceArray[i][7]); success = readVddDS2438 (portNumber, &batteryDeviceArray[i][0], &voltage); if (success) { printProgress (" Vdd was: %d mV\n", voltage); success = readVadDS2438 (portNumber, &batteryDeviceArray[i][0], &voltage); if (success) { printProgress (" Vad was: %d mV\n", voltage); success = readTemperatureDS2438 (portNumber, &batteryDeviceArray[i][0], &temperature); if (success) { printProgress (" Temperature was: %2.2f C\n", temperature); success = readCurrentDS2438 (portNumber, &batteryDeviceArray[i][0], ¤t); if (success) { printProgress (" Current was: %d ma (-ve means discharge)\n", current); success = readBatteryDS2438 (portNumber, &batteryDeviceArray[i][0], &voltage, ¤t); if (success) { printProgress ("Battery was %2.2f V, %1.3f A, %2.3f W\n", (double) voltage/1000, (double) current/1000, ((double) (voltage * current * -1)) / 1000000); } } } for (x = 0; (x < 8) && success; x++) { success = readNVPageDS2438 (portNumber, &batteryDeviceArray[i][0], x, &pageBuffer[0]); printProgress ("Page %d contents: %2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x\n", x, pageBuffer[0], pageBuffer[1], pageBuffer[2], pageBuffer[3], pageBuffer[4], pageBuffer[5], pageBuffer[6], pageBuffer[7]); } } } /* Check other data */ if (success) { UInt32 elapsedTime; UInt16 remainingCapacity; UInt32 piOff; UInt32 chargingStopped; UInt32 accumulatedCharge; UInt32 accumulatedDischarge; success = readTimeCapacityCalDS2438 (portNumber, &batteryDeviceArray[i][0], &elapsedTime, &remainingCapacity, &offsetCalibration, false); if (success) { printProgress ("Elapsed time: %lu secs, remaining capacity: %u mAHr, cal. offset: %d\n", elapsedTime, remainingCapacity, offsetCalibration); success = readTimePiOffChargingStoppedDS2438 (portNumber, &batteryDeviceArray[i][0], &piOff, &chargingStopped); if (success) { printProgress ("Pi last switched off: %lu secs, charging last stopped: %lu secs\n", piOff, chargingStopped); success = readNVChargeDischargeDS2438 (portNumber, &batteryDeviceArray[i][0], &accumulatedCharge, &accumulatedDischarge); if (success) { printProgress ("Accumulated charge: %lu mAHr, accumulated discharge %lu mAHr\n", accumulatedCharge, accumulatedDischarge); for (x = 0; (x < DS2438_NUM_USER_DATA_PAGES) && success; x++) { success = readNVUserDataDS2438 (portNumber, &batteryDeviceArray[i][0], x, &pageBuffer[0]); if (success) { printProgress ("UserData %d contents: %2x:%2x:%2x:%2x:%2x:%2x:%2x:%2x\n", x, pageBuffer[0], pageBuffer[1], pageBuffer[2], pageBuffer[3], pageBuffer[4], pageBuffer[5], pageBuffer[6], pageBuffer[7]); } } } } } } printProgress("\n"); } /* Battery devices for loop */ } while (!key_abort() && success); } if (!success) { printDebug ("Either something returned false or at least one IO device and at least one Battery device were not found.\n"); } oneWireStopBus (portNumber); printProgress ("Port released.\n"); } return success; }
//---------------------------------------------------------------------- // Main for tstfind // int main(int argc, char **argv) { int rslt,cnt; int portnum=0; uchar SNum[8]; // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n"); exit(1); } // attempt to acquire the 1-Wire Net if((portnum = owAcquireEx(argv[1])) < 0) { OWERROR_DUMP(stdout); exit(1); } // success printf("Port opened: %s\n",argv[1]); //---------------------------------------- // Introduction printf("\n/---------------------------------------------\n"); printf(" Loop to find all iButton on 1-Wire Net.\n\n"); do { printf("-------------------- Start of search\n"); cnt = 0; // find the first device (all devices not just alarming) rslt = owFirst(portnum, TRUE, FALSE); while (rslt) { // print the device number cnt++; printf("(%d) ",cnt); // print the Serial Number of the device just found owSerialNum(portnum,&SNum[0],TRUE); PrintSerialNum(&SNum[0]); printf("\n"); // find the next device rslt = owNext(portnum, TRUE, FALSE); } printf("-------------------- End of search\n\n"); } while (!key_abort()); // release the 1-Wire Net owRelease(portnum); printf("Closing port %s.\n", argv[1]); exit(0); return 0; }