summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/io/gpio_concat.cc
blob: 646a5241595f6ef4b8073c550deb14c6c3b48544 (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
// gpio_concat.cc
// nono - programme du robot 2004. {{{
//
// Copyright (C) 2004 Nicolas Schodet
//
// Robot APB Team/Efrei 2004.
//        Web: http://assos.efrei.fr/robot/
//      Email: robot AT efrei DOT fr
//
// 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 "gpio_concat.h"

/// Constructeur.
GpioConcat::GpioConcat (Gpio &io0, Gpio &io1, int nSep)
    : io0_ (io0), io1_ (io1), nSep_ (nSep)
{
}

/// Change plusieurs sorties.
void
GpioConcat::set (int n, unsigned int bits, int nb)
{
    if (n < nSep_)
      {
	int n0, bits0, nb0;
	n0 = n;
	nb0 = n + nb > nSep_ ? nSep_ - n : nb;
	bits0 = bits & ((1 << nb0) - 1);
	io0_.set (n0, bits0, nb0);
      }
    if (n + nb > nSep_)
      {
	int n1, bits1, nb1;
	n1 = n < nSep_ ? 0 : n - nSep_;
	nb1 = n < nSep_ ? nb - nSep_ + n : nb;
	bits1 = n < nSep_ ? bits >> (nSep_ - n) : bits;
	bits1 &= ((1 << nb1) - 1);
	io1_.set (n1, bits1, nb1);
      }
}

/// Lit plusieurs entr�es.
unsigned int
GpioConcat::get (int n, int nb)
{
    int n0, bits0 = 0, nb0;
    int n1, bits1 = 0, nb1;
    if (n < nSep_)
      {
	n0 = n;
	nb0 = n + nb > nSep_ ? nSep_ - n : nb;
	bits0 = io0_.get (n0, nb0);
	bits0 &= ((1 << nb0) - 1);
      }
    if (n + nb > nSep_)
      {
	n1 = n < nSep_ ? 0 : n - nSep_;
	nb1 = n < nSep_ ? nb - nSep_ + n : nb;
	bits1 = io1_.get (n1, nb1);
	bits1 &= ((1 << nb1) - 1);
	bits1 = n < nSep_ ? bits1 << (nSep_ - n) : bits1;
      }
    return bits0 | bits1;
}

/// Change la direction de plusieurs I/O.
void
GpioConcat::dir (int n, unsigned int bits, int nb)
{
    if (n < nSep_)
      {
	int n0, bits0, nb0;
	n0 = n;
	nb0 = n + nb > nSep_ ? nSep_ - n : nb;
	bits0 = bits & ((1 << nb0) - 1);
	io0_.dir (n0, bits0, nb0);
      }
    if (n + nb > nSep_)
      {
	int n1, bits1, nb1;
	n1 = n < nSep_ ? 0 : n - nSep_;
	nb1 = n < nSep_ ? nb - nSep_ + n : nb;
	bits1 = n < nSep_ ? bits >> (nSep_ - n) : bits;
	bits1 &= ((1 << nb1) - 1);
	io1_.dir (n1, bits1, nb1);
      }
}

/// Met � jour.
void
GpioConcat::update (void)
{
    io0_.update ();
    io1_.update ();
}

/// R�cup�re le nombre d'entr�es/sorties.
int
GpioConcat::getNbIo (void)
{
    return io0_.getNbIo () + io1_.getNbIo ();
}