mappare un telecomando IR


Vediamo come mappare un telcomando IR per fare i nostri Porci Comodi

Bello poter controllare un dispositivo – lampada robot o quasiasi altra cosa cosa abbiamo costruito – ma come mappare un telecomando??

Potremmo voler mappare il telecomando della nostra TV, spesso i telecomandi dei nuovi televisori hanno la possibilità di controllare altro oltre al TV, magari un satbox o un DVDplayer o Videoregistratore… o magari mappare un telecomando recuperatore nel dimenticatoio.. o infine giocare con un dispositivo universale…. In tutti questi casi, abbiamo bisogno di capire cosa succede quando premiamo un tasto del telecomando…

Ovviamente in questo post vengono presi in esame telecomandi che usano una delle codifiche supportate dalla liberia IRremote Ken Shirriff (tranne sony che non ho avuto modo di provare).

Parliamo quindi di:

  • RC/5
  • RC/6
  • NEC

Le differenze fra questi tre protocolli sono parecchie, ma per quello che interessa a noi l’unica differenza sta nel codice che viene trasmesso quando viene mantenuto premuto un tasto sul telecomando.

RC/5e6 ripetono il codice del tasto premuto

NEC invia un codice che è uguale per tutti i tasti

Rimando a queste pagine sui dettagli sui protocolli in inglese:

RC5 RC6 NEC

Per fare delle prove, collego direttamente all’arduino il ricevitore IR, sarebbe meglio collegare il pin RECV_PIN del ricevitore all’arduino tramite una resistenza in modo da limitare la corrente.

lo sketch è testato con un ricevitore della VISHAY tsop1838, collegato in modo da sfruttare i pin 5,6,7 dell’arduino e montato con la parte ricevente orientata verso l’esterno della scheda ed è una versione modificata di uno degli sketch presenti nella libreria.

/* * IRremote: IRrecvDump - dump details of IR codes with IRrecv * An IR detector/demodulator must be connected to the input RECV_PIN. * gnd_pin and vcc_pin can provide power for IR sensor * * Modified by 03 May 2010 * Matteo Lampugnani * now include a file to store key mapping of IRremote * add a line in serial output * * Version 0.1 July, 2009 * Copyright 2009 Ken Shirriff * http://arcfn.com */ #include <IRremote.h> #include "IRmap.h"; int gnd_pin = 6; int vcc_pin = 7; int RECV_PIN = 5; IRrecv irrecv(RECV_PIN); decode_results results; unsigned long decode(unsigned long value){ unsigned int b0 = (value&0xff00)>>8; unsigned int b1 = (value&0x00ff); //  Serial.println(b0, HEX); //  Serial.println(b1, HEX); unsigned int ret = -1; for (int i=0; i < REMOTEBUTTON; i++) { if(b0 == myBtn[i][0] && b1 == myBtn[i][1]){ ret = myBtn[i][2]; continue; } } return ret; } void setup() { pinMode(gnd_pin, OUTPUT); digitalWrite(gnd_pin, LOW); pinMode(vcc_pin, OUTPUT); digitalWrite(vcc_pin, HIGH); Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver } // Dumps out the decode_results structure. // Call this after IRrecv::decode() // void * to work around compiler issue //void dump(void *v) { //  decode_results *results = (decode_results *)v void dump(decode_results *results) { int count = results->rawlen; if (results->decode_type == UNKNOWN) { Serial.println("Could not decode message"); } else { if (results->decode_type == NEC) { Serial.print("Decoded NEC: "); } else if (results->decode_type == SONY) { Serial.print("Decoded SONY: "); } else if (results->decode_type == RC5) { Serial.print("Decoded RC5: "); } else if (results->decode_type == RC6) { Serial.print("Decoded RC6: "); } Serial.print(results->value, HEX); Serial.print(" ("); Serial.print(results->bits, DEC); Serial.println(" bits)"); } Serial.print("Pressed button: "); Serial.println(decode(results->value)); Serial.print("Raw ("); Serial.print(count, DEC); Serial.print("): "); for (int i = 0; i < count; i++) { if ((i % 2) == 1) { Serial.print(results->rawbuf[i]*USECPERTICK, DEC); } else { Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC); } Serial.print(" "); } Serial.println(""); } void loop() { if (irrecv.decode(&results)) { // Serial.println(results.value, HEX); dump(&results); irrecv.resume(); // Receive the next value } }

Il codice riportato sotto corrisponde alla mappatura di un telecomando per controllare luci RGB che usa la codifica NEC.

#define REMOTEBUTTON 24 #define CODEBUTTON 3 //Remote Button layout // // //  00  01  02  03    ##  Lup    Ldn    Lof    Lon //  04  05  06  07    ##  Red    Gre    Blu    Whi //  08  09  10  11    ##  XXX    XXX    XXX    fls //  12  13  14  15    ##  XXX    XXX    XXX    str //  16  17  18  19    ##  XXX    XXX    XXX    fad //  20  21  22  23    ##  XXX    XXX    XXX    smt unsigned int myBtn[REMOTEBUTTON][CODEBUTTON] = { {0x90, 0x6f, 0x00}, {0xb8, 0x47, 0x01}, {0xf8, 0x07, 0x02}, {0xb0, 0x4f, 0x03}, {0x98, 0x67, 0x04}, {0xd8, 0x27, 0x05}, {0x88, 0x77, 0x06}, {0xa8, 0x57, 0x07}, {0xe8, 0x17, 0x08}, {0x48, 0xb7, 0x09}, {0x68, 0x97, 0x0a}, {0xb2, 0x4d, 0x0b}, {0x02, 0xfd, 0x0c}, {0x32, 0xcd, 0x0d}, {0x20, 0xdf, 0x0e}, {0x00, 0xff, 0x0f}, {0x50, 0xaf, 0x10}, {0x78, 0x87, 0x11}, {0x70, 0x8f, 0x12}, {0x58, 0xa7, 0x13}, {0x38, 0xc7, 0x14}, {0x28, 0xd7, 0x15}, {0xf0, 0x0f, 0x16}, {0x30, 0xcf, 0x17}};

