Esempio n. 1
0
time_t time(time_t *out) 
{
	// TODO Millis must be reworked to return an actual time
	uint64_t millis = Millis();
	*out = millis;
	return millis;
}
Esempio n. 2
0
// Returns the keykode of the pressed key, or NO_KEY if no key is pressed
char Keypad_getKey(){
    
    char key = NO_KEY; // Assume that no key is pressed, this is the default return for getKey()
    unsigned char c,r;    

    for (c=0; c<keypad_columns; c++){
		digitalwrite(columnPins[c],LOW);	// Activate the current column.
		for (r=0; r<keypad_rows; r++){	// Scan all the rows for a key press.
			//  The user pressed a button for more then debounceTime microseconds.
			if (currentKey == userKeymap[c+(r*keypad_columns)]){
				// Button hold
				if (((Millis()-lastUpdate) >= holdTime) && digitalread(rowPins[r]) == LOW){
					Keypad_transitionTo(HOLD);
				}
				// Button release
				if (((Millis()-lastUpdate) >= debounceTime) && digitalread(rowPins[r]) == HIGH){
					Keypad_transitionTo(RELEASED);
					currentKey = NO_KEY;
				}
			} 
			// Button pressed event.  The user pressed a button.
			else if (((Millis()-lastUpdate) >= debounceTime) && digitalread(rowPins[r]) == LOW){
				digitalwrite(columnPins[c],HIGH);	// De-activate the current column.
				key = userKeymap[c+(r*keypad_columns)];
				lastUpdate = Millis();
				goto EVALUATE_KEY; 			// Save resources and do not attempt to parse two keys at a time
			} 
		}
		digitalwrite(columnPins[c],HIGH);	// De-activate the current column.
	}
	
	EVALUATE_KEY:
	if (key != NO_KEY && key != currentKey){ 
		currentKey = key;
		Keypad_transitionTo(PRESSED);
		return currentKey;
	} 
	else{
		return NO_KEY;
	}
}
void Wifi_WaitForAnswer_SEND_OK(uint16_t cmdSize)
{

	while(waitingForReponse == 1 && (Millis() - TxWaitForResponse_TimeStmp) < ESP_ResponseTimeout_ms)
	{
		WaitForAnswer_cmd_Buffer = memmem(USART3_RxBuffer,RxBuffSize,"AT+CIPSEND",10);
		if(strlen(WaitForAnswer_cmd_Buffer)>0)
		{
			while(waitingForReponse == 1 && (Millis() - TxWaitForResponse_TimeStmp) < ESP_ResponseTimeout_ms)
				{
				if(WaitForAnswer_ans_Buffer = memmem(USART3_RxBuffer,strlen(USART3_RxBuffer),"SEND OK\r\n",9))
				{
					pointerRange = WaitForAnswer_cmd_Buffer - WaitForAnswer_ans_Buffer;
					ClearArray_Size(WaitForAnswer_cmd_Buffer, cmdSize + 9);
					OKFound=1;
					waitingForReponse = 0;
				}
				}
			//Check for OK or Error Message
		}
	}

}
///Will parse the USART buffer periodically (based on #defined poll interval) for the echo of cmdToWaitFor
///in the response from the ESP8266 module.
void Wifi_WaitForAnswerCMD(char *cmdToWaitFor, uint16_t cmdSize)
{

	while(waitingForReponse == 1 && (Millis() - TxWaitForResponse_TimeStmp) < ESP_ResponseTimeout_ms)
		{
		WaitForAnswer_cmd_Buffer = memmem(USART3_RxBuffer,RxBuffSize,cmdToWaitFor,cmdSize);
		if(strlen(WaitForAnswer_cmd_Buffer)>0)
		{
			if(WaitForAnswer_ans_Buffer = memmem(WaitForAnswer_cmd_Buffer,strlen(WaitForAnswer_cmd_Buffer),"OK\r\n",4))
			{
				ClearArray_Size(WaitForAnswer_cmd_Buffer, strlen(WaitForAnswer_cmd_Buffer));
				OKFound=1;
				waitingForReponse = 0;
			}
			//Check for OK or Error Message

		}

		};
	//OKFound=0;
	//ERRORFound=0;
}
int main(void)
{
	//printf("Main()\r\n"); //SEMIHOSTING DEBUG OUT
	// LED lamp 12800 MAX (before no response, wont turn on) But a few second delay before Diode saturation (light comes on)
	// 12400 - min for reasonable saturation delay
	dimmingValue = 11000; // 12600

	//printf("RCC clocks Str\r\n"); //SEMIHOSTING DEBUG OUT
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); // ESP8266 - Wifi
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
	//printf("RCC clocks Fin\r\n"); //SEMIHOSTING DEBUG OUT


	POWER_LED_Config.GPIO_Speed = GPIO_Speed_50MHz;
	POWER_LED_Config.GPIO_Mode = GPIO_Mode_Out_PP;
	POWER_LED_Config.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6; // PB1 - Maple On-board LED | PB6 - Maple Pin 16 | PB0 - CH_PD (Power ON) Pin for ESP8266 Wifi
	GPIO_Init(GPIOB,&POWER_LED_Config);

	//SetArray_Size(USART3_RxBuffer,RxBuffSize);

	//printf("GPIO Port: B Pins: 0,1,6 Fin\r\n"); //SEMIHOSTING DEBUG OUT

		GPIOB->BRR = GPIO_Pin_0; // Power OFF for ESP8266
		//printf("ESP8266 Powered OFF (CH01 Pin Disabled (Pulled Low))\r\n"); //SEMIHOSTING DEBUG OUT
		GPIOB->BSRR = GPIO_Pin_1; // PB1 - Maple On-board LED
		GPIOB->BRR = GPIO_Pin_6; // PB6 - Maple Pin 16
		GPIOB->BSRR = GPIO_Pin_0; // Power On for ESP8266
		//printf("ESP8266 Powered ON\r\n"); //SEMIHOSTING DEBUG OUT
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);

	ZeroCross_Config.GPIO_Mode = GPIO_Speed_50MHz;
	ZeroCross_Config.GPIO_Mode = GPIO_Mode_IPD;
	ZeroCross_Config.GPIO_Pin = GPIO_Pin_14; // PB14 - Maple Pin 29
	GPIO_Init(GPIOB,&ZeroCross_Config);
	//printf("GPIOB Pin 14 configured for Zero-Crossing detection (Maple Pin 29)\r\n"); //SEMIHOSTING DEBUG OUT

	Button_Config.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	Button_Config.GPIO_Speed = GPIO_Speed_50MHz;
	Button_Config.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_7;
	GPIO_Init(GPIOB,&Button_Config);
	//printf("GPIOB Pin 14 configured for Zero-Crossing detection (Maple Pin 29)\r\n"); //SEMIHOSTING DEBUG OUT



	//Init_USART3(460800,ENABLE);
	//Init_USART1(460800,ENABLE);

	//*********USE USART3 Rx in DMA Mode
	#define ESP_RX_DMA_BUF_POLL_Interval_ms 1000 //Every 1 Sec check the Rx Buffer
	//uint32_t LastRxBufferReadTime = 0;
	Init_USART3_DMA(2000000,USART3_RxBuffer, USART3_RxBufferSize);

	//*********USE USART3 Rx in DMA Mode


	//Init_USART3(2000000,ENABLE);
	//Init_USART1(2000000,ENABLE);

	//for (mj=0;mj<500000;mj++);// FOR TESTING

	//for (mj=0;mj<5000;mj++);// FOR TESTING
	//Need to wait for a sec before transmitting data. Let ESP8266 power on
	//for (mj=0;mj<500000;mj++); // FOR TESTING


	//USART_SendData(USART3,"AT/r/n");

	ConfigZeroCrossExternalInt();
	ConfigZeroCross_NVIC();
	Init_Time(MILLISEC);

	//for (mj=0;mj<20500;mj++);
	//Wifi_Init();
	//printf("Wifi_Init() Complete\r\n"); //SEMIHOSTING DEBUG OUT
	//for (mj=0;mj<20500;mj++);
	for (mj=0;mj<130500;mj++);
	//ConnectToAP("Nonya","porsche911");
	//for (mj=0;mj<70500;mj++);
	//printf("Preparing to start local ESP8266 server at ID: 1 Port: 80\r\n"); //SEMIHOSTING DEBUG OUT
	Wifi_SendCommand(WIFI_JOIN_NONYA);
	StartServer(1,80);

	Wifi_SendCommand(WIFI_GET_CURRENT_IP);

	//Sets data of first few bites of DMA so it doesn't start with 12 null terminators.
	USART3_RxBuffer[0] = "1";
	USART3_RxBuffer[1] = "1";
	USART3_RxBuffer[2] = "1";
	USART3_RxBuffer[3] = "1";
	USART3_RxBuffer[4] = "1";
	USART3_RxBuffer[5] = "1";
	USART3_RxBuffer[6] = "1";
	USART3_RxBuffer[7] = "1";
	USART3_RxBuffer[8] = "1";
	USART3_RxBuffer[9] = "1";
	USART3_RxBuffer[10] = "1";
	USART3_RxBuffer[11] = "1";
	USART3_RxBuffer[12] = "1";



	//Main Run Loop
	for(;;)
    {

		if(restRequestWaiting == 1)
		{
			SendRESTResponse(activeConnectionNum,RESTResponse_Headers_Test_OK,RESTResponse_Body_TEST_JSON);
		}
      //This Polls the dma buffer ever [poll interval] For new incoming data from esp (Starting with +IPD)
		if((Millis() - lastDMABuffPoll) >= DMA_Rx_Buff_Poll_Int_ms)
		{
			lastDMABuffPoll = Millis();
			currentIPD = Wifi_CheckDMABuff_ForIPDData();
			if(currentIPD.Valid == 1)
			{
				SendRESTResponse(currentIPD.ConnectionNum, RESTResponse_Headers_Test_OK, customRESTResponse);
			}
		}
		//Check for data to transmit USART3

		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)) // Power cycle the ESP8266
		{
			Wifi_SendCommand(Command_To_Redirect);
			for (mj=0;mj<130500;mj++);//debounce
		}
    }
}
void Wifi_WaitForAnswer()
{
	while(waitingForReponse == 1 && (Millis() - TxWaitForResponse_TimeStmp) < ESP_ResponseTimeout_ms);
	OKFound=0;
	ERRORFound=0;
}
void Wifi_ReadyWaitForAnswer()
{
	TxWaitForResponse_TimeStmp = Millis();
	waitingForReponse = 1;

}
IPD_Data Wifi_CheckDMABuff_ForIPDData()
{
	currentIPD.Valid = 0;
	//if((Millis() - lastDMABuffPoll) >= DMA_Rx_Buff_Poll_Int_ms)
	//		{
				//Probably need to check for new client ({clientNum},CONNECT)
				lastDMABuffPoll = Millis();
				ESP_IPD_Data_Buffer_Pntr = memmem(USART3_RxBuffer,RxBuffSize,"+IPD",4);
				if(ESP_IPD_Data_Buffer_Pntr)
				{
					//position = DMA_GetCurrDataCounter(DMA1_Channel3);
					//position = strlen(USART3_RxBuffer);
					//Copy IPD message and data to its own buffer so DMA can go about its business
					strcpy(ESP_IPD_DataBuffer,ESP_IPD_Data_Buffer_Pntr);
					DMA_Cmd(DMA1_Channel3,DISABLE);

					//Wipes the received message from the DMA buffer (using the pointer to the data)
					//This makes sure the data doesn't get mistaken for a new request, on the next buffer polling.
					ClearArray_Size(ESP_IPD_Data_Buffer_Pntr,strlen(ESP_IPD_Data_Buffer_Pntr));
					DMA_Initialize(USART3_RxBuffer, RxBuffSize);


					//now we process since DMA isn't going to stomp on us.
					currentIPD = ProcessIPD_Data(ESP_IPD_DataBuffer);

						//TODO: Need to add a level of error detection/correction as data may be missing the
					if(strstr(currentIPD.RequestType, "POST"))
					{
						//if URI contains dimming (the test for now)
						if(strstr(currentIPD.URI, "dimming"))
						{
							if(strstr(currentIPD.URI, "?"))//If query String is found
							{
								URI = strtok(currentIPD.URI, "?");
								if(strstr(URI,"="))//If URI was sent prepended with a '/' this will be true
								{
									queryString1 = strtok(URI, "=");

									queryValue1 = strtok(NULL, "\0");
								}
								else
								{
								queryString1 = strtok(NULL, "=");
								if(strstr(currentIPD.URI, "&"))
								{
									queryValue1 = strtok(NULL, "&");
								}
								else
								{
									queryValue1 = strtok(NULL, "\0");
								}
								}
								currentIPD.Valid = 1;
							}

							dimmingValueToValidate = atoi(queryValue1);
							if(dimmingValueToValidate <= 13000)
							{
								dimmingValue = dimmingValueToValidate;

								RefreshCustomRESTResponseDimmer("172.20.112.136", "192.168.4.1", dimmingValue);
								//SendRESTResponse(currentIPD.ConnectionNum, RESTResponse_Headers_Test_OK, customRESTResponse);
							}
							else {
								RefreshCustomRESTResponse("172.20.112.136", "192.168.4.1", "dimmingValue", "InvalidValue");
							}
								currentIPD.Valid = 1;
						}
					}
					//printf("Incoming webrequest\r\n");
				}
				//DMA_Rx_Buff_Index = strlen(USART3_RxBuffer);
				//tstBuff = mempcpy(USART3_RxBuffer_Buffer, USART3_RxBuffer, RxBuffSize);
				//DMA_Rx_Buff_Index = tstBuff - &USART3_RxBuffer_Buffer[0];
				//tstBuff = memmem(USART3_RxBuffer,sizeof(USART3_RxBuffer),"OK\r\n",4);
				//ClearArray_Size(USART3_RxBuffer, sizeof(USART3_RxBuffer));

		//	}

				return currentIPD;
}
Esempio n. 9
0
void main(void)
{    
  CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  
  Delay_Init();
  RS485_Init(115200);
#if DEBUG
  //  UART_Init(115200);
#endif
  SRF05_Init();
  SRF05_AutoPoolEnable();
  
  //flash_read_buffer((char *)&my_data, sizeof (struct flash_data));
  my_data.id = 0x21;
  //  UART_SendByte(0x21, HEX);
  //  RS485_SendStr("Hello world.\n");
  
  // LED run
  GPIO_Init(LED_RUN_PORT, LED_RUN_PIN, GPIO_MODE_OUT_PP_HIGH_FAST);
  GPIO_WriteHigh(LED_RUN_PORT, LED_RUN_PIN);
  
  while (1)
  {
    if (Millis() - tmp_time > 500)
    {
      LED_RUN_TOGGLE;
      tmp_time = Millis();
			
#if DEBUG
			RS485_DIR_OUTPUT;
			RS485_SendStr("\n");
			RS485_SendFloat(SRF05_GetDistance());
			RS485_DIR_INPUT;
#endif
    }
    
    if (GPIO_ReadInputPin(RS485_SEL_PORT, RS485_SEL_PIN) == RESET)
    {
      if (RS485_Available() >= 8)
      {
        //      memset(packet_buff, 0, PACKET_BUFFER_SIZE);
        packet_len = RS485_GetData(packet_buff);
        packet = (struct Packet *)packet_buff;
        if (packet_len < 4 + getTypeLength(packet->data_type))
        {
          // not enough length
        }
        else
        {
          switch (packet->cmd)
          {
          case CMD_QUERY:
            if (packet->id == my_data.id)
            {
              LED_RUN_TOGGLE;
              
              tmp_distance = SRF05_GetDistance();
              packet->data_type = TYPE_FLOAT | BIG_ENDIAN_BYTE_ORDER;
              memcpy(packet->data, &tmp_distance, getTypeLength(packet->data_type));
              packet->data[getTypeLength(packet->data_type)] = checksum((char *)packet);
              RS485_DIR_OUTPUT;
              RS485_SendData(packet_buff, 4 + getTypeLength(packet->data_type));
              RS485_DIR_INPUT;
            }
            else if (IS_BROADCAST_ID(packet->id))
            {
              //              if (GPIO_ReadInputPin(RS485_SEL_PORT, RS485_SEL_PIN) == RESET)
              //              {
              LED_RUN_TOGGLE;
              
              packet->id = my_data.id;
              tmp_distance = SRF05_GetDistance();
              packet->data_type = TYPE_FLOAT | BIG_ENDIAN_BYTE_ORDER;
              memcpy(packet->data, &tmp_distance, getTypeLength(packet->data_type));
              packet->data[getTypeLength(packet->data_type)] = checksum((char *)packet);
              RS485_DIR_OUTPUT;
              RS485_SendData(packet_buff, 4 + getTypeLength(packet->data_type));
              RS485_DIR_INPUT;
              //              }
            }
            else
            {
              // not own id
            }
            break;
          case CMD_CONTROL:
            // by default, this mode used only for setting id
            // the id stored on the first bytes of data bytes
            if (IS_BROADCAST_ID(packet->id))
            {
              LED_RUN_TOGGLE;
              if (IS_SENSOR_ULTRA_SONIC(packet->data[0]))
              {
                my_data.id = packet->data[0];
              }
            }
            break;
          default:
            break;
          }
          
          RS485_Flush();
        }
      }
    }
    else
    {
      RS485_Flush();
    }
		
		SRF05_ProcessTrigger();
  }
}