Skip to content

Instantly share code, notes, and snippets.

@acassis
Created October 3, 2015 13:47
Show Gist options
  • Save acassis/085c80c4860fa890d7b9 to your computer and use it in GitHub Desktop.
Save acassis/085c80c4860fa890d7b9 to your computer and use it in GitHub Desktop.
Oven simulator to integrate with Andy Brown Android App
/****************************************************************************
* Included Files
****************************************************************************/
#include <termios.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <sys/types.h>
#define BAUDRATE B9600
#define FALSE 0
#define TRUE 1
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
volatile int STOP=FALSE;
void signal_handler_IO (int status);
int wait_flag=TRUE;
char devicename[80] = "/dev/ttyUSB0", ch;
int status;
int newdata = 0;
#define BUFFERED_IO
#define SIZE 4
enum oven_cmds
{
NONE = 0,
READ_TEMPERATURE = 1,
SET_DUTY_CYCLE = 2,
SET_SENSOR_OFFSET = 3,
SET_LCD_BACKLIGHT = 4,
SET_LCD_CONTRAST = 5,
READ_SETTINGS = 6
};
enum acknowledge{
ACK = 0,
NACK = 1
};
/****************************************************************************
* Private Data
****************************************************************************/
static int count = 0;
/****************************************************************************
* Public Functions
****************************************************************************/
int validcmd(uint8_t cmd)
{
if (cmd >= NONE && cmd <= READ_SETTINGS)
return 1;
else
return 0;
}
/****************************************************************************
* serialrx_main
****************************************************************************/
int main(int argc, char *argv[])
{
int fd, res, i, rcvd;
struct termios newtio;
struct sigaction saio;
char buf[255];
uint8_t cmd_data[12], cmd_pos = 0, temp = 20;
int new_cmd = 0;
/* open the device in non-blocking way (read will return immediately) */
fd = open(devicename, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0)
{
perror(devicename);
exit(1);
}
/* install the serial handler before making the device asynchronous */
saio.sa_handler = signal_handler_IO;
sigemptyset(&saio.sa_mask);
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
/* allow the process to receive SIGIO */
fcntl(fd, F_SETOWN, getpid());
/* make the file descriptor asynchronous */
fcntl(fd, F_SETFL, FASYNC);
/* set new port settings for canonical input processing */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag &= ~OPOST; /* Raw mode TX */
newtio.c_lflag = 0;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
while(STOP == FALSE)
{
/* We received some data from serial*/
if (wait_flag == FALSE)
{
rcvd = read(fd, buf, SIZE);
for (i = 0; i < rcvd; i++)
{
/* Show received byte */
printf("0x%02X\n", (unsigned char) buf[i]);
cmd_data[cmd_pos] = buf[i];
if ( (cmd_pos == 0 && cmd_data[0] != 0xAA)
|| (cmd_pos == 1 && cmd_data[1] != 0x55)
|| (cmd_pos == 2 && !validcmd(cmd_data[2])) )
cmd_pos = 0;
else
cmd_pos++;
/* We don't have command more than 9 bytes */
if (cmd_pos > 9)
cmd_pos = 0;
/* Verify if we got a valid command */
if ( (cmd_pos == 5) && ( cmd_data[2] == READ_TEMPERATURE
|| cmd_data[2] == READ_SETTINGS ) )
{
cmd_pos = 0;
new_cmd = 1;
break;
}
if ( (cmd_pos == 6) && (cmd_data[2] == SET_DUTY_CYCLE
|| cmd_data[2] == SET_SENSOR_OFFSET
|| cmd_data[2] == SET_LCD_BACKLIGHT
|| cmd_data[2] == SET_LCD_CONTRAST) )
{
cmd_pos = 0;
new_cmd = 1;
break;
}
}
if (new_cmd)
{
printf("New command received = %d!\n", cmd_data[2]);
if (cmd_data[2] == READ_SETTINGS)
{
cmd_data[5] = ACK;
cmd_data[6] = 1;
cmd_data[7] = 50;
cmd_data[8] = 50;
write(fd, cmd_data, 9);
syncfs(fd);
new_cmd = 0;
cmd_pos = 0;
}
if (cmd_data[2] == READ_TEMPERATURE)
{
cmd_data[5] = ACK;
cmd_data[6] = temp;
cmd_data[7] = 0;
cmd_data[8] = 1;
write(fd, cmd_data, 9);
syncfs(fd);
new_cmd = 0;
cmd_pos = 0;
}
if (cmd_data[2] == SET_DUTY_CYCLE)
{
/* If duty cycle lesser than 25% temperature decreases */
if (cmd_data[5] < 25)
{
if (temp > 20) temp--;
}
else
{
if (temp < 250) temp++;
}
cmd_data[5] = ACK;
cmd_data[6] = temp;
cmd_data[7] = 0;
cmd_data[8] = 1;
write(fd, cmd_data, 9);
syncfs(fd);
new_cmd = 0;
cmd_pos = 0;
}
}
fflush(stdout);
wait_flag = TRUE; /* wait for new input */
}
}
return 0;
}
//
/***************************************************************************
* signal handler. sets wait_flag to FALSE, to indicate above loop that *
* characters have been received. *
***************************************************************************/
//
void signal_handler_IO (int status)
{
//printf("received SIGIO signal.\n");
wait_flag = FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment