Commit 48fa72471b98a0b4513c863216d759011a04c2ac

Authored by rguillom
1 parent 65705671

avancées code + fonctions get_buttons shape_joy get_joystick + fix

Showing 1 changed file with 97 additions and 47 deletions   Show diff stats
... ... @@ -49,7 +49,7 @@ return ADCH;
49 49  
50 50 // For the I/O
51 51 void output_init(void){
52   -DDRB |= 0b00011111; // PIN 8-13 as output
  52 +DDRB |= 0b00111111; // PIN 8-13 as output
53 53 }
54 54  
55 55 void output_set(unsigned char value){
... ... @@ -57,69 +57,119 @@ if(value==0) PORTB &= 0xfe; else PORTB |= 0x01;
57 57 }
58 58  
59 59 void input_init(void){
60   -DDRD |= 0x7c; // PIN 2-6 as input (Bouton Joystick + boutons)
  60 +DDRD |= 0b01111100; // PIN 2-6 as input (Bouton Joystick + boutons)
61 61 DDRC |= 0x03; // PIN 0-1 analogiques comme input (x et y joystick analogique)
62 62 }
63 63  
  64 +/*
64 65 unsigned char input_get(void){
65 66 return ((PIND&0x04)!=0)?1:0;
66 67 }
  68 +*/
67 69  
  70 +/* Commande des LED */
68 71 void commande_leds(){
69 72 unsigned char temp_serial, leds;
70   - temp_serial = get_serial();
71   - if (temp_serial !=0){
72   - leds = temp_serial;
73   - if (leds == 65) PORTB = PORTB | 0x01;
74   - if (leds == 66) PORTB = PORTB | 0x02;
75   - if (leds == 67) PORTB = PORTB | 0x04;
76   - if (leds == 68) PORTB = PORTB | 0x08;
77   - if (leds == 69) PORTB = PORTB | 0x10;
78   - if (leds == 70) PORTB = PORTB | 0x20;
79   -
80   - if (leds == 97) PORTB = PORTB & 0xfe;
81   - if (leds == 98) PORTB = PORTB & 0xfd;
82   - if (leds == 99) PORTB = PORT B & 0xfb;
83   - if (leds == 100) PORTB = PORT B & 0xf7;
84   - if (leds == 101) PORTB = PORT B & 0xef;
85   - if (leds == 102) PORTB = PORT B & 0xdf;
86   - }
  73 + temp_serial = UDR0;
  74 + if (temp_serial !=0){
  75 + leds = temp_serial;
  76 + // Allumer LED de 'A' à 'F'
  77 + if (leds == 65) PORTB = PORTB | 0x01;
  78 + if (leds == 66) PORTB = PORTB | 0x02;
  79 + if (leds == 67) PORTB = PORTB | 0x04;
  80 + if (leds == 68) PORTB = PORTB | 0x08;
  81 + if (leds == 69) PORTB = PORTB | 0x10;
  82 + if (leds == 70) PORTB = PORTB | 0x20;
  83 +
  84 + // Eteindre LED de 'a' à 'f'
  85 + if (leds == 97) PORTB = PORTB & 0xfe;
  86 + if (leds == 98) PORTB = PORTB & 0xfd;
  87 + if (leds == 99) PORTB = PORTB & 0xfb;
  88 + if (leds == 100) PORTB = PORTB & 0xf7;
  89 + if (leds == 101) PORTB = PORTB & 0xef;
  90 + if (leds == 102) PORTB = PORTB & 0xdf;
  91 + }
87 92 }
88 93  
89   -// Dummy main
  94 +/* Récupération de la valeur des boutons et mise en forme */
  95 +int get_buttons(void){
  96 + unsigned boutons = ((PIND & 0x7C)>>2) | 0b00100000;
  97 + return(boutons);
  98 +}
90 99  
91   -int main(void){
92   - init_serial(9600);
93   - output_init();
94   - input_init();
95   -
96   - unsigned char boutons, boutons_anc;
97   - unsigned char joystick_x, joystick_x_anc;
98   - unsigned char joystick_y, joystick_y_anc;
  100 +/* Met en forme l'octet de l'axe pos du joystick */
  101 +int shape_joy(unsigned char pos){
  102 + pos = (pos >> 3) | 0b00100000;
  103 + return(pos);
  104 +}
99 105  
  106 +/* Récupération de la valeur de l'axe du joystick sur la chaîne channel du CAN */
  107 +int get_joystick(int channel){
  108 + unsigned char axis;
  109 + ad_init(channel);
  110 + axis = ad_sample();
  111 + shape_joy(axis);
  112 + return(axis);
  113 +}
  114 +
  115 +/* Dummy main */
  116 +int main(void){
  117 + init_serial(9600);
  118 + output_init();
  119 + input_init();
  120 +
  121 + unsigned char boutons, boutons_anc;
  122 + unsigned char joystick_x, joystick_x_anc;
  123 + unsigned char joystick_y, joystick_y_anc;
  124 +
  125 + //Récupération des valeurs des boutons et joystick, et mise en forme
  126 + boutons = get_buttons();
  127 + joystick_x = get_joystick(1);
  128 + joystick_y = get_joystick(0);
100 129  
101   - boutons_anc = PORTD & 0x7C;
102   - ad_init(0);
103   - joystick_x_anc = ad_sample();
104   - ad_init(1);
105   - joystick_y_anc = ad_sample();
  130 + //send_serial(boutons);
  131 +
  132 + while(1){
106 133  
107   - while(1){
108   - boutons = PORTD & 0x7C;
109   - ad_init(1);
110   - joystick_x = ad_sample();
111   - ad_init(0);
112   - joystick_y = ad_sample();
  134 + boutons_anc = boutons;
  135 + joystick_x_anc = joystick_x;
  136 + joystick_y_anc = joystick_y;
113 137  
114   - if (boutons_anc != boutons) send_serial(boutons);
115   - if ((joystick_x_anc != joystick_x) || (joystick_y_anc != joystick_y){
116   - send_serial(joystick_x);
117   - send_serial(joystick_y);
118   - }
  138 + //Récupération des valeurs des boutons et joystick, et mise en forme
  139 + boutons = get_buttons();
  140 + joystick_x = get_joystick(1);
  141 + joystick_y = get_joystick(0);
  142 +
  143 +
  144 + send_serial(boutons+2);
  145 + //retour chariot
  146 + send_serial(0x0a);
  147 + send_serial(0x0d);
  148 +
  149 + //Cas où aucun caractère n'est reçu
  150 + if ((UCSR0A & (1<<RXC0)) == 0){
  151 +
  152 + //if (boutons_anc != boutons){
  153 + //if (joystick_y_anc != joystick_y){
  154 + //if ((boutons_anc != boutons) || (joystick_x_anc != joystick_x) || (joystick_y_anc != joystick_y)){
  155 + //send_serial(boutons);
  156 + //send_serial(joystick_y);
  157 + //send_serial(joystick_y);
  158 +
  159 + //send_serial('r');
  160 +
  161 + //retour chariot
  162 + //send_serial(0x0a);
  163 + //send_serial(0x0d);
  164 + //}
  165 + }
119 166  
120   - commande_leds();
121   - }
122   -
  167 + else{
  168 + //Cas où un caractère attent d'être reçu
  169 + commande_leds();
  170 + }
  171 + }
  172 +
123 173 return 0;
124 174 }
125 175  
... ...