Diff of /main.c [000000] .. [a1785f] Maximize Restore

  Switch to side-by-side view

--- a
+++ b/main.c
@@ -0,0 +1,195 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <time.h>
+#include <string.h>
+//#include <libconfig.h>
+//#include <shout/shout.h>
+
+#include "rs232.h"
+#include "cp_config.h"
+#include "cp_group.h"
+
+#define COM_PORT 0
+#define BAUD_RATE 921600
+#define BUFFER_LEN 4096
+#define SLEEP_TIME 5000
+
+#define CMD_CTRL  0x10
+#define CMD_ONAIR 0x20
+#define CMD_ONAIRBLINK 0x30
+#define CMD_SET_QUALITY 0x40
+#define CMD_SET_BITRATE 0x50 //Bitrate = 32 + ARG * 16
+#define CMD_STATUSLED_RIGHT 0x60
+#define CMD_STATUSLED_LEFT 0x70
+#define CMD_SAMPLERATE 0x80
+
+
+#define ARG_CTRL_STARTSTREAM 0x0
+#define ARG_CTRL_VUONLY 0x1
+#define ARG_CTRL_STOPSTREAM 0x2
+#define ARG_CTRL_STOPVUONLY 0x3
+
+enum MAIN_STM {
+	IDLE,
+	START_VUMETER,
+	VUMETER,
+	START_STREAMING,
+	STREAMING,
+	END_STREAMING,
+	ERROR 
+};
+
+unsigned char buf[BUFFER_LEN];
+unsigned char runflag;
+
+void stm8_reset(int r)
+{
+
+	if(r)
+	{
+		cp_group_data_output("STM8_RESET", 1);
+	} else {
+		cp_group_data_output("STM8_RESET", 0);
+		usleep(5000);
+	}
+}
+
+int button_debounce()
+{
+	static int button_hist[3] = {0,0,0};
+	int ret=0;
+	unsigned long pValue;
+
+ 	cp_group_data_input ("BUTTON", &pValue);
+
+	if(button_hist[0] && button_hist[1] && button_hist[2] && !pValue)
+		ret = 1;
+
+	button_hist[0] = button_hist[1];
+	button_hist[1] = button_hist[2];
+	button_hist[2] = pValue;
+
+	return ret;
+}
+
+void FlushComport()
+{
+	int n;
+	
+	do {
+		n = PollComport(COM_PORT,buf,BUFFER_LEN);
+	} while(n > 1);
+}
+
+void handler(int signum)
+{
+	runflag = 0;
+}
+
+void main_stm(int button)
+{
+	unsigned int n;
+	static int addfinaltime;
+	static enum MAIN_STM state=IDLE;
+
+	switch(state)
+	{
+		case IDLE:
+			if(button) state=START_VUMETER;
+		break;
+		case START_VUMETER:
+			SendByte(COM_PORT, CMD_ONAIRBLINK | 1 );
+			SendByte(COM_PORT,CMD_CTRL | ARG_CTRL_VUONLY);				
+			state = VUMETER;
+		break;
+		case VUMETER:
+			if(button) state = START_STREAMING;
+		break;
+		case START_STREAMING:
+			SendByte(COM_PORT,CMD_CTRL | ARG_CTRL_STOPVUONLY);
+			FlushComport(COM_PORT);
+			
+			SendByte(COM_PORT, CMD_ONAIR | 1 );
+			SendByte(COM_PORT,CMD_CTRL | ARG_CTRL_STARTSTREAM);
+			state = STREAMING;
+		break;
+		case STREAMING:
+			n = PollComport(COM_PORT,buf,BUFFER_LEN);
+		
+			if(n > 0) 
+			{
+				fwrite(buf,1,n,stdout);
+				//if(shout_config.enabled) stream_shout(buf,n);
+			}
+			if (button) {
+				state = END_STREAMING;
+				SendByte(COM_PORT,CMD_CTRL | ARG_CTRL_STOPSTREAM);
+				addfinaltime = 500000;
+			}
+		break;
+		case END_STREAMING:
+			addfinaltime -= SLEEP_TIME;
+			n = PollComport(COM_PORT,buf,BUFFER_LEN);
+			
+			if(n > 0) 
+			{
+				fwrite(buf,1,n,stdout);
+				
+			}						
+			
+			if(addfinaltime < 0) {
+				SendByte(COM_PORT, CMD_ONAIR | 0 );
+				state = IDLE;
+			}
+				
+		break;
+		case ERROR:
+			//clear ERROR with button
+			if(button) state = IDLE;
+		break;
+	}
+
+}
+
+
+int main(int argc, char *argv[])
+{
+	
+	unsigned char i;
+	unsigned long b;
+	int button;
+	
+	
+
+	signal(SIGINT, handler); 
+
+	if(OpenComport(COM_PORT,BAUD_RATE) != 0)
+		fprintf(stderr,"Error opening comport!\n");
+
+	cp_config_init();
+	cp_group_init();
+	read_config();
+	
+	stm8_reset(1);
+	FlushComport(COM_PORT);
+	stm8_reset(0);
+
+	SendByte(COM_PORT, CMD_STATUSLED_LEFT | 1);	
+	
+	runflag=1;
+	while(runflag)
+	{
+		button = button_debounce();
+		if(button)
+		{
+			asm("nop");
+		}
+		main_stm(button);
+		usleep(SLEEP_TIME);
+	}
+
+	fprintf(stderr, "Exiting ...\n");
+	CloseComport(COM_PORT);
+}
+