Rev 365 | Go to most recent revision | Blame | Last modification | View Log | Download
/*****************************************************************************/
/*
* 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 BC_Addr 0x0B
#define US_Addr 0x70 // 0xE0 in fact
#define PIC_Addr 0x50 // 0xA0 in fact
char vystup[30];
pthread_t thread_id;
FILE *pRouraO,*pRouraI;
unsigned int vzdalenost;
char command,ble;
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 ---------------------------------------------------------------------------
pthread_create(&thread_id, NULL, print_tele, NULL);
while(true)
{
if (ioctl(file, I2C_SLAVE, US_Addr) == -1)
{
fprintf(stderr, "Failed to set address to 0x%02x.\n", US_Addr);
DoExit(-6);
}
Buf[0]=0x0;
Buf[1]=0x51;
write(file, Buf, 2);
usleep(80000);
read(file, Buf, 3);
vzdalenost=(Buf[1]*256+Buf[2]);
usleep(300000);
if (ioctl(file, I2C_SLAVE, PIC_Addr) == -1)
{
fprintf(stderr, "Failed to set address to 0x%02x.\n", US_Addr);
DoExit(-6);
}
Buf[0]=command;
write(file, Buf, 1);
read(file, Buf, 1);
ble=Buf[0];
//!--------
printf("Vzdalenost: %u cm Command: %x\n",vzdalenost,ble);
};
close(file);
pthread_join(thread_id, NULL);
fclose(pRouraO);
return 0;
}
void *print_tele(void *unused)
{
while(1)
{
pRouraI = fopen("/home/ble/pipe","r");
command=fgetc(pRouraI);
fclose(pRouraI);
pRouraO = fopen("/home/ble/pipe","w");
fprintf(pRouraO,"Vzdalenost: %u cm Command: %x\n",vzdalenost,ble);
fclose(pRouraO);
}
}