0,0 → 1,176 |
/*****************************************************************************/ |
/* |
* Board.cpp - communication with NGW100 Board Controller and I2C |
* |
* Copyright (C) 2007 Karel Hojdar, KAKL |
* |
* This program is free software; you can redistribute it and/or modify |
* it under the terms of the GNU General Public License as published by |
* the Free Software Foundation; either version 2 of the License, or |
* (at your option) any later version. |
* |
* This program is distributed in the hope that it will be useful, |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* GNU General Public License for more details. |
* |
* |
* |
* Revision history |
* 15.06.2007 1.0 Undertaken from KAHO |
* 09.09.2007 2.0 I2C US Ranger |
*/ |
/*****************************************************************************/ |
|
#include <iostream> |
|
#include <getopt.h> |
#include <errno.h> |
#include <string.h> |
#include <pthread.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <unistd.h> |
#include "linux/i2c-dev.h" |
#include "linux/i2c.h" |
#include <sys/ioctl.h> |
#include <sys/types.h> |
#include <sys/stat.h> |
#include <fcntl.h> |
|
|
using namespace std; |
|
#define CMPS03_SOFTWARE_REVISION 0x0 |
#define SRF02_SOFTWARE_REVISION 0x0 |
|
#define I2C_SLAVE 0x0703 /* Change slave address */ |
#define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/ |
#define I2C_SLAVE_FORCE 0x0706 /* Change slave address */ |
/* Attn.: Slave address is 7 or 10 bits */ |
/* This changes the address, even if it */ |
/* is already taken! */ |
|
#define BC_Addr 0x0B |
#define US_Addr 0x70 // 0xE0 in fact |
|
char vystup[30]; |
char flag; |
pthread_t thread_id; |
FILE *pRouraO,*pRouraI; |
unsigned int vzdalenost; |
|
void *print_tele(void *unused); |
|
void DoExit(int ex) |
{ |
exit(ex); |
} |
|
unsigned char xfunc[5] = {0x99, 0x9A, 0x9B, 0x9E, 0x8D}; |
unsigned char xlen[5] = {8, 6, 1, 15, 2}; |
|
int main(int argc, char *argv[], char *envp[]) |
{ |
int Len; |
int i2cbus = 0; |
char filename[20],Buf[64]; |
int file; |
|
|
fprintf(stdout, "Ble................"); |
|
sprintf(filename, "/dev/i2c-%d", i2cbus); |
file = open(filename, O_RDWR); |
|
if (file < 0) |
{ |
cerr << "Could not open /dev/i2c-0." << endl; |
return -1; |
} |
|
if (ioctl(file, I2C_SLAVE, BC_Addr) == -1) |
{ |
fprintf(stderr, "Failed to set address to 0x%02x.\n", BC_Addr); |
DoExit(-2); |
} |
|
Buf[0] = xfunc[0]; |
if ( write(file, Buf, 1) != 1) |
{ |
fprintf(stderr, "Failed to write byte to address to 0x%02x, errno %i.\n", BC_Addr, errno); |
DoExit(-3); |
} |
|
if (read(file, Buf, 1) != 1) |
{ |
fprintf(stderr, "Failed to read response length, errno %i.\n", errno); |
DoExit(-4); |
} |
|
Len = Buf[0]; |
if (read(file, Buf, Len) != Len) |
{ |
fprintf(stderr, "Failed to read response, errno %i.\n", errno); |
DoExit(-5); |
} |
|
|
Buf[Len] = 0x00; |
fprintf(stdout, "Board ID is %s.\n", Buf); |
|
//!------ US --------------------------------------------------------------------------- |
|
if (ioctl(file, I2C_SLAVE, US_Addr) == -1) |
{ |
fprintf(stderr, "Failed to set address to 0x%02x.\n", US_Addr); |
DoExit(-6); |
} |
|
pthread_create(&thread_id, NULL, print_tele, NULL); |
flag = 0; |
|
while(true) |
{ |
Buf[0]=0x0; |
Buf[1]=0x51; |
write(file, Buf, 2); |
usleep(80000); |
read(file, Buf, 3); |
if (flag==0) |
vzdalenost=(Buf[1]*256+Buf[2]); |
// printf("Vzdalenost: %u cm\n",ble); |
|
/* |
if (0==flag) |
{ |
sprintf(vystup,"Vzdalenost: %u cm\n",vzdalenost); |
}; |
*/ |
usleep(300000); |
}; |
close(file); |
pthread_join(thread_id, NULL); |
fclose(pRouraO); |
|
return 0; |
} |
|
void *print_tele(void *unused) |
{ |
flag=0; |
|
while(1) |
{ |
char pom; |
|
pRouraI = fopen("/home/ble/pipe","r"); |
pom=fgetc(pRouraI); |
fclose(pRouraI); |
flag=1; |
pRouraO = fopen("/home/ble/pipe","w"); |
fprintf(pRouraO,"Vzdalenost: %u cm\n",vzdalenost); |
fclose(pRouraO); |
flag=0; |
} |
} |