Program to read the temperature from multiple DS18B20 sensors on the same pin. The original library/code came from Petras Saduikis (see http://mbed.org/users/snatch59/programs/OneWireCRC/gpdz56), but I've modified it to remember the address of multiple sensors all connected to the same data pin. My sample program displays the temperature of each device in turn. If you want to see more of what's going on behind the scenes turn on the debug by setting DebugTrace pc(OFF, TO_SERIAL); to ON (instead of OFF) in the couple of places it's used - it will log the device addresses as they are found etc. The addresses are set in the devices at the factory - I don't think they can be changed, the search always seems to find them in the same order, but this won't be anything to do with the way you've plugged them in. I've had a play with up to 7 sensors (the code has a limit of 10 hardwired in it, but this would be easy to change)

Dependencies:   mbed

Committer:
tonymudd
Date:
Sun Jul 17 15:56:49 2011 +0000
Revision:
0:fb8b6da96a8b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tonymudd 0:fb8b6da96a8b 1 /*
tonymudd 0:fb8b6da96a8b 2 * DebugTrace. Allows dumping debug messages/values to serial or
tonymudd 0:fb8b6da96a8b 3 * to file.
tonymudd 0:fb8b6da96a8b 4 *
tonymudd 0:fb8b6da96a8b 5 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
tonymudd 0:fb8b6da96a8b 6 *
tonymudd 0:fb8b6da96a8b 7 * This file is part of DebugTrace.
tonymudd 0:fb8b6da96a8b 8 *
tonymudd 0:fb8b6da96a8b 9 * DebugTrace is free software: you can redistribute it and/or modify
tonymudd 0:fb8b6da96a8b 10 * it under the terms of the GNU General Public License as published by
tonymudd 0:fb8b6da96a8b 11 * the Free Software Foundation, either version 3 of the License, or
tonymudd 0:fb8b6da96a8b 12 * (at your option) any later version.
tonymudd 0:fb8b6da96a8b 13 *
tonymudd 0:fb8b6da96a8b 14 * DebugTrace is distributed in the hope that it will be useful,
tonymudd 0:fb8b6da96a8b 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
tonymudd 0:fb8b6da96a8b 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tonymudd 0:fb8b6da96a8b 17 * GNU General Public License for more details.
tonymudd 0:fb8b6da96a8b 18 *
tonymudd 0:fb8b6da96a8b 19 * You should have received a copy of the GNU General Public License
tonymudd 0:fb8b6da96a8b 20 * along with DebugTrace. If not, see <http://www.gnu.org/licenses/>.
tonymudd 0:fb8b6da96a8b 21 */
tonymudd 0:fb8b6da96a8b 22
tonymudd 0:fb8b6da96a8b 23 #include "DebugTrace.h"
tonymudd 0:fb8b6da96a8b 24 #include <mbed.h>
tonymudd 0:fb8b6da96a8b 25 #include <stdarg.h>
tonymudd 0:fb8b6da96a8b 26 #include <string.h>
tonymudd 0:fb8b6da96a8b 27
tonymudd 0:fb8b6da96a8b 28 Serial logSerial(USBTX, USBRX);
tonymudd 0:fb8b6da96a8b 29 LocalFileSystem local("local");
tonymudd 0:fb8b6da96a8b 30
tonymudd 0:fb8b6da96a8b 31 const char* FILE_PATH = "/local/";
tonymudd 0:fb8b6da96a8b 32 const char* EXTN = ".bak";
tonymudd 0:fb8b6da96a8b 33
tonymudd 0:fb8b6da96a8b 34 DebugTrace::DebugTrace(eLog on, eLogTarget mode, const char* fileName, int maxSize) :
tonymudd 0:fb8b6da96a8b 35 enabled(on), logMode(mode), maxFileSize(maxSize), currentFileSize(0),
tonymudd 0:fb8b6da96a8b 36 logFileStatus(0)
tonymudd 0:fb8b6da96a8b 37 {
tonymudd 0:fb8b6da96a8b 38 // allocate memory for file name strings
tonymudd 0:fb8b6da96a8b 39 int str_size = (strlen(fileName) + strlen(FILE_PATH) + strlen(EXTN) + 1) * sizeof(char);
tonymudd 0:fb8b6da96a8b 40 logFile = (char*)malloc(str_size);
tonymudd 0:fb8b6da96a8b 41 logFileBackup = (char*)malloc(str_size);
tonymudd 0:fb8b6da96a8b 42
tonymudd 0:fb8b6da96a8b 43 // add path to log file name
tonymudd 0:fb8b6da96a8b 44 strcpy(logFile, FILE_PATH);
tonymudd 0:fb8b6da96a8b 45 strcat(logFile, fileName);
tonymudd 0:fb8b6da96a8b 46
tonymudd 0:fb8b6da96a8b 47 // create backup file name
tonymudd 0:fb8b6da96a8b 48 strcpy(logFileBackup, logFile);
tonymudd 0:fb8b6da96a8b 49 strcpy(logFileBackup, strtok(logFileBackup, "."));
tonymudd 0:fb8b6da96a8b 50 strcat(logFileBackup, EXTN);
tonymudd 0:fb8b6da96a8b 51 }
tonymudd 0:fb8b6da96a8b 52
tonymudd 0:fb8b6da96a8b 53 DebugTrace::~DebugTrace()
tonymudd 0:fb8b6da96a8b 54 {
tonymudd 0:fb8b6da96a8b 55 // dust to dust, ashes to ashes
tonymudd 0:fb8b6da96a8b 56 if (logFile != NULL) free(logFile);
tonymudd 0:fb8b6da96a8b 57 if (logFileBackup != NULL) free(logFileBackup);
tonymudd 0:fb8b6da96a8b 58 }
tonymudd 0:fb8b6da96a8b 59
tonymudd 0:fb8b6da96a8b 60 void DebugTrace::clear()
tonymudd 0:fb8b6da96a8b 61 {
tonymudd 0:fb8b6da96a8b 62 // don't care about whether these fail
tonymudd 0:fb8b6da96a8b 63 remove(logFile);
tonymudd 0:fb8b6da96a8b 64 remove(logFileBackup);
tonymudd 0:fb8b6da96a8b 65 }
tonymudd 0:fb8b6da96a8b 66
tonymudd 0:fb8b6da96a8b 67 void DebugTrace::backupLog()
tonymudd 0:fb8b6da96a8b 68 {
tonymudd 0:fb8b6da96a8b 69 // delete previous backup file
tonymudd 0:fb8b6da96a8b 70 if (remove(logFileBackup))
tonymudd 0:fb8b6da96a8b 71 {
tonymudd 0:fb8b6da96a8b 72 // standard copy stuff
tonymudd 0:fb8b6da96a8b 73 char ch;
tonymudd 0:fb8b6da96a8b 74 FILE* to = fopen(logFileBackup, "wb");
tonymudd 0:fb8b6da96a8b 75 if (NULL != to)
tonymudd 0:fb8b6da96a8b 76 {
tonymudd 0:fb8b6da96a8b 77 FILE* from = fopen(logFile, "rb");
tonymudd 0:fb8b6da96a8b 78 if (NULL != from)
tonymudd 0:fb8b6da96a8b 79 {
tonymudd 0:fb8b6da96a8b 80 while(!feof(from))
tonymudd 0:fb8b6da96a8b 81 {
tonymudd 0:fb8b6da96a8b 82 ch = fgetc(from);
tonymudd 0:fb8b6da96a8b 83 if (ferror(from)) break;
tonymudd 0:fb8b6da96a8b 84
tonymudd 0:fb8b6da96a8b 85 if(!feof(from)) fputc(ch, to);
tonymudd 0:fb8b6da96a8b 86 if (ferror(to)) break;
tonymudd 0:fb8b6da96a8b 87 }
tonymudd 0:fb8b6da96a8b 88 }
tonymudd 0:fb8b6da96a8b 89
tonymudd 0:fb8b6da96a8b 90 if (NULL != from) fclose(from);
tonymudd 0:fb8b6da96a8b 91 if (NULL != to) fclose(to);
tonymudd 0:fb8b6da96a8b 92 }
tonymudd 0:fb8b6da96a8b 93 }
tonymudd 0:fb8b6da96a8b 94
tonymudd 0:fb8b6da96a8b 95 // now delete the log file, so we are ready to start again
tonymudd 0:fb8b6da96a8b 96 // even if backup creation failed - the show must go on!
tonymudd 0:fb8b6da96a8b 97 logFileStatus = remove(logFile);
tonymudd 0:fb8b6da96a8b 98 }
tonymudd 0:fb8b6da96a8b 99
tonymudd 0:fb8b6da96a8b 100 void DebugTrace::traceOut(const char* fmt, ...)
tonymudd 0:fb8b6da96a8b 101 {
tonymudd 0:fb8b6da96a8b 102 if (enabled)
tonymudd 0:fb8b6da96a8b 103 {
tonymudd 0:fb8b6da96a8b 104 va_list ap; // argument list pointer
tonymudd 0:fb8b6da96a8b 105 va_start(ap, fmt);
tonymudd 0:fb8b6da96a8b 106
tonymudd 0:fb8b6da96a8b 107 if (TO_SERIAL == logMode)
tonymudd 0:fb8b6da96a8b 108 {
tonymudd 0:fb8b6da96a8b 109 vfprintf(logSerial, fmt, ap);
tonymudd 0:fb8b6da96a8b 110 }
tonymudd 0:fb8b6da96a8b 111 else // TO_FILE
tonymudd 0:fb8b6da96a8b 112 {
tonymudd 0:fb8b6da96a8b 113 if (0 == logFileStatus) // otherwise we failed to remove a full log file
tonymudd 0:fb8b6da96a8b 114 {
tonymudd 0:fb8b6da96a8b 115 // Write data to file. Note the file size may go over limit
tonymudd 0:fb8b6da96a8b 116 // as we check total size afterwards, using the size written to file.
tonymudd 0:fb8b6da96a8b 117 // This is not a big issue, as this mechanism is only here
tonymudd 0:fb8b6da96a8b 118 // to stop the file growing unchecked. Just remember log file sizes may
tonymudd 0:fb8b6da96a8b 119 // be some what over (as apposed to some what under), so don't push it
tonymudd 0:fb8b6da96a8b 120 // with the max file size.
tonymudd 0:fb8b6da96a8b 121 FILE* fp = fopen(logFile, "a");
tonymudd 0:fb8b6da96a8b 122 if (NULL == fp)
tonymudd 0:fb8b6da96a8b 123 {
tonymudd 0:fb8b6da96a8b 124 va_end(ap);
tonymudd 0:fb8b6da96a8b 125 return;
tonymudd 0:fb8b6da96a8b 126 }
tonymudd 0:fb8b6da96a8b 127 int size_written = vfprintf(fp, fmt, ap);
tonymudd 0:fb8b6da96a8b 128 fclose(fp);
tonymudd 0:fb8b6da96a8b 129
tonymudd 0:fb8b6da96a8b 130 // check if we are over the max file size
tonymudd 0:fb8b6da96a8b 131 // if so backup file and start again
tonymudd 0:fb8b6da96a8b 132 currentFileSize += size_written;
tonymudd 0:fb8b6da96a8b 133 if (currentFileSize >= maxFileSize)
tonymudd 0:fb8b6da96a8b 134 {
tonymudd 0:fb8b6da96a8b 135 backupLog();
tonymudd 0:fb8b6da96a8b 136 currentFileSize = 0;
tonymudd 0:fb8b6da96a8b 137 }
tonymudd 0:fb8b6da96a8b 138 }
tonymudd 0:fb8b6da96a8b 139 }
tonymudd 0:fb8b6da96a8b 140
tonymudd 0:fb8b6da96a8b 141 va_end(ap);
tonymudd 0:fb8b6da96a8b 142 }
tonymudd 0:fb8b6da96a8b 143 }