summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/motor/output/test/test_output.c
blob: f38c7383168f858d86c85e4212397858dc7e853b (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
/* test_output.c */
/* motor - Motor control module. {{{
 *
 * 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 "modules/utils/utils.h"
#include "modules/uart/uart.h"
#include "modules/proto/proto.h"

#include "modules/motor/output/output.h"

#include "io.h"

output_t output[OUTPUT_NB];

int16_t target[OUTPUT_NB];
int16_t current[OUTPUT_NB];

uint16_t output_speed_cpt = 1, output_speed = 250;
uint16_t output_stat_cpt, output_stat;

int
main (void)
{
    uint8_t i;
    for (i = 0; i < OUTPUT_NB; i++)
      {
	output[i].min = 0x10;
	output[i].max = OUTPUT_MAX;
	output_init (i, &output[i]);
      }
    uart0_init ();
    proto_send0 ('z');
    sei ();
    while (1)
      {
	utils_delay_ms (4);
	if (output_speed && !--output_speed_cpt)
	  {
	    for (i = 0; i < OUTPUT_NB; i++)
	      {
		if (target[i] < current[i])
		    current[i]--;
		else if (target[i] > output[i].cur)
		    current[i]++;
		output_set (&output[i], current[i]);
	      }
	    output_speed_cpt = output_speed;
	  }
	if (output_stat && !--output_stat_cpt)
	  {
	    proto_send2w ('W', output[0].cur, output[1].cur);
	    output_stat_cpt = output_stat;
	  }
	while (uart0_poll ())
	    proto_accept (uart0_getc ());
      }
}

/** Handle incoming messages. */
void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
    uint8_t i;
    uint8_t ok = 1;
    uint8_t index = args[0];
    int16_t value = v8_to_v16 (args[1], args[2]);
#define c(cmd, size) (cmd << 8 | size)
    switch (c (cmd, size))
      {
      case c ('z', 0):
	/* Reset. */
	utils_reset ();
	break;
      case c ('w', 0):
	/* Set zero value. */
	for (i = 0; i < OUTPUT_NB; i++)
	  {
	    output_set (&output[index], 0);
	    current[index] = target[index] = 0;
	  }
      case c ('w', 3):
	/* Set direct value.
	 * - b: output index.
	 * - w: output value. */
	if (index < OUTPUT_NB)
	  {
	    output_set (&output[index], value);
	    target[index] = current[index];
	  }
	else
	    ok = 0;
	break;
      case c ('t', 3):
	/* Set target.
	 * - b: output index.
	 * - w: output target. */
	if (index < OUTPUT_NB)
	  {
	    current[index] = output[index].cur;
	    target[index] = value;
	  }
	else
	    ok = 0;
	break;
      case c ('s', 2):
	/* Set targeting speed.
	 * - w: loops between update. */
	output_speed_cpt = output_speed = v8_to_v16 (args[0], args[1]);
	break;
      case c ('r', 2):
	/* Set reverse flag.
	 * - b: output index.
	 * - b: reverse flag. */
	if (index < OUTPUT_NB)
	    output_set_reverse (&output[index], args[1]);
	else
	    ok = 0;
	break;
      case c ('W', 2):
	/* Output stats. */
	output_stat_cpt = output_stat = v8_to_v16 (args[0], args[1]);
	break;
      default:
	ok = 0;
	break;
      }
    if (ok)
	proto_send (cmd, size, args);
    else
	proto_send0 ('?');
#undef c
}