Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
364 kakl 1
/*****************************************************************************/
2
/*
3
 *  Board.cpp - communication with NGW100 Board Controller and I2C
4
 *
5
 *      Copyright (C) 2007  Karel Hojdar, 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
 *  Revision history
20
 *    15.06.2007   1.0   Undertaken from KAHO
21
 *    09.09.2007   2.0   I2C US Ranger
22
 */
23
/*****************************************************************************/
24
 
25
#include <iostream>
26
 
27
#include <getopt.h>
28
#include <errno.h>
29
#include <string.h>
30
#include <pthread.h>
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <unistd.h>
34
#include "linux/i2c-dev.h"
35
#include "linux/i2c.h"
36
#include <sys/ioctl.h>
37
#include <sys/types.h>
38
#include <sys/stat.h>
39
#include <fcntl.h>
40
 
41
 
42
using namespace std;
43
 
44
#define CMPS03_SOFTWARE_REVISION 0x0
45
#define SRF02_SOFTWARE_REVISION 0x0
46
 
47
#define I2C_SLAVE	0x0703	/* Change slave address			*/
48
#define I2C_RDWR	0x0707	/* Combined R/W transfer (one stop only)*/
49
#define I2C_SLAVE_FORCE	0x0706	/* Change slave address			*/
50
				/* Attn.: Slave address is 7 or 10 bits */
51
				/* This changes the address, even if it */
52
				/* is already taken!	*/
53
 
54
#define BC_Addr	0x0B
55
#define US_Addr	0x70 // 0xE0 in fact
56
 
57
char vystup[30];
58
char flag;
59
pthread_t thread_id;
60
FILE *pRouraO,*pRouraI;
61
unsigned int vzdalenost;
62
 
63
void *print_tele(void *unused);
64
 
65
void DoExit(int ex)
66
{
67
	exit(ex);
68
}
69
 
70
unsigned char xfunc[5] = {0x99, 0x9A, 0x9B, 0x9E, 0x8D};
71
unsigned char xlen[5] = {8, 6, 1, 15, 2};
72
 
73
int main(int argc, char *argv[], char *envp[])
74
{
75
	int Len;
76
	int	i2cbus = 0;
77
	char filename[20],Buf[64];
78
	int file;
79
 
80
 
81
	fprintf(stdout, "Ble................");
82
 
83
	sprintf(filename, "/dev/i2c-%d", i2cbus);
84
	file = open(filename, O_RDWR);
85
 
86
	if (file < 0)
87
	{
88
		cerr << "Could not open /dev/i2c-0." << endl;
89
		return -1;
90
	}
91
 
92
	if (ioctl(file, I2C_SLAVE, BC_Addr) == -1)
93
	{
94
		fprintf(stderr, "Failed to set address to 0x%02x.\n", BC_Addr);
95
		DoExit(-2);
96
	}
97
 
98
    Buf[0] = xfunc[0];
99
    if ( write(file, Buf, 1) != 1)
100
    {
101
        fprintf(stderr, "Failed to write byte to address to 0x%02x, errno %i.\n", BC_Addr, errno);
102
        DoExit(-3);
103
    }
104
 
105
    if (read(file, Buf, 1) != 1)
106
    {
107
        fprintf(stderr, "Failed to read response length, errno %i.\n", errno);
108
        DoExit(-4);
109
    }
110
 
111
    Len = Buf[0];
112
    if (read(file, Buf, Len) != Len)
113
    {
114
        fprintf(stderr, "Failed to read response, errno %i.\n", errno);
115
        DoExit(-5);
116
    }
117
 
118
 
119
    Buf[Len] = 0x00;
120
    fprintf(stdout, "Board ID is %s.\n", Buf);
121
 
122
//!------ US ---------------------------------------------------------------------------
123
 
124
    if (ioctl(file, I2C_SLAVE, US_Addr) == -1)
125
    {
126
        fprintf(stderr, "Failed to set address to 0x%02x.\n", US_Addr);
127
        DoExit(-6);
128
    }
129
 
130
    pthread_create(&thread_id, NULL, print_tele, NULL);
131
    flag = 0;
132
 
133
    while(true)
134
    {
135
        Buf[0]=0x0;
136
        Buf[1]=0x51;
137
        write(file, Buf, 2);
138
        usleep(80000);
139
        read(file, Buf, 3);
140
        if (flag==0)
141
          vzdalenost=(Buf[1]*256+Buf[2]);
142
//        printf("Vzdalenost: %u cm\n",ble);
143
 
144
 /*
145
        if (0==flag)
146
        {
147
            sprintf(vystup,"Vzdalenost: %u cm\n",vzdalenost);
148
        };
149
*/
150
        usleep(300000);
151
    };
152
	close(file);
153
    pthread_join(thread_id, NULL);
154
    fclose(pRouraO);
155
 
156
	return 0;
157
}
158
 
159
void *print_tele(void *unused)
160
{
161
    flag=0;
162
 
163
    while(1)
164
    {
165
        char pom;
166
 
167
        pRouraI = fopen("/home/ble/pipe","r");
168
        pom=fgetc(pRouraI);
169
        fclose(pRouraI);
170
        flag=1;
171
        pRouraO = fopen("/home/ble/pipe","w");
172
        fprintf(pRouraO,"Vzdalenost: %u cm\n",vzdalenost);
173
        fclose(pRouraO);
174
        flag=0;
175
    }
176
}