Subversion Repositories svnkaklik

Rev

Rev 364 | Go to most recent revision | Details | Compare with Previous | 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
 
365 kakl 54
#define BC_Addr	    0x0B
55
#define US_Addr	    0x70 // 0xE0 in fact
56
#define PIC_Addr	0x50 // 0xA0 in fact
364 kakl 57
 
58
char vystup[30];
59
pthread_t thread_id;
60
FILE *pRouraO,*pRouraI;
61
unsigned int vzdalenost;
365 kakl 62
char command,ble;
364 kakl 63
 
64
void *print_tele(void *unused);
65
 
66
void DoExit(int ex)
67
{
68
	exit(ex);
69
}
70
 
71
unsigned char xfunc[5] = {0x99, 0x9A, 0x9B, 0x9E, 0x8D};
72
unsigned char xlen[5] = {8, 6, 1, 15, 2};
73
 
74
int main(int argc, char *argv[], char *envp[])
75
{
76
	int Len;
77
	int	i2cbus = 0;
78
	char filename[20],Buf[64];
79
	int file;
80
 
81
 
82
	fprintf(stdout, "Ble................");
83
 
84
	sprintf(filename, "/dev/i2c-%d", i2cbus);
85
	file = open(filename, O_RDWR);
86
 
87
	if (file < 0)
88
	{
89
		cerr << "Could not open /dev/i2c-0." << endl;
90
		return -1;
91
	}
92
 
93
	if (ioctl(file, I2C_SLAVE, BC_Addr) == -1)
94
	{
95
		fprintf(stderr, "Failed to set address to 0x%02x.\n", BC_Addr);
96
		DoExit(-2);
97
	}
98
 
99
    Buf[0] = xfunc[0];
100
    if ( write(file, Buf, 1) != 1)
101
    {
102
        fprintf(stderr, "Failed to write byte to address to 0x%02x, errno %i.\n", BC_Addr, errno);
103
        DoExit(-3);
104
    }
105
 
106
    if (read(file, Buf, 1) != 1)
107
    {
108
        fprintf(stderr, "Failed to read response length, errno %i.\n", errno);
109
        DoExit(-4);
110
    }
111
 
112
    Len = Buf[0];
113
    if (read(file, Buf, Len) != Len)
114
    {
115
        fprintf(stderr, "Failed to read response, errno %i.\n", errno);
116
        DoExit(-5);
117
    }
118
 
119
 
120
    Buf[Len] = 0x00;
121
    fprintf(stdout, "Board ID is %s.\n", Buf);
122
 
123
//!------ US ---------------------------------------------------------------------------
124
 
125
    pthread_create(&thread_id, NULL, print_tele, NULL);
126
 
127
    while(true)
128
    {
365 kakl 129
        if (ioctl(file, I2C_SLAVE, US_Addr) == -1)
130
        {
131
            fprintf(stderr, "Failed to set address to 0x%02x.\n", US_Addr);
132
            DoExit(-6);
133
        }
364 kakl 134
        Buf[0]=0x0;
135
        Buf[1]=0x51;
136
        write(file, Buf, 2);
137
        usleep(80000);
138
        read(file, Buf, 3);
365 kakl 139
        vzdalenost=(Buf[1]*256+Buf[2]);
140
        usleep(300000);
364 kakl 141
 
365 kakl 142
        if (ioctl(file, I2C_SLAVE, PIC_Addr) == -1)
364 kakl 143
        {
365 kakl 144
            fprintf(stderr, "Failed to set address to 0x%02x.\n", US_Addr);
145
            DoExit(-6);
146
        }
147
        Buf[0]=command;
148
        write(file, Buf, 1);
149
        read(file, Buf, 1);
150
        ble=Buf[0];
364 kakl 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
    while(1)
162
    {
163
        pRouraI = fopen("/home/ble/pipe","r");
365 kakl 164
        command=fgetc(pRouraI);
364 kakl 165
        fclose(pRouraI);
166
        pRouraO = fopen("/home/ble/pipe","w");
365 kakl 167
        fprintf(pRouraO,"Vzdalenost: %u cm Command: %x\n",vzdalenost,ble);
364 kakl 168
        fclose(pRouraO);
169
    }
170
}