Subversion Repositories svnkaklik

Rev

Rev 409 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
385 kakl 1
/*****************************************************************************/
2
/*
3
 *  Zmena I2C adresy
4
 *
5
 *      Copyright (C) 2007 KAKL
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
#define US3_Addr	0x70    // 0xE0 in fact; Sonar na doprovod
39
#define CMPS_Addr   0x60    // 0xC0
40
#define M1	0x50 // 0xA0 in fact
41
#define M2	0x51 // 0xA2 in fact
42
 
43
 
44
int file;
45
 
46
void I2C_addr (int Addr)
47
{
48
    if (ioctl(file, I2C_SLAVE, Addr) == -1)
49
    {
50
        fprintf(stderr, "Failed to set address to 0x%02x.\n", Addr);
51
        exit(-5);
52
    }
53
}
54
 
55
 
56
unsigned int echo(int Addr)  // precte vzdalenost z US cidla
57
{
58
    char Buf[3];
59
 
60
    I2C_addr(Addr);
61
    Buf[0]=0x0;
62
    Buf[1]=0x51;
63
    write(file, Buf, 2);
64
    usleep(80000);
65
    read(file, Buf, 3);
66
    return (Buf[1]*256+Buf[2]);
67
}
68
 
69
unsigned char read_azimut_mag()  // precte azimut z kompasu
70
{
71
    char Buf[3];      // promena pro manipulaci s i2c
72
 
73
    I2C_addr(CMPS_Addr);
74
    Buf[0]=1;
75
    write(file,Buf,1);
76
    read(file, Buf,1);
77
    return Buf[0];
78
}
79
 
80
void calib()  // kalibrace kompasu
81
{
82
    char Buf[3];      // promena pro manipulaci s i2c
83
 
84
    I2C_addr(CMPS_Addr);
85
    Buf[0]=15;
86
    Buf[1]=0xFF;
87
    write(file,Buf,2);
88
}
89
 
90
int i2c_init()   // zinicializuje i2c
91
{
92
	file = open("/dev/i2c-0", O_RDWR);
93
	if (file < 0)
94
	{
95
		cerr << "Could not open /dev/i2c-0." << endl;
96
		return -1;
97
	}
98
	return 0;
99
}
100
 
101
int main(int argc, char *argv[], char *envp[])
102
{
103
    unsigned int OldAddress, NewAddress;
104
    char Buf[10];
105
 
106
	fprintf(stdout, "\n **** Change I2C Address **** \n \r");
107
 
108
    if (argc<2)
109
    {
110
        printf("Use:\n%s OldAddress NewAddress - for change address\nOR\n%s Address - for echo\n\n\r",argv[0],argv[0]);
111
        return 0;
112
    }
113
 
114
    i2c_init();
115
 
116
    sscanf(argv[1],"%x",&OldAddress);
117
 
118
    if (argc==2)
119
    {
120
        printf("Vzdalenost: %d\n", echo(OldAddress>>1));
121
        close(file);
122
        return 0;
123
    }
124
 
125
    sscanf(argv[2],"%x",&NewAddress);
126
 
127
    printf("Old: %x New: %x\n", OldAddress, NewAddress);
128
 
129
    printf("Vzdalenost: %d\n", echo(OldAddress>>1));
130
 
131
    I2C_addr(OldAddress>>1);
132
    Buf[0]=0x0;
133
    Buf[1]=0xA0;
134
    write(file, Buf, 2);
135
    I2C_addr(OldAddress>>1);
136
    Buf[0]=0x0;
137
    Buf[1]=0xAA;
138
    write(file, Buf, 2);
139
    I2C_addr(OldAddress>>1);
140
    Buf[0]=0x0;
141
    Buf[1]=0xA5;
142
    write(file, Buf, 2);
143
    I2C_addr(OldAddress>>1);
144
    Buf[0]=0x0;
145
    Buf[1]=NewAddress;
146
    write(file, Buf, 2);
147
 
148
 
149
    usleep(100000);
150
 
151
    printf("Vzdalenost: %d\n", echo(NewAddress>>1));
152
 
153
	close(file);
154
	return 0;
155
}