Subversion Repositories svnkaklik

Compare Revisions

Ignore whitespace Rev 384 → Rev 354

/programy/Atmel32_C/compass/compass.c
33,8 → 33,7
* Linux i2c does not include this bit in this address, so the actual
* address is 0xC0 shifted down, 0x60.
*/
#define CMPS03_ADDR 0x60
#define I2C_SLAVE 0x0B /* Change slave address
#define CMPS03_ADDR 0x60
 
/* The important registers on the compass. Internal/test registers omitted. */
#define CMPS03_SOFTWARE_REVISION 0x0
46,7 → 45,7
int main(int argc, char *argv[]) {
char *end;
int res,file;
int error;
int e1;
char filename[20] ;
long funcs;
53,54 → 52,45
int heading_byte, heading_word_h, heading_word_l;
int bearing_long, bearing_degrees;
sprintf(filename,"/dev/i2c-0");
file = open(filename,O_RDWR);
if (errno != 0)
{
switch (errno)
{
case EACCES:
fprintf(stderr,"Run as root? \n");
break;
default:
fprintf(stderr,"Error: Could not open file '%s' : %s \n", filename, strerror(errno));
break;
sprintf(filename,"/dev/i2c-%d");
if ((file = open(filename,O_RDWR)) < 0) {
e1 = errno;
if (e1 != ENOENT) {
fprintf(stderr,"Error: Could not open file '%s' : %sn",
filename,strerror(e1));
if(e1 == EACCES)
fprintf(stderr,"Run as root?n");
}
}
if (ioctl(file,I2C_SLAVE,CMPS03_ADDR) < 0) {
switch (errno)
{
case EBUSY:
printf("device is busy \n");
break;
 
default:
printf("Got error: %s \n", strerror(errno));
exit(0);
}
if (errno == EBUSY) {
printf("device is busyn");
}
printf("Got error: %d\n", errno);
exit(0);
}
/* Get software revision number */
res = read(file, CMPS03_SOFTWARE_REVISION,1);
res = i2c_smbus_read_byte_data(file, CMPS03_SOFTWARE_REVISION);
if (res < 0) {
printf("Cannot read software revision level \n");
printf("Cannot read software revision leveln");
} else {
printf("Software revision level: %02x \n", res);
printf("Software revision level: %02xn", res);
}
/* Loop and read from the compass. */
while (1) {
/* The heading byte is 0-255 for the 360 degrees. */
heading_byte = read(file, CMPS03_BEARING_BYTE,1);
if (heading_byte < 0) { printf("Error reading from compass. \n"); exit(1);}
heading_byte = i2c_smbus_read_byte_data(file, CMPS03_BEARING_BYTE);
if (heading_byte < 0) { printf("Error reading from compass."); exit(1);}
/* The high resolution heading is given in two registers, and is 10 * the
* heading in degrees, ie 359.9 degrees reads as 3599. */
heading_word_h = read(file, CMPS03_BEARING_WORD_HIGH,1);
if (heading_word_h < 0) { printf("Error reading from compass. \n"); exit(1);}
heading_word_l = read(file, CMPS03_BEARING_WORD_LOW,1);
if (heading_word_l < 0) { printf("Error reading from compass. \n"); exit(1);}
heading_word_h = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_HIGH);
if (heading_word_h < 0) { printf("Error reading from compass."); exit(1);}
heading_word_l = i2c_smbus_read_byte_data(file, CMPS03_BEARING_WORD_LOW);
if (heading_word_l < 0) { printf("Error reading from compass."); exit(1);}
/* Combine the two bytes, and get the heading in degrees. */
bearing_long = heading_word_h * 256 + heading_word_l;
/programy/Atmel32_C/compass/compass.cpp
0,0 → 1,176
/*****************************************************************************/
/*
* compass.cpp - communication with digital compass module
*
* Copyright (C) 2007 Jakub Kakona
*
* 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
* 10.08.2007 1.0 Initial release
*/
/*****************************************************************************/
 
#include <iostream>
 
#include <getopt.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "linux/i2c-dev.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
 
using namespace std;
 
static char* version = "CompassControlloler, version 0.1";
 
static char* help = "Usage: BoardController [OPTION]\n -A\t\tshow Azimuth\n -M\t\tshow model\n -R\t\tshow revision\n -S\t\tshow serial no.\n -T\t\tshow temperature\n\n";
 
#define I2C_SLAVE 0x0703 /* Change slave address */
#define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/
 
#define BC_Addr 0xC0
 
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[])
{
char *progname;
int c, func = 0, Len;
int i2cbus = 0;
char filename[20], Buf[64];
int file;
 
progname = strrchr(argv[0], '/');
progname = progname ? progname + 1 : argv[0];
 
while ((c = getopt (argc, argv, "IiMmRrSsTth")) != -1)
switch (c)
{
case 'I':
case 'i':
func = 0;
break;
 
case 'M':
case 'm':
func = 1;
break;
 
case 'R':
case 'r':
func = 2;
break;
 
case 'S':
case 's':
func = 3;
break;
 
case 'T':
case 't':
func = 4;
break;
 
case 'h':
printf ("%s\n%s", version, help);
return 1;
 
case '?':
printf ("Unknown option `-%c', try %s -h.\n", optopt,progname);
return 1;
}
 
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);
}
 
int Loops = 0;
 
do
{
Buf[0] = xfunc[func];
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);
}
 
Loops++;
} while (Len != xlen[func]);
 
if (Loops > 1)
fprintf(stderr, "After %i attempts got: \n", Loops);
 
switch (func)
{
case 0:
Buf[Len] = 0x00;
fprintf(stdout, "Board ID is %s.\n", Buf);
break;
case 1:
Buf[Len] = 0x00;
fprintf(stdout, "Model of the board is %s.\n", Buf);
break;
case 2:
fprintf(stdout, "Revision of the board is 0x%02X.\n", Buf[0]);
break;
case 3:
Buf[Len] = 0x00;
fprintf(stdout, "Serial number of the board is %s.\n", Buf);
break;
case 4:
fprintf(stdout, "Temperature is %i or %i.\n", (Buf[0] << 8) + Buf[1], (Buf[1] << 8) + Buf[0]);
break;
 
}
 
close(file);
 
return 0;
}