James Topham
/
OneWireCRC
Working
Fork of OneWireCRC by
Revision 1:ee5848eb918c, committed 2014-11-28
- Comitter:
- jimbotops
- Date:
- Fri Nov 28 16:24:50 2014 +0000
- Parent:
- 0:01a6a40578c9
- Commit message:
- Working with some libraries cut and less text
Changed in this revision
diff -r 01a6a40578c9 -r ee5848eb918c DS18B20.cpp --- a/DS18B20.cpp Sun Jan 03 11:57:31 2010 +0000 +++ b/DS18B20.cpp Fri Nov 28 16:24:50 2014 +0000 @@ -21,9 +21,8 @@ */ #include "DS18B20.h" -#include "DebugTrace.h" -DebugTrace pc_ds18B20(ON, TO_SERIAL); + DS18B20::DS18B20(bool crcOn, bool useAddr, bool parasitic, PinName pin) : OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18B20_ID) @@ -81,23 +80,23 @@ { case nineBit: // 0.5 deg C increments read_temp &= 0xFFF8; // bits 2,1,0 are undefined - pc_ds18B20.traceOut("9 bit resolution ...\r\n"); + printf("9 bit resolution ...\r\n"); break; case tenBit: // 0.25 deg C increments read_temp &= 0xFFFC; // bits 1,0 are undefined - pc_ds18B20.traceOut("10 bit resolution ...\r\n"); + printf("10 bit resolution ...\r\n"); break; case elevenBit: // 0.125 deg C increments read_temp &= 0xFFFE; // bit 0 is undefined - pc_ds18B20.traceOut("11 bit resolution ...\r\n"); + printf("11 bit resolution ...\r\n"); break; case twelveBit: // 0.0625 deg C increments - pc_ds18B20.traceOut("12 bit resolution ...\r\n"); + printf("12 bit resolution ...\r\n"); break; } float realTemp = (float)read_temp/16 ; - pc_ds18B20.traceOut("TEMP_READ/REAL TEMP: %f \r\n", realTemp); + printf("TEMP_READ/REAL TEMP: %f \r\n", realTemp); return realTemp; } \ No newline at end of file
diff -r 01a6a40578c9 -r ee5848eb918c DS18S20.cpp --- a/DS18S20.cpp Sun Jan 03 11:57:31 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/* -* DS18S20. Maxim DS18S20 One-Wire Thermometer. -* Uses the OneWireCRC library. -* -* Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk> -* -* This file is part of OneWireThermometer. -* -* OneWireThermometer is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* OneWireThermometer is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include "DS18S20.h" -#include "DebugTrace.h" - -DebugTrace pc_ds18S20(ON, TO_SERIAL); - -DS18S20::DS18S20(bool crcOn, bool useAddr, bool parasitic, PinName pin) : - OneWireThermometer(crcOn, useAddr, parasitic, pin, DS18S20_ID) -{ -} - -float DS18S20::calculateTemperature(BYTE* data) -{ - // DS18S20 basic resolution is always 9 bits, which can be enhanced as follows - bool signBit = false; - if (data[TEMPERATURE_MSB] & 0x80) signBit = true; - - int read_temp = (data[TEMPERATURE_MSB] << 8) + data[TEMPERATURE_LSB]; - if (signBit) - { - read_temp = (read_temp ^ 0xFFFF) + 1; // two's complement - read_temp *= -1; - } - - float readTemp = (float)read_temp/2 ; // divide by 2 - pc_ds18S20.traceOut("TEMP_READ: %f \r\n", readTemp); // 9 bit resolution value - - // convert to real temperature - float tempCount = float(data[COUNT_PER_DEG_BYTE] - data[COUNT_REMAIN_BYTE])/(float)data[COUNT_PER_DEG_BYTE]; - float realTemp = (readTemp - 0.25) + tempCount; - pc_ds18S20.traceOut("Temperature: %f \r\n", realTemp); // enhanced resolution value - - return realTemp; -} \ No newline at end of file
diff -r 01a6a40578c9 -r ee5848eb918c DS18S20.h --- a/DS18S20.h Sun Jan 03 11:57:31 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -/* -* DS18S20. Maxim DS18S20 One-Wire Thermometer. -* Uses the OneWireCRC library. -* -* Copyright (C) <2010> Petras Saduikis <petras@petras.co.uk> -* -* This file is part of OneWireThermometer. -* -* OneWireThermometer is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* OneWireThermometer is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with OneWireThermometer. If not, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef SNATCH59_DS18S20_H -#define SNATCH59_DS18S20_H - -#include "OneWireThermometer.h" - -class DS18S20 : public OneWireThermometer -{ -public: - DS18S20(bool crcOn, bool useAddr, bool parasitic, PinName pin); - - virtual void setResolution(eResolution resln) { }; // do nothing - -protected: - virtual float calculateTemperature(BYTE* data); -}; - -#endif \ No newline at end of file
diff -r 01a6a40578c9 -r ee5848eb918c DebugTrace.cpp --- a/DebugTrace.cpp Sun Jan 03 11:57:31 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,143 +0,0 @@ -/* -* DebugTrace. Allows dumping debug messages/values to serial or -* to file. -* -* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> -* -* This file is part of DebugTrace. -* -* DebugTrace is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* DebugTrace is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with DebugTrace. If not, see <http://www.gnu.org/licenses/>. -*/ - -#include "DebugTrace.h" -#include <mbed.h> -#include <stdarg.h> -#include <string.h> - -Serial logSerial(USBTX, USBRX); -LocalFileSystem local("local"); - -const char* FILE_PATH = "/local/"; -const char* EXTN = ".bak"; - -DebugTrace::DebugTrace(eLog on, eLogTarget mode, const char* fileName, int maxSize) : - enabled(on), logMode(mode), maxFileSize(maxSize), currentFileSize(0), - logFileStatus(0) -{ - // allocate memory for file name strings - int str_size = (strlen(fileName) + strlen(FILE_PATH) + strlen(EXTN) + 1) * sizeof(char); - logFile = (char*)malloc(str_size); - logFileBackup = (char*)malloc(str_size); - - // add path to log file name - strcpy(logFile, FILE_PATH); - strcat(logFile, fileName); - - // create backup file name - strcpy(logFileBackup, logFile); - strcpy(logFileBackup, strtok(logFileBackup, ".")); - strcat(logFileBackup, EXTN); -} - -DebugTrace::~DebugTrace() -{ - // dust to dust, ashes to ashes - if (logFile != NULL) free(logFile); - if (logFileBackup != NULL) free(logFileBackup); -} - -void DebugTrace::clear() -{ - // don't care about whether these fail - remove(logFile); - remove(logFileBackup); -} - -void DebugTrace::backupLog() -{ - // delete previous backup file - if (remove(logFileBackup)) - { - // standard copy stuff - char ch; - FILE* to = fopen(logFileBackup, "wb"); - if (NULL != to) - { - FILE* from = fopen(logFile, "rb"); - if (NULL != from) - { - while(!feof(from)) - { - ch = fgetc(from); - if (ferror(from)) break; - - if(!feof(from)) fputc(ch, to); - if (ferror(to)) break; - } - } - - if (NULL != from) fclose(from); - if (NULL != to) fclose(to); - } - } - - // now delete the log file, so we are ready to start again - // even if backup creation failed - the show must go on! - logFileStatus = remove(logFile); -} - -void DebugTrace::traceOut(const char* fmt, ...) -{ - if (enabled) - { - va_list ap; // argument list pointer - va_start(ap, fmt); - - if (TO_SERIAL == logMode) - { - vfprintf(logSerial, fmt, ap); - } - else // TO_FILE - { - if (0 == logFileStatus) // otherwise we failed to remove a full log file - { - // Write data to file. Note the file size may go over limit - // as we check total size afterwards, using the size written to file. - // This is not a big issue, as this mechanism is only here - // to stop the file growing unchecked. Just remember log file sizes may - // be some what over (as apposed to some what under), so don't push it - // with the max file size. - FILE* fp = fopen(logFile, "a"); - if (NULL == fp) - { - va_end(ap); - return; - } - int size_written = vfprintf(fp, fmt, ap); - fclose(fp); - - // check if we are over the max file size - // if so backup file and start again - currentFileSize += size_written; - if (currentFileSize >= maxFileSize) - { - backupLog(); - currentFileSize = 0; - } - } - } - - va_end(ap); - } -}
diff -r 01a6a40578c9 -r ee5848eb918c DebugTrace.h --- a/DebugTrace.h Sun Jan 03 11:57:31 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* -* DebugTrace. Allows dumping debug messages/values to serial or -* to file. -* -* Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk> -* -* This file is part of DebugTrace. -* -* DebugTrace is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* DebugTrace is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with DebugTrace. If not, see <http://www.gnu.org/licenses/>. -*/ - -#ifndef SNATCH59_DEBUGTRACE_H -#define SNATCH59_DEBUGTRACE_H - -enum eLog {OFF, ON}; -enum eLogTarget {TO_SERIAL, TO_FILE}; - -class DebugTrace -{ -public: - DebugTrace(eLog on, eLogTarget mode, const char* fileName = "log.txt", const int maxSize = 1024); - ~DebugTrace(); - - void clear(); - void traceOut(const char* fmt, ...); - -private: - eLog enabled; - eLogTarget logMode; - int maxFileSize; - int currentFileSize; - char* logFile; - char* logFileBackup; - int logFileStatus; // if things go wrong, don't write any more data to file - - void backupLog(); -}; - -#endif
diff -r 01a6a40578c9 -r ee5848eb918c OneWireThermometer.cpp --- a/OneWireThermometer.cpp Sun Jan 03 11:57:31 2010 +0000 +++ b/OneWireThermometer.cpp Fri Nov 28 16:24:50 2014 +0000 @@ -22,9 +22,8 @@ #include "OneWireThermometer.h" #include "OneWireDefs.h" -#include "DebugTrace.h" -DebugTrace pc(ON, TO_SERIAL); +Serial pc(USBTX, USBRX); // constructor specifies standard speed for the 1-Wire comms OneWireThermometer::OneWireThermometer(bool crcOn, bool useAddr, bool parasitic, PinName pin, int device_id) : @@ -42,27 +41,27 @@ // - not really needed except for device validation if using skipROM() if (useAddress) { - pc.traceOut("\r\n"); - pc.traceOut("New Scan\r\n"); + pc.printf("\r\n"); + pc.printf("New Scan\r\n"); oneWire.resetSearch(); if (!oneWire.search(address)) // search for 1-wire device address { - pc.traceOut("No more addresses.\r\n"); + pc.printf("No more addresses.\r\n"); wait(2); return false; } - pc.traceOut("Address = "); + pc.printf("Address = "); for (int i = 0; i < ADDRESS_SIZE; i++) { - pc.traceOut("%x ", (int)address[i]); + pc.printf("%x ", (int)address[i]); } - pc.traceOut("\r\n"); + pc.printf("\r\n"); if (OneWireCRC::crc8(address, ADDRESS_CRC_BYTE) != address[ADDRESS_CRC_BYTE]) // check address CRC is valid { - pc.traceOut("CRC is not valid!\r\n"); + pc.printf("CRC is not valid!\r\n"); wait(2); return false; } @@ -71,19 +70,19 @@ { // Make sure it is a one-wire thermometer device if (DS18B20_ID == deviceId) - pc.traceOut("You need to use a DS1820 or DS18S20 for correct results.\r\n"); + pc.printf("You need to use a DS1820 or DS18S20 for correct results.\r\n"); else if (DS18S20_ID == deviceId) - pc.traceOut("You need to use a DS18B20 for correct results.\r\n"); + pc.printf("You need to use a DS18B20 for correct results.\r\n"); else - pc.traceOut("Device is not a DS18B20/DS1820/DS18S20 device.\r\n"); + pc.printf("Device is not a DS18B20/DS1820/DS18S20 device.\r\n"); wait(2); return false; } else { - if (DS18B20_ID == deviceId) pc.traceOut("DS18B20 present and correct.\r\n"); - if (DS18S20_ID == deviceId) pc.traceOut("DS1820/DS18S20 present and correct.\r\n"); + if (DS18B20_ID == deviceId) pc.printf("DS18B20 present and correct.\r\n"); + if (DS18S20_ID == deviceId) pc.printf("DS1820/DS18S20 present and correct.\r\n"); } } @@ -115,20 +114,17 @@ resetAndAddress(); oneWire.writeByte(READSCRATCH); // read Scratchpad - pc.traceOut("read = "); for (int i = 0; i < THERMOM_SCRATCHPAD_SIZE; i++) { // we need all bytes which includes CRC check byte data[i] = oneWire.readByte(); - pc.traceOut("%x ", (int)data[i]); } - pc.traceOut("\r\n"); // Check CRC is valid if you want to if (useCRC && !(OneWireCRC::crc8(data, THERMOM_CRC_BYTE) == data[THERMOM_CRC_BYTE])) { // CRC failed - pc.traceOut("CRC FAILED... \r\n"); + pc.printf("CRC FAILED... \r\n"); dataOk = false; }
diff -r 01a6a40578c9 -r ee5848eb918c main.cpp --- a/main.cpp Sun Jan 03 11:57:31 2010 +0000 +++ b/main.cpp Fri Nov 28 16:24:50 2014 +0000 @@ -25,7 +25,6 @@ //////////////////////////////////////////////////////////////////// #include <mbed.h> -#include "DS18S20.h" #include "DS18B20.h" #include "OneWireDefs.h" @@ -35,7 +34,7 @@ int main() { // device( crcOn, useAddress, parasitic, mbed pin ) - THERMOMETER device(true, true, false, p25); + THERMOMETER device(true, true, false, p22); while (!device.initialize()); // keep calling until it works @@ -45,15 +44,6 @@ device.setResolution(nineBit); device.readTemperature(); wait(2); - device.setResolution(tenBit); - device.readTemperature(); - wait(2); - device.setResolution(elevenBit); - device.readTemperature(); - wait(2); - device.setResolution(twelveBit); - device.readTemperature(); - wait(2); } return EXIT_SUCCESS;