-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial_read.cpp
47 lines (44 loc) · 1.88 KB
/
serial_read.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "serial_read.h"
#include <stdio.h>
UART *pUart=0; //The UART starts out as 0.
/*Open the serial device for later reading*/
int SerialInit()
{
int file;
//Open the device file
if((file = open(UART_DEVICE_NAME, O_RDWR | O_SYNC)) == -1)
{
perror("Failed to open device");
return 0; /*File error! Serial might be open somehow.*/
}
//The next line will put the serial data in memory. UART is a nicely formatted Struct to make it easy to get specific parts of the serial data stream
pUart = (UART*)mmap(0, sizeof(UART), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file, 0);
if (pUart == MAP_FAILED)
{
perror("Failed to map device");
return 0; /*Memory Mapping error.*/
}
return 1;
}
//In ReadSerial, we take in a port number and the serial stream in memory, pointed to by pUart.
//We use the nice format of the struct to return the raw value of the sensor at the requested port.
//For more information on the UART struct, look at defines.h
int ReadSerial(char port)
{
int testy=1;
if(pUart == 0) testy = SerialInit();
if(testy==0) return 0;
unsigned char LSB = (unsigned char)(pUart->Raw[(port-1)][pUart->Actual[(port-1)]][0]); //The 8 Least-Significant bits of our return value are stored here.
unsigned char MSB = (unsigned char)(pUart->Raw[(port-1)][pUart->Actual[(port-1)]][1]) & 0x7; //The three most-significant bytes of the return value are the 3 LSB of the next byte of memory.
short rtn = MSB << 8; //Assuming short is 16 bit, rtn = 0000 0MMM 0000 0000 where MMM are the values in MSB
rtn |= LSB; //rtn= 0000 0MMM LLLL LLLL
return rtn;
}
int ReadLight(char port)
{
return ReadSerial(port) & 0xFF; //Light sensor doesn't use MSB from Serial.
}
void SerialExit()
{
munmap(pUart,sizeof(UART)); //Finished with the serial ports so we close them up
}