File: resistor.cc

package info (click to toggle)
gpsim 0.32.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,640 kB
  • sloc: cpp: 121,258; asm: 54,223; ansic: 13,576; python: 9,708; sh: 4,695; makefile: 1,566; lex: 1,139; yacc: 854; pascal: 511; perl: 93; awk: 44; xml: 41
file content (278 lines) | stat: -rw-r--r-- 6,107 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
   Copyright (C) 1998,1999,2000,2001 T. Scott Dattalo

This file is part of the libgpsim_modules library of gpsim

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see
<http://www.gnu.org/licenses/lgpl-2.1.html>.
*/

/*

  resistor.cc

  This is gpsim's resistor component.

  resistor
  pullup
  pulldown

*/


/* IN_MODULE should be defined for modules */
//#define IN_MODULE

#include <iostream>
#include <string>

#include "../config.h"

#ifdef HAVE_GUI
#include <gtk/gtk.h>
#endif

#include "resistor.h"
#include "../src/stimuli.h"
#include "../src/value.h"
#include "../src/ui.h"

//----------------------------------------

class ResistanceAttribute : public Float {
public:
  PullupResistor *pur;

  explicit ResistanceAttribute(PullupResistor *ppur)
    : Float("resistance", 0.0, "resistance value of the pullup"),
      pur(ppur)
  {
    if (pur) {
      Float::set((pur->res)->get_Zth());
    }
  }

  void set(double r) override
  {
    Float::set(r);

    if (pur) {
      pur->res->set_Zpullup(r);
      pur->res->updateNode();
    }
  }

  void set(int r) override
  {
    Float::set((double)r);
  }
};


//----------------------------------------

class CapacitanceAttribute : public Float {
public:
  PullupResistor *pur;

  explicit CapacitanceAttribute(PullupResistor *ppur)
    : Float("capacitance", 0.0, "pin capacitance of pullup resistor"),
      pur(ppur)
  {
    if (pur) {
      Float::set(pur->res->get_Cth());
    }
  }

  void set(double r) override
  {
    Float::set(r);

    if (pur) {
      pur->res->set_Cth(r);
      pur->res->updateNode();
    }
  }

  void set(int r) override
  {
    set(double(r));
  }
};


//----------------------------------------

class VoltageAttribute : public Float {
public:
  PullupResistor *pur;

  explicit VoltageAttribute(PullupResistor *ppur)
    : Float("voltage", 0.0, "Voltage of pullup resistor"),
      pur(ppur)
  {
    if (pur) {
      Float::set(pur->res->get_Vpullup());
    }
  }

  void set(double r) override
  {
    Float::set(r);

    if (pur) {
      pur->res->set_Vpullup(r);
      pur->res->updateNode();
    }
  }

  void set(int r) override
  {
    set(double(r));
  }
};


//--------------------------------------------------------------
// PullupResistor::create_iopin_map
//

void PullupResistor::_create_iopin_map()
{
  //   The PullupResistor has only one pin.
  create_pkg(1);
  // Define the I/O pins and assign them to the package.
  //   There are two things happening here. First, there is
  //   a new I/O pin that is being created.The second thing is
  //   that the pins are "assigned" to the package. If we
  //   need to reference these newly created I/O pins (like
  //   below) then we can call the member function 'get_pin'.
  assign_pin(1, res);
}


//--------------------------------------------------------------

Module * PullupResistor::pu_construct(const char *_new_name)
{
  PullupResistor *pur = new PullupResistor(_new_name, "Pullup Resistor");
  pur->res->set_Vth(5.0);
  pur->res->set_Vpullup(5.0);
  return pur;
}


//--------------------------------------------------------------
Module * PullupResistor::pd_construct(const char *_new_name)
{
  PullupResistor *pur = new PullupResistor(_new_name, "PullDown resistor", 0.0);
  pur->res->set_Vth(0.0);
  pur->res->set_Vpullup(0.0);
  return pur;
}


//--------------------------------------------------------------
PullupResistor::PullupResistor(const char *init_name, const char *desc, float vinit)
  : Module(init_name, desc)
{
  std::string s;

  // set the module name
  if (init_name) {
    s = init_name;
    new_name(init_name);
    s.append(".pin");
  }

  //rRRres = new IO_bi_directional_pu(s.c_str());
  res = new IO_bi_directional_pu("pin");
  res->set_Vpullup(vinit);
  _create_iopin_map();
  // Default module attributes.
  //initializeAttributes();
  set_description(
    "pullup resistor or generic voltage source\n"
    " Attributes:\n"
    " .resistance - pullup resistance\n"
    " .voltage - pullup or drive voltage\n"
    " .capacitance - pin capacitance\n");

  if (verbose) {
    std::cout << description() << '\n';
  }

  // Note ResistanceAttribute is designed to give access
  // to res.Zth with a symbol name of "modulename + '.resistance'".
  attr = new ResistanceAttribute(this);
  cattr = new CapacitanceAttribute(this);
  vattr = new VoltageAttribute(this);
  addSymbol(res);
  addSymbol(attr);
  addSymbol(cattr);
  addSymbol(vattr);
  attr->set(10e3);
  cattr->set(0.0);
  res->setDriving(false);
  res->update_pullup('1', true);
  vattr->set(res->get_Vpullup());
#ifdef MANAGING_GUI
  pu_window = nullptr;

  if (get_interface().bUsingGUI()) {
    build_window();
  }

#endif
}


PullupResistor::~PullupResistor()
{
  removeSymbol(attr);
  removeSymbol(cattr);
  removeSymbol(vattr);
  delete attr;
  delete cattr;
  delete vattr;
}


#ifdef MANAGING_GUI

static void pu_cb(GtkWidget *button, gpointer pur_class)
{
  // PullupResistor *pur = (PullupResistor *)pur_class;
}


void PullupResistor::build_window()
{
  GtkWidget *buttonbox;
  GtkWidget *button;
  pu_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  buttonbox = gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(pu_window), buttonbox);
  gtk_container_set_border_width(GTK_CONTAINER(buttonbox), 1);
  button = gtk_button_new_with_label(name().c_str());
  g_signal_connect(button, "clicked",
                   G_CALLBACK(pu_cb), (gpointer)this);
  gtk_box_pack_start(GTK_BOX(buttonbox), button, TRUE, TRUE, 0);
  gtk_widget_show_all(pu_window);
  set_widget(pu_window);
}


#endif