Subversion Repositories svnkaklik

Compare Revisions

Ignore whitespace Rev 372 → Rev 373

/roboti/Robotour/SW/i2c/main.cpp
0,0 → 1,149
/*****************************************************************************/
/*
* Zmena I2C adresy
*
* Copyright (C) 2007 KAKL
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
/*****************************************************************************/
 
#include <iostream>
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "linux/i2c-dev.h"
#include "linux/i2c.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
using namespace std;
 
 
#define BC_Addr 0x0B
#define US3_Addr 0x70 // 0xE0 in fact; Sonar na doprovod
#define CMPS_Addr 0x60 // 0xC0
#define M1 0x50 // 0xA0 in fact
#define M2 0x51 // 0xA2 in fact
 
 
char vystup[50];
pthread_t thread_1, thread_2, thread_3;
FILE *pRouraO,*pRouraI;
unsigned int vzdalenost;
char command,ble;
int param;
int file;
double nord, east;
int done; // vlajka, ze se neco udelalo
int last_cross; // posledni krizovatka
 
 
void I2C_addr (int Addr)
{
if (ioctl(file, I2C_SLAVE, Addr) == -1)
{
fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
exit(-5);
}
}
 
 
unsigned int echo(int Addr) // precte vzdalenost z US cidla
{
char Buf[3];
 
I2C_addr(Addr);
Buf[0]=0x0;
Buf[1]=0x51;
write(file, Buf, 2);
usleep(80000);
read(file, Buf, 3);
return (Buf[1]*256+Buf[2]);
}
 
unsigned char read_azimut_mag() // precte azimut z kompasu
{
char Buf[3]; // promena pro manipulaci s i2c
 
I2C_addr(CMPS_Addr);
Buf[0]=1;
write(file,Buf,1);
read(file, Buf,1);
return Buf[0];
}
 
void calib() // kalibrace kompasu
{
char Buf[3]; // promena pro manipulaci s i2c
 
I2C_addr(CMPS_Addr);
Buf[0]=15;
Buf[1]=0xFF;
write(file,Buf,2);
}
 
int i2c_init() // zinicializuje i2c
{
file = open("/dev/i2c-0", O_RDWR);
if (file < 0)
{
cerr << "Could not open /dev/i2c-0." << endl;
return -1;
}
return 0;
}
 
int main(int argc, char *argv[], char *envp[])
{
unsigned int OldAddress, NewAddress;
char Buf[10];
 
fprintf(stdout, "\n **** Changing I2C Address **** \n \r");
 
i2c_init();
 
if ( argc < 3 )
{
printf("Use:\n%s OldAddress NewAddress\n",argv[0]);
return 0;
}
 
sscanf(argv[1],"%x",&OldAddress);
sscanf(argv[2],"%x",&NewAddress);
 
printf("Old: %x New: %x\n", OldAddress, NewAddress);
 
echo(OldAddress);
printf("Vzdalenost: %d\n", echo(NewAddress));
 
I2C_addr(OldAddress);
Buf[0]=0x0;
Buf[1]=0xA0;
Buf[2]=0xAA;
Buf[3]=0xA5;
Buf[4]=(unsigned char)NewAddress;
write(file, Buf, 5);
 
usleep(100000);
 
echo(NewAddress);
printf("Vzdalenost: %d\n", echo(NewAddress));
 
close(file);
return 0;
}