File: encoder.cc

package info (click to toggle)
gpsim 0.32.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,644 kB
  • sloc: cpp: 121,258; asm: 54,223; ansic: 13,576; python: 9,708; sh: 4,695; makefile: 1,575; lex: 1,139; yacc: 854; pascal: 511; perl: 93; awk: 44; xml: 41
file content (208 lines) | stat: -rw-r--r-- 4,742 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
/*
   Copyright (C) 2004 Chris Emerson
   (Based on the push_button Copyright (C) 2002 Carlos Ghirardelli)

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>.
*/

/*
  encoder.cc
*/

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

#include <cassert>
#include <iostream>

#include "../config.h"    // get the definition for HAVE_GUI
#ifdef HAVE_GUI

#include <gtk/gtk.h>
#include <glib.h>
#include "../src/gpsim_time.h"
#include "../src/packages.h"
#include "../src/stimuli.h"
#include "encoder.h"


//--------------------------------------------------------------
// create_iopin_map
//
//  This is where the information for the Module's package is defined.
// Specifically, the I/O pins of the module are created.

void Encoder::_create_iopin_map()
{
  // Define the physical package.
  //   The Package class, which is a parent of all of the modules,
  //   is responsible for allocating memory for the I/O pins.
  //
  create_pkg(2);
  // 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. For the binary
  //   indicator, both pins are inputs. 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'.
  a_pin = new IO_bi_directional("a");
  addSymbol(a_pin);
  assign_pin(1, a_pin);
  a_pin->update_direction(1, true);
  b_pin = new IO_bi_directional("b");
  b_pin->update_direction(1, true);
  addSymbol(b_pin);
  assign_pin(2, b_pin);
}


//--------------------------------------------------------------
// GUI
static void cw_cb(GtkButton * , Encoder *enc)
{
  enc->send_cw();
}


static void ccw_cb(GtkButton * , Encoder *enc)
{
  enc->send_ccw();
}


void Encoder::create_widget(Encoder *enc)
{
  GtkWidget *box1 = gtk_hbox_new(FALSE, 0);
  GtkWidget *buttonl = gtk_button_new_with_label("ccw");
  GtkWidget *buttonr = gtk_button_new_with_label(" cw");
  gtk_container_set_border_width(GTK_CONTAINER(buttonl), 5);
  gtk_container_set_border_width(GTK_CONTAINER(buttonr), 5);
  g_signal_connect(buttonl, "pressed",
                   G_CALLBACK(ccw_cb), (gpointer)enc);
  g_signal_connect(buttonr, "pressed",
                   G_CALLBACK(cw_cb), (gpointer)enc);
  gtk_widget_show(buttonl);
  gtk_widget_show(buttonr);
  gtk_box_pack_start(GTK_BOX(box1), buttonl, FALSE, FALSE, 0);
  gtk_box_pack_start(GTK_BOX(box1), buttonr, FALSE, FALSE, 0);
  // Tell gpsim which widget to use in breadboard.
  enc->set_widget(box1);
}


//--------------------------------------------------------------
// construct
Module *Encoder::construct(const char *_new_name = nullptr)
{
  Encoder *enc_p = new Encoder(_new_name);
  enc_p->create_widget(enc_p);
  return enc_p;
}


Encoder::Encoder(const char * _name)
  : Module(_name, "Encoder"), rs(rot_detent)
{
  _create_iopin_map();
}


Encoder::~Encoder()
{
  /* done elsewhere RRR
  delete a_pin;
  delete b_pin;
  */
}


void Encoder::send_cw()
{
  switch (rs) {
  case rot_detent:
    rs = rot_moving_cw;
    toggle_a(); /* A toggles before B going clockwise */
    schedule_tick();
    break;

  default:
    // XXX: ought to do something here as well
    break;
  }
}


void Encoder::send_ccw()
{
  switch (rs) {
  case rot_detent:
    rs = rot_moving_ccw;
    toggle_b();
    schedule_tick();
    break;

  default:
    // XXX: ought to do something here as well
    break;
  }
}


void Encoder::toggle_a()
{
  a_pin->toggle();
  a_pin->update();
}


void Encoder::toggle_b()
{
  b_pin->toggle();
  b_pin->update();
}


void Encoder::schedule_tick()
{
  /* XXX: make the time delay configurable */
  if (!get_cycles().set_break_delta(100, this)) {
    std::cerr << "Encoder: error setting breakpoint.\n";
  }
}


void Encoder::callback()
{
  switch (rs) {
  case rot_detent:
    assert(false);
    break;

  case rot_moving_cw:
    toggle_b();
    rs = rot_detent;
    break;

  case rot_moving_ccw:
    toggle_a();
    rs = rot_detent;
    break;
  }
}

#endif // HAVE_GUI