simulavr  1.1.0
adcpin.cpp
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002, 2003 Klaus Rudolph
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 #include "adcpin.h"
26 #include "avrerror.h"
27 
28 AdcPin::AdcPin(const char* fileName, Net& pinNet) throw():
29  _analogPin(),
30  _anaFile(fileName)
31 {
32  _analogPin.outState = Pin::ANALOG;
33  pinNet.Add(&_analogPin);
34 
35  if(!_anaFile)
36  avr_error("Cannot open Analog input file '%s'.", fileName);
37 }
38 
39 char* readNextLine(std::ifstream& f, char* buffer, unsigned len, SystemClockOffset *timeToNextStepIn_ns) {
40  for(unsigned i = 0; i < 2; ++i){
41  while(f.getline(buffer, len)){
42  // Skip comment lines
43  if(buffer[0] == '#')
44  continue;
45  return buffer;
46  }
47  f.clear();
48  f.seekg (0, std::ios::beg);
49  }
50  return 0;
51 }
52 
53 int AdcPin::Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns) {
54  char lineBuffer[1024];
55 
56  if(!readNextLine(_anaFile, lineBuffer, sizeof(lineBuffer), timeToNextStepIn_ns)) {
57  _anaFile.close();
58  }
59 
60  char* p = lineBuffer;
61  unsigned long delayInNs = strtoul(p, &p, 0);
62  int analogValue = (int)strtol(p, &p, 0);
63  if(analogValue > 5000000) // limit to 5000000 = 5.0V
64  // this isn't correct, because it dosn't respect real Vcc level
65  analogValue = 5000000;
66  _analogPin.setAnalogValue(0.000001 * analogValue);
67 
68  *timeToNextStepIn_ns = delayInNs;
69 
70  return 0;
71 }
72 
int Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns=0)
Return nonzero if a breakpoint was hit.
Definition: adcpin.cpp:53
void setAnalogValue(float value)
Set the analog value and propagte through Net.
Definition: adcpin.h:40
#define avr_error(...)
Definition: avrerror.h:135
long long SystemClockOffset
std::ifstream _anaFile
The analog input file.
Definition: adcpin.h:60
AdcPin(const char *fileName, Net &pinNet)
Definition: adcpin.cpp:28
char * readNextLine(std::ifstream &f, char *buffer, unsigned len, SystemClockOffset *timeToNextStepIn_ns)
Definition: adcpin.cpp:39
AdcAnalogPin _analogPin
Output to AVR.
Definition: adcpin.h:57
Connect Pins to each other and transfers a output change from a pin to input values for all pins...
Definition: net.h:34