Notoriamente in C onn è possibile conoscere il tipo della variabile
il compilatore GCC di Arduino dovrebbe avere la funzione tipeof(expression), ma No, non funziona con Arduino
Siccome mi serve mi sono preso la briga di usare il sovraccarico delle funzioni del C++ per "simulare" la tipeof()
in pratica è una banalità:
byte tipo(byte variabile)
{
return 1;
// 1 byte
}
byte tipo(char variabile)
{
return 2;
// 2 char
}
byte tipo(int variabile)
{
return 3;
// 3 int
}
byte tipo(unsigned int variabile)
{
return 4;
// 4 insigned int
}
byte tipo(long int variabile)
{
return 5;
// 5 long int
}
byte tipo(unsigned long int variabile)
{
return 6;
// 6 unsigned long int
}
byte tipo(float variabile)
{
return 7;
// 7 float
}
byte tipo(double variabile)
{
return 8;
// 8 double
}
byte tipo(char variabile[])
{
return 9;
// 9 'c' string
}
byte tipo(byte variabile[])
{
return 10;
// 10 byte array
}
byte tipo(int variabile[])
{
return 11;
// 11 int array
}
byte tipo(unsigned int variabile[])
{
return 12;
// 12 unsigned int array
}
byte tipo(long int variabile[])
{
return 13;
// 13 long int array
}
byte tipo(unsigned long int variabile[])
{
return 14;
// 14 unsigned long int array
}
byte tipo(float variabile[])
{
return 15;
// 15 float array
}
byte tipo(double variabile[])
{
return 16;
// 16 double array
}
byte tipo(byte variabile[][2])
{
return 17;
// 16 byte bi-dimensional array
}
come vedete ho brutalmente barato, e fatto una "sporca enumerazione"
noterete che il tipo 17 è un poco inutile, dovndosi dichiarare l'entità delle dimensioni successive alla prima, di fatto è inutile
comunque il metodo è espnadibile, per chi gli servisse
naturalmente lo ho provato con la funzione
void stampatipo(byte tipo)
{
Serial.print(tipo);
switch (tipo)
{
case 1:
Serial.println(" :Byte");
break;
case 2:
Serial.println(" :Char");
break;
case 3:
Serial.println(" :Int");
break;
case 4:
Serial.println(" :Insigned Int");
break;
case 5:
Serial.println(" :Long Int");
break;
case 6:
Serial.println(" :Unsigned Long Int");
break;
case 7:
Serial.println(" :Float");
break;
case 8:
Serial.println(" :Double");
break;
case 9:
Serial.println(" :'C' string");
break;
case 10:
Serial.println(" :Byte array");
break;
case 11:
Serial.println(" :Int array");
break;
case 12:
Serial.println(" :Unsigned Int array");
break;
case 13:
Serial.println(" :Long Int array");
break;
case 14:
Serial.println(" :Unsigned Long Int array");
break;
case 15:
Serial.println(" :Float array");
break;
case 16:
Serial.println(" :Double array");
break;
case 17:
Serial.println(" :Byte bi-dimensional array");
break;
default:
Serial.print("Undefined -");
Serial.print(tipo);
Serial.println("-");
break;
}
}
che viene richiamata così:
// di Nelson "StandardOil"
// Idea da sviluppare:
// prova riconoscimento variabili
byte a;
char b;
int c;
unsigned int d;
long int e;
unsigned long int f;
float g;
double h;
char i[0];
byte j[0];
int k[0];
unsigned int l[0];
long int m[0];
unsigned long int n[0];
float o[0];
double p[0];
byte q[2][2];
#include <nelson.h>
void setup(void)
{
Serial.begin(9600);
Serial.println("prova riconoscimento variabili");
stampatipo(tipo(a));
stampatipo(tipo(b));
stampatipo(tipo(c));
stampatipo(tipo(d));
stampatipo(tipo(e));
stampatipo(tipo(f));
stampatipo(tipo(g));
stampatipo(tipo(h));
stampatipo(tipo(i));
stampatipo(tipo(j));
stampatipo(tipo(k));
stampatipo(tipo(l));
stampatipo(tipo(m));
stampatipo(tipo(n));
stampatipo(tipo(o));
stampatipo(tipo(p));
stampatipo(tipo(q));
}
void loop(void)
{
}
Naturalmente non è da copiare dritta dritta nel codice, ma da mettere in una libreria personale da includere tutte le volte che serve