Now supports DS18B20 and DS18S20 Maxim/Dallas one-wire thermometer devices. Also supports DS18S20 in 9, 10, 11, and 12 bit resolution modes. 'Use Address' mode now checks if the correct device type is present, and informs the user which device to use. Correct temperature conversion times now used in non-parasitic mode. The device should be placed at least 6 inches (15 cm) from the mbed board in order to accurately read ambient temperature.

Dependencies:   mbed

Fork of OneWireCRC by Petras Saduikis

Committer:
lagish
Date:
Mon Jul 13 17:22:49 2015 +0000
Revision:
1:ade3fad22621
Parent:
0:01a6a40578c9
Draft1

Who changed what in which revision?

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