summaryrefslogtreecommitdiff
path: root/digital/avr/modules/motor/output/output.c
blob: f1e87b95cc697facf04dd40ee113dab6d3c0832b (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
/* 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 "output.h"

#ifdef HOST
# include "output_host.h"
# define OUTPUT_MODULES ,host
#else
# if AC_OUTPUT_USE_PWM_OCR
#  include "pwm_ocr/output_pwm_ocr.h"
#  define OUTPUT_MODULE_1 ,pwm_ocr
# else
#  define OUTPUT_MODULE_1
# endif
# if AC_OUTPUT_USE_PWM_MP
#  include "pwm_mp/output_pwm_mp.h"
#  define OUTPUT_MODULE_2 ,pwm_mp
# else
#  define OUTPUT_MODULE_2
# endif
# define OUTPUT_MODULES \
    OUTPUT_MODULE_1 OUTPUT_MODULE_2
#endif

void
output_init (uint8_t index, output_t *output)
{
    /* Initialize corresponding module. */
#ifdef HOST
    output_host_init (index, output);
#else
# define output_init__(module, module_index) \
    PREPROC_PASTE (output_, module, _init) (module_index, output)
# define output_init_(index, output_info) \
    case index: output_init__ output_info; break;
    switch (index)
      {
	PREPROC_FOR_ENUM (output_init_, AC_OUTPUT_LIST)
      }
#endif
}

void
output_set (output_t *output, int16_t value)
{
    /* Reverse output if requested. */
    if (output->reverse)
	value = -value;
    /* Saturation and dead zone. */
    if (value > output->max)
	output->cur = output->max;
    else if (value < -output->max)
	output->cur = -output->max;
    else if (value > -output->min && value < output->min)
	output->cur = 0;
    else
	output->cur = value;
}

void
output_update (void)
{
    /* Update each module. */
#define output_update_(module) PREPROC_PASTE (output_, module, _update) ();
    PREPROC_FOR (output_update_ OUTPUT_MODULES)
}