Subversion Repositories svnkaklik

Rev

Rev 385 | Blame | Last modification | View Log | Download

/*****************************************************************************/
/*
 *  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


int file;

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 **** Change I2C Address **** \n \r");

    if (argc<2)
    {
        printf("Use:\n%s OldAddress NewAddress - for change address\nOR\n%s Address - for echo\n\n\r",argv[0],argv[0]);
        return 0;
    }

    i2c_init();

    sscanf(argv[1],"%x",&OldAddress);

    if (argc==2)
    {
        printf("Vzdalenost: %d\n", echo(OldAddress>>1));
        close(file);
        return 0;
    }

    sscanf(argv[2],"%x",&NewAddress);

    printf("Old: %x New: %x\n", OldAddress, NewAddress);

    printf("Vzdalenost: %d\n", echo(OldAddress>>1));

    I2C_addr(OldAddress>>1);
    Buf[0]=0x0;
    Buf[1]=0xA0;
    write(file, Buf, 2);
    I2C_addr(OldAddress>>1);
    Buf[0]=0x0;
    Buf[1]=0xAA;
    write(file, Buf, 2);
    I2C_addr(OldAddress>>1);
    Buf[0]=0x0;
    Buf[1]=0xA5;
    write(file, Buf, 2);
    I2C_addr(OldAddress>>1);
    Buf[0]=0x0;
    Buf[1]=NewAddress;
    write(file, Buf, 2);


    usleep(100000);

    printf("Vzdalenost: %d\n", echo(NewAddress>>1));

        close(file);
        return 0;
}