Salut les cpp,
Je ne connais absolument rien au langage cpp mais j'ai une petite expérience du langage C et arduino.
Dans le bouquin que je suis en train de lire, j'ai découvert que les librairies arduino sont écrite en cpp et un exemple y est donner comment en fabriquer une.
J'ai bien sur foncer tête baisser a me fabriquer une librairie qui peut faire luire maximum 7 LED dans toutes les possibilités: pow(2,7) codé dans un tableau de bit, chaque bit représentant l'état d'une LED.
Voici le fichier *.h de la librairie:
Et voici le fichier *.cpp:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 #include "Arduino.h" class Blinker { public: Blinker(byte pins[],byte pins_number) ; void blinking(unsigned int duree,boolean reversed) ; private: byte _c ; byte _cc ; byte _number_of_LED ; byte _pins[] ; byte _total_possibilities ; byte _table[][255] ; } ;
Ne vous inquiétez pas si des méthodes vous sont inconnues c'est de l'arduino.
Le problème étant que quand j'instancie, de part le constructeur, l'objet de la librairie les attributs ne sont pas enregistrer quand j'appelle une autre méthode.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include "Arduino.h" #include "Blinker.h" Blinker::Blinker(byte pins[],byte pins_number) { byte _c ; byte _number_of_LED = pins_number ; byte _pins[pins_number] ; for (_c=0 ; _c < _number_of_LED ; _c++) { _pins[_c] = pins[_c] ; } for( _c=0 ; _c < _number_of_LED ; _c++) { pinMode(pins[_c],OUTPUT) ; } byte _total_possibilities ; _total_possibilities = byte(pow(2,_number_of_LED)) ; // pow(2,7) return 127 instead of 128 another mystery of arduino... _total_possibilities++ ; // [DEBUG : Serial.print(_total_possibilities) ; // [DEBUG : Serial.println() ; byte _table[_total_possibilities][_number_of_LED] ; for (_c=0 ; _c < _total_possibilities ; _c++) { byte _cc ; byte _value_to_bin=_c ; for (_cc=0 ; _cc < _number_of_LED ; _cc++) { _table[_c][_cc]=_value_to_bin % 2 ; _value_to_bin /= 2 ; // [DEBUG : Serial.print(_table[c][cc]) ; } // [DEBUG : Serial.println() ; } } void Blinker::blinking(unsigned int duree,boolean reversed) { // [DEBUG : Serial.print(total_possibilities) ; // Here i get the value 0 instead of the pow() computed value from the constructor // [DEBUG : Serial.println() ; if ( ! reversed) { for (byte _c = 0 ; _c < _total_possibilities ; _c++) { for(byte _cc=0 ; _cc < _number_of_LED ; _cc++ ) { if ( _table[_c][_cc] == 1 ) { //digitalWrite(_pins[_cc],HIGH) ; ; } Serial.print(_table[_c][_cc]) ; } Serial.println() ; delay(duree) ; //_init_LED() ; } } else { for (byte _c = _total_possibilities-1 ; _c >=0 ; _c++) { for(byte _cc=0 ; _cc < _number_of_LED ; _cc++ ) { if ( _table[_c][_cc] == 1 ) { //digitalWrite(_pins[_cc],HIGH) ; ; } Serial.print(_table[_c][_cc]) ; } Serial.println() ; delay(duree) ; //_init_LED() ; } } }
Le plus embêtant étant que j'ai copier l'exemple du bouquin, suite a ce problème afin d'en avoir le cœur net et j'ai le même problème et donc ça ne fonctionne pas non plus...
Le problème ne vient pas des attributs privées ou publique, j'ai déjà chercher dans cette direction.
Si le code vous semble correcte il est possible que le problème vienne de mon code arduino que je vous copie de tête:
Merci pour vos réponses éclairés, dans le but de connaître la cause, de la non accessibilité d'une variable initialiser dans le constructeur ( _total_possibilities et _number_of_LED en particulier ) depuis une autre méthode de l'objet.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include "Blinker.h" byte LEDS[3]={1,2,4} ; Blinker blinks(LEDS, 3) ; void setup() { Serial.begin(9600) ; } void loop() { if (Serial.available() > 0) { blinks.blinking(500,false) ; } }
Partager