Subversion Repositories svnkaklik

Rev

Rev 409 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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