summaryrefslogtreecommitdiff
path: root/digital/io/src/simu.host.c
blob: 1eb18a390af40c084593e37a21df1bb6b93b3736 (plain)
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
/* simu.host.c - Host simulation. */
/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
 *
 * Copyright (C) 2008 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "simu.host.h"

#include "servo.h"
#include "sharp.h"

#include "modules/utils/utils.h"
#include "modules/host/host.h"
#include "modules/host/mex.h"
#include "modules/adc/adc.h"

enum
{
    MSG_SIMU_IO_JACK = 0xb0,
    MSG_SIMU_IO_COLOR = 0xb1,
    MSG_SIMU_IO_SERVO = 0xb2,
    MSG_SIMU_IO_SHARPS = 0xb3,
    MSG_SIMU_IO_PATH = 0xb4,
};

/** Requested servo position. */
uint8_t servo_high_time_[SERVO_NUMBER];

/** Current servo position. */
uint8_t servo_high_time_current_[SERVO_NUMBER];

/** Servo speed is about 120 ms for 60 degrees.  This means about 360 ms for
 * the full swing. */
#define SERVO_SPEED (int) ((256 / (360.0 / 4.4444)) + 0.5)

/** Do not update too often, interface is too slow. */
uint8_t simu_servo_update = 10, simu_servo_update_cpt;
uint8_t simu_switch_update = 100, simu_switch_update_cpt;
uint8_t simu_sharps_update = 9, simu_sharps_update_cpt;

/** Sampled switches. */
uint8_t simu_switches;

/** Initialise simulation. */
void
simu_init (void)
{
    mex_node_connect ();
    simu_servo_update_cpt = 1;
    simu_switch_update_cpt = 1;
}

/** Make a simulation step. */
void
simu_step (void)
{
    int i;
    mex_msg_t *m;
    /* Update servos. */
    for (i = 0; i < SERVO_NUMBER; i++)
      {
	if (UTILS_ABS (servo_high_time_current_[i] - servo_high_time_[i]) <
	    SERVO_SPEED)
	    servo_high_time_current_[i] = servo_high_time_[i];
	else if (servo_high_time_current_[i] < servo_high_time_[i])
	    servo_high_time_current_[i] += SERVO_SPEED;
	else
	    servo_high_time_current_[i] -= SERVO_SPEED;
      }
    /* Send servos. */
    if (simu_servo_update && !--simu_servo_update_cpt)
      {
	simu_servo_update_cpt = simu_servo_update;
	m = mex_msg_new (MSG_SIMU_IO_SERVO);
	for (i = 0; i < SERVO_NUMBER; i++)
	    mex_msg_push (m, "B", servo_high_time_current_[i]);
	mex_node_send (m);
      }
    /* Update switches. */
    if (simu_switch_update && !--simu_switch_update_cpt)
      {
	simu_switch_update_cpt = simu_switch_update;
	uint8_t r;
	simu_switches = 0;
	/* Get color switch. */
	m = mex_msg_new (MSG_SIMU_IO_COLOR);
	m = mex_node_request (m);
	mex_msg_pop (m, "B", &r);
	mex_msg_delete (m);
	if (!r)
	    simu_switches |= 1;
	/* Get jack. */
	m = mex_msg_new (MSG_SIMU_IO_JACK);
	m = mex_node_request (m);
	mex_msg_pop (m, "B", &r);
	mex_msg_delete (m);
	if (!r)
	    simu_switches |= 2;
      }
    /* Update sharps. */
    if (simu_sharps_update && !--simu_sharps_update_cpt)
      {
	simu_sharps_update_cpt = simu_sharps_update;
	m = mex_msg_new (MSG_SIMU_IO_SHARPS);
	m = mex_node_request (m);
	uint8_t i;
	for (i = 0; i < SHARP_NUMBER; i++)
	    mex_msg_pop (m, "H", &adc_values[i]);
	mex_msg_delete (m);
      }
}

void
servo_init (void)
{
}

void
servo_set_high_time (uint8_t servo, uint8_t high_time)
{
    servo_high_time_[servo] = high_time;
}

uint8_t
servo_get_high_time (uint8_t servo)
{
    return servo_high_time_[servo];
}

void
switch_init (void)
{
}

uint8_t
switch_get_color (void)
{
    return (simu_switches & 1) ? 1 : 0;
}

uint8_t
switch_get_jack (void)
{
    return (simu_switches & 2) ? 1 : 0;
}

void
switch_update (void)
{
}

void
main_timer_init (void)
{
    simu_init ();
}

uint8_t
main_timer_wait (void)
{
    mex_node_wait_date (mex_node_date () + 4);
    simu_step ();
    return 0;
}

void
eeprom_load_param (void)
{
}

void
eeprom_save_param (void)
{
}

void
eeprom_clear_param (void)
{
}

/** Send computed path. */
void
simu_send_path (uint8_t len, uint16_t *points)
{
    int i;
    mex_msg_t *m;
    m = mex_msg_new (MSG_SIMU_IO_PATH);
    for (i = 0; i < len; i++)
	mex_msg_push (m, "h", points[i]);
    mex_node_send (m);
}