Page MenuHomePhabricator

No OneTemporary

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -1,446 +1,455 @@
/*
* This is COSMICrtOS based on FreeRTOS port to ATmega2560.
* (c) 2011 Andreas Boehler <andreas@aboehler.at>
*
* ATTENTION: Software licenses apply! GPL is probably incompatible
* with our NDA! Can we link to a binary blob instead?
*/
// FIXME: Get rid of FreeRTOS
#include <stdlib.h>
#include <string.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "iomacro.h"
#include "main.h"
#include "ethernet.h"
volatile int gEthFired = FALSE;
SOCKET gSock = MAX_SOCK_NUM;
#define TOGGLE_DELAY 300
#define INCR_DELAY 200
#define CMD_STOP 5
#define CMD_SELECT 6
#define CMD_UP 7
#define CMD_DOWN 8
#define AI_PIN0 F,0
#define AI_PIN1 F,1
#define AI_PIN2 F,2
#define AI_PIN3 F,3
#define DO_PIN_STOP E,3
#define DO_PIN_SELECT H,3
#define DO_PIN_UP H,4
#define DO_PIN_DOWN H,5
/***************************************
* Helper function to setup interrupts *
***************************************/
void setupExternalInterrupts(void)
{
EICRB |= (1 << ISC41); // Enable falling edge interrupt
EIMSK |= (1 << INT4); // enable external interrupt 4
}
/****************************************************
* Setup the interrupt handler for ethernet traffic *
****************************************************/
ISR(INT4_vect)
{
gEthFired = TRUE;
}
/************************************************
* Setup and initialize the ethernet controller *
************************************************/
void setupEthernet(void)
{
uint8_t mac[] = { 0x00, 0x01, 0x02, 0x0D, 0x74, 0x47 };
uint8_t ip[] = { 192, 168, 1, 20 };
uint8_t gateway[] = { 192, 168, 1, 1 };
uint8_t subnet[] = { 255, 255, 255, 0 };
if(ethernet_init(mac, ip, gateway, subnet))
{
if((gSock = socket_server_init(SOCK_PORT)))
{
gSock -= 1;
}
}
}
/*****************
* Main function *
*****************/
int main( void )
{
// Setup registers for input and output
setupIOPorts();
setupExternalInterrupts();
if(spi_init())
setupEthernet();
sei();
/*
selectBlindChannel(5);
_delay_ms(5000);
blindUp(1);
blindDown(1);
blindDown(2);
blindDown(3);
blindDown(4);
blindDown(5);
*/
uint8_t new_connect[MAX_SOCK_NUM];
uint8_t connected[MAX_SOCK_NUM];
uint8_t i;
uint16_t len;
SOCKET sock;
unsigned char clientline[BUF_SIZE];
for(i=0; i < MAX_SOCK_NUM; i++)
{
new_connect[i] = TRUE;
connected[i] = FALSE;
}
for( ;; )
{
if(gEthFired)
{
gEthFired = FALSE;
sock = socket_server_handle_interrupt();
connected[sock] = socket_server_socket_connected(sock);
if(connected[sock])
{
if(new_connect[sock])
{
socket_server_write(sock, (unsigned char*)"BlindControl ready\n", (uint16_t)19);
new_connect[sock] = FALSE;
}
}
else
{
if (!new_connect[sock])
new_connect[sock] = TRUE;
if(socket_server_socket_closed(sock))
{
socket_server_close_socket(gSock);
setupEthernet();
}
}
if(connected[sock] && (len = socket_server_data_available(sock)))
{
if(len > BUF_SIZE)
len = BUF_SIZE;
socket_server_receive(sock, clientline, len);
if(!handle_req((signed char*)clientline))
{
socket_server_close_socket(gSock);
new_connect[sock] = TRUE;
setupEthernet();
}
}
}
}
return 0;
}
int handle_req(signed char *clientline)
{
int blindNum;
char helpStr[] = "Available Commands: SelectBlind N, BlindUp[ N], BlindDown[ N], "
- "BlindStop[ N], BlindIncr[ N], BlindDecr[ N], AllUp, AllDown, help, quit\n\n";
+ "BlindStop[ N], BlindIncr[ N], BlindDecr[ N], AllUp, AllDown, AllStop, help, quit\n\n";
if(strncmp(clientline, "SelectBlind ", 12) == 0)
{
blindNum = atoi(&clientline[12]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
selectBlindChannel(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindUp ", 8) == 0)
{
blindNum = atoi(&clientline[8]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
blindUp(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindDown ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
blindDown(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindStop ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
blindStop(blindNum);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindIncr ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
selectBlindChannel(blindNum);
sendCommand(CMD_UP);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindDecr ", 10) == 0)
{
blindNum = atoi(&clientline[10]);
if((blindNum > 5) || (blindNum < 0))
{
socket_server_write(gSock, (unsigned char *)"ERR\n", 4);
}
else
{
selectBlindChannel(blindNum);
sendCommand(CMD_DOWN);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP); socket_server_write(gSock, "OK\n", 3);
}
}
else if(strncmp(clientline, "BlindUp", 7) == 0)
{
sendCommand(CMD_UP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindDown", 9) == 0)
{
sendCommand(CMD_DOWN);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindStop", 9) == 0)
{
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindIncr", 9) == 0)
{
sendCommand(CMD_UP);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "BlindDecr", 9) == 0)
{
sendCommand(CMD_DOWN);
_delay_ms(INCR_DELAY);
sendCommand(CMD_STOP);
socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "AllUp", 5) == 0)
{
for(blindNum=1; blindNum<6; blindNum++)
{
blindUp(blindNum);
- socket_server_write(gSock, "OK\n", 3);
}
+ socket_server_write(gSock, "OK\n", 3);
+
}
else if(strncmp(clientline, "AllDown", 7) == 0)
{
for(blindNum=1; blindNum<6; blindNum++)
{
blindDown(blindNum);
- socket_server_write(gSock, "OK\n", 3);
}
+ socket_server_write(gSock, "OK\n", 3);
+ }
+ else if(strncmp(clientline, "AllStop", 7) == 0)
+ {
+ for(blindNum = 1; blindNum < 6; blindNum++)
+ {
+ blindStop(blindNum);
+ }
+ socket_server_write(gSock, "OK\n", 3);
}
else if(strncmp(clientline, "help", 4) == 0)
{
socket_server_write(gSock, helpStr, strlen(helpStr));
}
else if(strncmp(clientline, "quit", 4) == 0)
{
return FALSE;
}
else
{
socket_server_write(gSock, "ERR\n", 4);
}
return TRUE;
}
int getCurrentBlindChannel(void)
{
int currentChannel = 0;
int readValues;
readValues = readFromADC();
if(readValues == 0)
{
sendCommand(CMD_SELECT);
readValues = readFromADC();
}
switch(readValues)
{
case 1:
currentChannel = 1;
break;
case 2:
currentChannel = 2;
break;
case 4:
currentChannel = 3;
break;
case 8:
currentChannel = 4;
break;
case 15:
currentChannel = 5;
break;
default:
currentChannel = 0;
}
return currentChannel;
}
int readFromADC(void)
{
int j;
int channel = 0;
for(j=0;j<200;j++)
{
if(!IS_SET(AI_PIN0))
channel |= 0x01;
if(!IS_SET(AI_PIN1))
channel |= 0x02;
if(!IS_SET(AI_PIN2))
channel |= 0x04;
if(!IS_SET(AI_PIN3))
channel |= 0x08;
}
return channel;
}
int selectBlindChannel(int telisChannel)
{
while(getCurrentBlindChannel() != telisChannel)
sendCommand(CMD_SELECT);
return TRUE;
}
int blindUp(int telisChannel)
{
selectBlindChannel(telisChannel);
sendCommand(CMD_UP);
return TRUE;
}
int blindDown(int telisChannel)
{
selectBlindChannel(telisChannel);
sendCommand(CMD_DOWN);
return TRUE;
}
int blindStop(int telisChannel)
{
selectBlindChannel(telisChannel);
sendCommand(CMD_STOP);
return TRUE;
}
int sendCommand(int digitalChannel)
{
switch(digitalChannel)
{
case CMD_STOP:
SET(DO_PIN_STOP);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_STOP);
break;
case CMD_SELECT:
SET(DO_PIN_SELECT);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_SELECT);
break;
case CMD_UP:
SET(DO_PIN_UP);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_UP);
break;
case CMD_DOWN:
SET(DO_PIN_DOWN);
_delay_ms(TOGGLE_DELAY);
RESET(DO_PIN_DOWN);
break;
}
_delay_ms(TOGGLE_DELAY);
return TRUE;
}
/****************************************
* Setup all required I/O Ports for use *
****************************************/
uint8_t setupIOPorts( void )
{
SET_INPUT(AI_PIN0);
SET_INPUT(AI_PIN1);
SET_INPUT(AI_PIN2);
SET_INPUT(AI_PIN3);
RESET(DO_PIN_STOP);
RESET(DO_PIN_SELECT);
RESET(DO_PIN_UP);
RESET(DO_PIN_DOWN);
SET_OUTPUT(DO_PIN_STOP);
SET_OUTPUT(DO_PIN_SELECT);
SET_OUTPUT(DO_PIN_UP);
SET_OUTPUT(DO_PIN_DOWN);
return TRUE;
}
//EOF

File Metadata

Mime Type
text/x-diff
Expires
Mon, Dec 23, 2:59 AM (1 d, 16 h ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
539960
Default Alt Text
(10 KB)

Event Timeline