summaryrefslogtreecommitdiff
path: root/digital/beacon/src/stub/simu_stub.host.c
blob: cdbacaa3c9a8bea9eac9a8ea823ed3d9f1bffd0b (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
/* simu_stub.c */
/* lol - Laser Opponent Location finding system. {{{
 *
 * Copyright (C) 2012 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 "modules/host/host.h"
#include "modules/host/mex.h"
#include "modules/twi/twi.h"
#include "modules/utils/utils.h"
#include "modules/utils/byte.h"
#include "modules/utils/crc.h"

/** This is a stub only used in simulation to bypass all beacon computations
 * and directly provide a position via TWI. */

struct position_t
{
    uint16_t x, y;
};

struct position_t simu_positions[2];

uint8_t simu_seq;

static void
simu_twi_proto_poll (void)
{
    /* Do nothing interresting with the received value, but check it
     * anyway. */
    uint8_t recv_buf[AC_TWI_SLAVE_RECV_BUFFER_SIZE];
    uint8_t recv_size;
    while ((recv_size = twi_slave_poll (recv_buf, sizeof (recv_buf))))
      {
	if (crc_compute (recv_buf + 1, recv_size - 1) != recv_buf[0])
	    continue;
	if (recv_buf[1] == simu_seq)
	    continue;
	if (recv_size != 3)
	    continue;
	simu_seq = recv_buf[1];
      }
}

static void
simu_twi_proto_update (void)
{
    /* Update TWI slave buffer after each position update. */
    uint8_t status[4 + 5 * UTILS_COUNT (simu_positions)];
    uint8_t i, index = 1;
    status[index++] = 0;
    status[index++] = 0;
    status[index++] = simu_seq;
    for (i = 0; i < UTILS_COUNT (simu_positions); i++)
      {
	status[index++] = v16_to_v8 (simu_positions[i].x, 1);
	status[index++] = v16_to_v8 (simu_positions[i].x, 0);
	status[index++] = v16_to_v8 (simu_positions[i].y, 1);
	status[index++] = v16_to_v8 (simu_positions[i].y, 0);
	status[index++] = simu_positions[i].x == (uint16_t) -1 ? 0 : 100;
      }
    status[0] = crc_compute (&status[1], sizeof (status) - 1);
    twi_slave_update (status, sizeof (status));
}

static void
simu_positions_handle (void *user, mex_msg_t *msg)
{
    uint8_t i;
    struct position_t pos;
    mex_msg_pop (msg, "BHH", &i, &pos.x, &pos.y);
    simu_positions[i] = pos;
    simu_twi_proto_update ();
}

static void
simu_init (void)
{
    const char *mex_instance;
    mex_node_connect ();
    mex_instance = host_get_instance ("beacon0", 0);
    uint8_t mtype = mex_node_reservef ("%s:position", mex_instance);
    mex_node_register (mtype, simu_positions_handle, 0);
    uint8_t i;
    for (i = 0; i < UTILS_COUNT (simu_positions); i++)
      {
	simu_positions[i].x = (uint16_t) -1;
	simu_positions[i].y = (uint16_t) -1;
      }
}

int
main (int argc, char ** argv)
{
    avr_init (argc, argv);
    simu_init ();
    twi_init (AC_BEACON_TWI_ADDRESS);
    simu_twi_proto_update ();
    while (1)
      {
	simu_twi_proto_poll ();
	mex_node_wait_date (mex_node_date () + 1);
      }
    return 0;
}