summaryrefslogtreecommitdiff
path: root/digital/io-hub/src/robospierre/simu.host.c
blob: 745cdbc87dfddb9e9e0da286e8cce40d72371560 (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
/* simu.host.c - Host simulation. */
/* robospierre - Eurobot 2011 AI. {{{
 *
 * Copyright (C) 2011 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 "modules/utils/utils.h"
#include "modules/host/host.h"
#include "modules/host/mex.h"
#include "modules/adc/adc.h"
#include "modules/path/path.h"
#include "io.h"

/** AVR registers. */
uint8_t PORTA, DDRA, PINA, PINE, PINF;

/** Message types. */
uint8_t simu_mex_pos_report;
uint8_t simu_mex_path;

static void
simu_adc_handle (void *user, mex_msg_t *msg)
{
    uint8_t index;
    uint16_t value;
    mex_msg_pop (msg, "BH", &index, &value);
    adc_values[index] = value;
}

/** Initialise simulation. */
void
simu_init (void)
{
    const char *mex_instance;
    mex_node_connect ();
    mex_instance = host_get_instance ("io-hub0", 0);
    uint8_t mtype = mex_node_reservef ("%s:adc", mex_instance);
    mex_node_register (mtype, simu_adc_handle, 0);
    simu_mex_pos_report = mex_node_reservef ("%s:pos-report", mex_instance);
    simu_mex_path = mex_node_reservef ("%s:path", mex_instance);
}

/** Make a simulation step. */
void
simu_step (void)
{
}

void
timer_init (void)
{
    simu_init ();
}

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

uint8_t
timer_get_tick (void)
{
    return mex_node_date () / 4;
}

/** Send computed path. */
void
simu_send_path (vect_t *points, uint8_t len,
		struct path_obstacle_t *obstacles, uint8_t obstacles_nb)
{
    int i;
    mex_msg_t *m;
    m = mex_msg_new (simu_mex_path);
    for (i = 0; i < len; i++)
	mex_msg_push (m, "hh", points[i].x, points[i].y);
    mex_node_send (m);
}

void
simu_send_pos_report (vect_t *pos, uint8_t pos_nb, uint8_t id)
{
    mex_msg_t *m;
    m = mex_msg_new (simu_mex_pos_report);
    mex_msg_push (m, "b", id);
    for (; pos_nb; pos++, pos_nb--)
	mex_msg_push (m, "hh", pos->x, pos->y);
    mex_node_send (m);
}