Subversion Repositories svnkaklik

Rev

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

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