Subversion Repositories svnkaklik

Rev

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