Hai provato a vedere il http://www.vincenzov.net/tutorial/RaspberryPi/i2c_c.htm (http://www.vincenzov.net/tutorial/RaspberryPi/i2c_c.htm)
Iniziare con un sorgente potrebbe essere d'aiuto.
Bash ha le sue madonne ;)
http://www.vincenzov.net/tutorial/RaspberryPi/LM92.c (http://www.vincenzov.net/tutorial/RaspberryPi/LM92.c)
questo legge il sensore di temperatura.
#define I2C_ADDR 0x4B // Device adress
#define LM92_RES 0.0625 // Resolution (see data sheet)
static const char *device = "/dev/i2c-1"; // I2C bus
static void exit_on_error (const char *s) // Exit and print error code
{ perror(s);
abort();
}
int main(int argc, char *argv[])
{
int fd;
uint8_t buffer[2];
int16_t data;
double temperature;
printf("Rapsberry Pi: I2C in C - Versione 0.61 - Luglio 2013\n");
printf("Copyright (c) 2013, Vincenzo Villa (http://www.vincenzov.net)\n");
printf("Creative Commons | Attribuzione-Condividi allo stesso modo 3.0 Unported.\n");
printf("Creative Commons | Attribution-Share Alike 3.0 Unported\n");
printf("http://www.vincenzov.net/tutorial/elettronica-di-base/RaspberryPi/i2c-c.htm\n\n");
// Open I2C device
if ((fd = open(device, O_RDWR)) < 0) exit_on_error ("Can't open I2C device");
// Set I2C slave address
if (ioctl(fd, I2C_SLAVE,I2C_ADDR) < 0) exit_on_error ("Can't talk to slave");
// Read from LM92 (from Power-up default register)
printf("Reading from LM92 at address 0x%.2X on %s\n\n", I2C_ADDR, device);
if (read(fd,buffer,2) != 2 ) exit_on_error ("Failed to read from the i2c bus");
printf("Data read from Power-up default register: 0x%.2X%.2X\n\n", buffer[0], buffer[1]);
data = buffer[0];
data = data << 8;
data = data | buffer[1];
data = data >> 3;
temperature = LM92_RES * data;
printf("Temperature: %2.1f °C\n\n", temperature);
close(fd);
return (0);
}
potrebbe diventare
const
I2C_ADDR=$4B; // Device adress
LM92_RES=0.0625; // Resolution (see data sheet)
device:String= '/dev/i2c-1'; // I2C bus
procedure exit_on_error (const message : string) // Exit and print error code
begin
perror(s); halt(1);
end;
var
fd: File; buffer: array [0..1] of byte; data: smallint; temperature:double ;
begin
writeln("Rapsberry Pi: I2C in C - Versione 0.61 - Luglio 2013\n"); writeln("Copyright (c) 2013, Vincenzo Villa (http://www.vincenzov.net)\n");
writeln("Creative Commons | Attribuzione-Condividi allo stesso modo 3.0 Unported.\n"); writeln("Creative Commons | Attribution-Share Alike 3.0 Unported\n");
writeln("http://www.vincenzov.net/tutorial/elettronica-di-base/RaspberryPi/i2c-c.htm\n\n"); // Open I2C device
fd := FileOpen(device, O_RDWR);
if (fc <= 0) then
exit_on_error ("Can't open I2C device"); // Set I2C slave address
if (fpioctl(fd, I2C_SLAVE,I2C_ADDR) < 0) then exit_on_error ("Can't talk to slave"); // Read from LM92 (from Power-up default register)
writeln(Format("Reading from LM92 at address 0x%.2X on %s\n\n", [I2C_ADDR, device]); if (read(fd,buffer,2) <> 2 ) then exit_on_error ("Failed to read from the i2c bus"); Writeln(Format("Data read from Power-up default register: 0x%.2X%.2X\n\n", [buffer[0], buffer[1]]); data := buffer[0]; data := data shl 8; data := data or buffer[1]; data := data shr 3; temperature := LM92_RES * data; writeln("Temperature: %2.1f °C\n\n", [temperature]); close(fd);
end;
Non l'ho passato al compilatore.
Ci sono evidenti errori di sintassi per il pascal (esempio le stringhe ;) sono pigro :D )
Una volta capito come operare potresti fare una class TI2CDriver del tipo:
type
TWire = class(THandleStream)
protected
FI2CAddr: word;
FMaster : boolean;
FRead : Boolean;
public
constructor create(address: word; asMaster : Boolean);
...
end;
l'idea della classe non è nuova.
Dai un'occhiata https://github.com/arduino/Arduino/blob/master/libraries/Wire/Wire.h (https://github.com/arduino/Arduino/blob/master/libraries/Wire/Wire.h)
Un esempio (a caso, ma caso caso) per vedere come viene usata la libreria Wire
http://playground.arduino.cc/Main/WiiChuckClass (http://playground.arduino.cc/Main/WiiChuckClass)
hihi. Il prossimo passo è comandare i led da nunchack delle wii ;)
Nomore che ne dici? :p
Stilgar