Ed ecco l’output tipo recuperabile dalla porta seriale (mi rendo conto che l’impaginazione del preformattato farà schifo e mi scuso):

Decoded NEC: FFB24D (32 bits) Pressed button: 11 Raw (68): 15422 9000 -4550 550 -550 550 -600 600 -550 500 -600 550 -600 550 -550 550 -600 550 -550 600 -1650 550 -1700 550 -1700 600 -1650 550 -1750 500 -1700 600 -1650 550 -1750 500 -1750 550 -550 550 -1700 600 -1650 550 -600 550 -550 550 -1700 600 -550 550 -550 550 -1750 500 -600 550 -550 600 -1650 550 -1750 500 -600 550 -1700 550 Decoded NEC: FFE817 (32 bits) Pressed button: 8 Raw (68): -30656 9000 -4500 550 -600 500 -600 550 -550 600 -550 550 -550 550 -600 600 -550 500 -600 550 -1700 550 -1700 600 -1650 550 -1700 550 -1700 600 -1650 550 -1750 500 -1700 600 -1650 550 -1750 500 -1700 600 -550 550 -1700 600 -550 500 -600 550 -600 550 -550 550 -600 550 -550 600 -1650 550 -600 550 -1700 550 -1700 550 -1700 550 Decoded NEC: FF32CD (32 bits) Pressed button: 13 Raw (68): 770 9050 -4500 600 -550 500 -600 550 -600 550 -550 550 -550 600 -550 600 -550 500 -600 550 -1700 550 -1700 600 -1650 550 -1700 550 -1700 600 -1650 550 -1750 500 -1750 550 -550 550 -550 600 -1700 500 -1750 550 -550 550 -600 550 -1700 550 -550 550 -1750 500 -1700 600 -550 550 -550 550 -1750 500 -1750 550 -550 550 -1700 600

la riga Decoded NEC indica il codice rilevato dal ricevitore quando si preme il particolare bottone

La riga Pressed button indica il numero del tasto che avete premuto (questo numero lo decidete voi nel file IRmap.h).

Il file IRmap.h è un array di due dimensioni: REMOTEBUTTON, il numero di tasti del vostro telecomando e CODEBUTTON che continene nella posizione 0 e 1 gli ultimi 4 caratteri del codice del vostro telecomando e nella posizione 2 il numero del tasto. Le posizioni 0 e 1 devono corrispondere a quelle provenienti dal telecomando, la posizione 2 la decidete voi.

Sfruttando il valore myBtn[x][2] potrete eseguire dei controlli  o altro all’interno dei vostri sketch per eseguire operazioni o prendere decisioni.

A questo punto siete in grado di modificare il file IRmap.h inserendo il codice dei tasti del vostro telcomando: uno a uno premete tutti i tasti e andate a modificare il file (la prime due posizioni di ogni riga, la terza è il numero che volete dare al vostro tasto), ricordo che il file vuole valori esadecimali. Di tanto in tanto potete caricare lo sketch sull’arduino e provare i tasti già registrati…

Quando avete finito, potete provare quest’altro sketch.. più breve, che vi printerà sulla porta seriale solo il numero del tasto ( il myBtn[x][2] di cui parlavo prima):

/*Print out button number of a preconfigured IRremote  *Version 0.1 03-may-2010  *Copyright 2010 Matteo Lampugnani  *http://blog.lampugnani.org  */ #include &lt;IRremote.h&gt; //#include &lt;avr/pgmspace.h&gt; <pre>#include "IRmap.h";</pre> int gnd_pin = 6; int vcc_pin = 7; int RECV_PIN = 5; IRrecv irrecv(RECV_PIN); decode_results results; unsigned long decode(unsigned long value){ unsigned int b0 = (value&amp;0xff00)&gt;&gt;8; unsigned int b1 = (value&amp;0x00ff); //  Serial.println(b0, HEX); //  Serial.println(b1, HEX); unsigned int ret = -1; for (int i=0; i &lt; REMOTEBUTTON; i++) { if(b0 == myBtn[i][0] &amp;&amp; b1 == myBtn[i][1]){ ret = myBtn[i][2]; continue; } } return ret; } void setup() { pinMode(gnd_pin, OUTPUT); digitalWrite(gnd_pin, LOW); pinMode(vcc_pin, OUTPUT); digitalWrite(vcc_pin, HIGH); Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver } void loop() { if (irrecv.decode(&amp;results)) { Serial.print("Pressed button: "); //    Serial.println(results.value, HEX); Serial.println(decode(results.value)); irrecv.resume(); // Receive the next value } }

Ed infine l’output tipo di questo ultimo sketch (codifica NEC):

Pressed button: 13 Pressed button: 65535 Pressed button: 12 Pressed button: 12 Pressed button: 8 Pressed button: 9 Pressed button: 65535 Pressed button: 14 Pressed button: 65535 Pressed button: 65535 Pressed button: 65535 Pressed button: 65535 Pressed button: 65535

Ora potete usare myBtn[x][2] per fare tutti i controllo che volete all’interno dei vostri sketch!!

Visita il mio miniSHOP!! Potrati trovare Il ricevitore IR e molto altri componenti interessanti. Spedizioni rapide!

Ben accetti domane e commenti!!


Tag: , , , ,

Lascia un Commento