Subversion Repositories svnkaklik

Rev

Rev 373 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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