Subversion Repositories svnkaklik

Rev

Rev 409 | Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
385 kakl 1
/*****************************************************************************/
2
/*
3
 *  Zmena I2C adresy
4
 *
5
 *      Copyright (C) 2007 KAKL, KAKLIK
6
 *
7
 *      This program is free software; you can redistribute it and/or modify
8
 *      it under the terms of the GNU General Public License as published by
9
 *      the Free Software Foundation; either version 2 of the License, or
10
 *      (at your option) any later version.
11
 *
12
 *      This program is distributed in the hope that it will be useful,
13
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *      GNU General Public License for more details.
16
 *
17
 */
18
/*****************************************************************************/
19
 
20
#include <iostream>
21
#include <getopt.h>
22
#include <errno.h>
23
#include <string.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <unistd.h>
27
#include "linux/i2c-dev.h"
28
#include "linux/i2c.h"
29
#include <sys/ioctl.h>
30
#include <sys/types.h>
31
#include <sys/stat.h>
32
#include <fcntl.h>
33
 
34
using namespace std;
35
 
36
 
37
#define BC_Addr	    0x0B
38
 
39
int file;
40
 
41
void I2C_addr (int Addr)
42
{
43
    if (ioctl(file, I2C_SLAVE, Addr) == -1)
44
    {
45
        fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
46
        exit(-5);
47
    }
48
}
49
 
50
 
51
unsigned int echo(int Addr)  // precte vzdalenost z US cidla
52
{
53
    char Buf[3];
54
 
55
    I2C_addr(Addr);
56
    Buf[0]=0x0;
57
    Buf[1]=0x51;
58
    write(file, Buf, 2);
59
    usleep(80000);
60
    read(file, Buf, 3);
61
    return (Buf[1]*256+Buf[2]);
62
}
63
 
64
int i2c_init()   // zinicializuje i2c
65
{
66
	file = open("/dev/i2c-0", O_RDWR);
67
	if (file < 0)
68
	{
69
		cerr << "Could not open /dev/i2c-0." << endl;
70
		return -1;
71
	}
72
	return 0;
73
}
74
 
75
int main(int argc, char *argv[], char *envp[])
76
{
77
    unsigned int OldAddress, NewAddress;
78
    char Buf[10];
79
 
80
	fprintf(stdout, "\n **** Change I2C Address **** \n \r");
81
 
82
    if (argc<2)
83
    {
84
        printf("Use:\n%s OldAddress NewAddress - for change address\nOR\n%s Address - for echo\n\n\r",argv[0],argv[0]);
85
        return 0;
86
    }
87
 
88
    i2c_init();
89
 
90
    sscanf(argv[1],"%x",&OldAddress);
91
 
92
    if (argc==2)
93
    {
94
        printf("Vzdalenost: %d\n", echo(OldAddress>>1));
95
        close(file);
96
        return 0;
97
    }
98
 
99
    sscanf(argv[2],"%x",&NewAddress);
100
 
101
    printf("Old: %x New: %x\n", OldAddress, NewAddress);
102
 
103
    printf("Vzdalenost: %d\n", echo(OldAddress>>1));
104
 
105
    I2C_addr(OldAddress>>1);
106
    Buf[0]=0x0;
107
    Buf[1]=0xA0;
108
    write(file, Buf, 2);
109
    I2C_addr(OldAddress>>1);
110
    Buf[0]=0x0;
111
    Buf[1]=0xAA;
112
    write(file, Buf, 2);
113
    I2C_addr(OldAddress>>1);
114
    Buf[0]=0x0;
115
    Buf[1]=0xA5;
116
    write(file, Buf, 2);
117
    I2C_addr(OldAddress>>1);
118
    Buf[0]=0x0;
119
    Buf[1]=NewAddress;
120
    write(file, Buf, 2);
121
 
122
 
123
    usleep(100000);
124
 
125
    printf("Vzdalenost: %d\n", echo(NewAddress>>1));
126
 
127
	close(file);
128
	return 0;
129
}