tangibleInterface.c 2.28 KB
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "serial.h"
#include "ethernet.h"
#include "w5100.h"
#include "socket.h"

#define MAC_SIZE	6
#define IPV4_SIZE	4
bool isComMem = false; // Commande déjà enregistrée ou non?
bool sleep = true; //Etat de l'interface : true = mode sommeil / false = mode eveillé

SOCKET sUDP=0;
SOCKET sTCP=1;

uint8 comMem[2]; //Commande en mémoire

uint8_t addr[IPV4_SIZE];
uint16_t port;


void request(uint8 rq [2])
{
  
  if (!isComMem) //Aucune commande en mémoire
    {
      isComMem = true;
      comMem[0] = rq[0]; comMem[1] = rq [1];
    }
  
  uint8 x = rq [0] & 0xE0; //Récupération de la commande
  x = x >> 5;
  uint16 y = rq[0] & 0x1F; //Récupération du pourcentage
  y = y << 8;
  y += rq[1];
  uint16 per = (100*y)/8192; //2^13 = 100 %
  switch(x)
    {
      
    case 0:
      printf("REQUEST STATUS\n");
      //ENVOI TCP ICI , renvoi x = 0x01
      comMem[0] = rq[0]; comMem[1] = rq [1]; //save
      break;

    case 2:
      printf("SET / UNSET SLEEP\n");
      if (y == 0x0001)
	{
	      sleep = false;
	      printf("MODE EVEILLE\n");
	}
      else if (y == 0x0000)
	{
	      sleep = true;
	      printf("MODE SOMMEIL\n");
	}
      comMem[0] = rq[0]; comMem[1] = rq [1]; //save 
      break;
      
    case 3:
      // ENVOI TCP ICI , envoi de comMem[2]    
      break;
    case 5:
      //Envoi à l'interface secondaire ordre + pourcentage.
      
      break;
    }


  
}

int main(void)
{ 
  uint8_t mac[MAC_SIZE] = {0xA0,0xBD,0xCD,0xDD,0xED,0xFD};
  uint8_t ip[IPV4_SIZE] = {192,168,1,205};
  uint8_t gateway[4] = {192,168,1,254};
  uint8_t mask[4] = {255,255,255,0};

  /* uint8_t mac[MAC_SIZE] = {0xA0,0xBD,0xCD,0xDD,0xED,0xFD}; */
  /* uint8_t ip[IPV4_SIZE] = {172,26,145,205}; */
  /* uint8_t gateway[4] = {172,26,145,254}; */
  /* uint8_t mask[4] = {255,255,255,0}; */
  
  uint8 buf[2];
  uint16 datasize;

  init_printf();
  ethernet_init(mac,ip,gateway,mask); 
  if (!socket(sUDP,Sn_MR_UDP,2020,0))
    {
      return -1;
    }
  if (!socket(sTCP,Sn_MR_TCP,2020,0))
    {
      return -1;
    }
    
  while(1)
    {
      if((datasize=recvfrom(sUDP,buf,sizeof(buf),addr,&port)) == 8)
      {
        request(buf);
      }
    }
  close(sTCP);
  close(sUDP);
  return 0;
}