Пример #1
0
Файл: main.c Проект: myixr/wshow
int main(void)
{

  /* USER CODE BEGIN 1 */
	user_init1();
  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C2_Init();
  MX_USART1_UART_Init();

  /* USER CODE BEGIN 2 */
		user_init2();

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */
		user_loop();

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}
static void ICACHE_FLASH_ATTR loop(os_event_t* events)
{
    if (!initFinished)
    {
        // loop by adding this function to the task queue
        system_os_post(user_procTaskPrio, 0, 0);

        if(is_wifi_connected())
        {
            user_init2();
            initFinished = true;
        }

        return;
    }

    static bool pendingEmailAlert = false;
    // check unprocessed OOK packets to see if there are any newly
    //   triggered sensors
    bool newTriggerings = false;
    while(packets_available(&unprocessedPackets))
    {
        uint32 packet = packet_pop(&unprocessedPackets);
        char* source = my_strdup(ook_ID_to_name(packet));
        // check if the current packet is a new triggering
        bool packetIsNewTriggering = true;

#ifdef PRINT_OOK_PACKETS_DEBUG
        os_printf("%x\r\n", packet);
#endif
        // check for the key fob signals for arming/disarming
        if (packet == ARM_CODE)
            arm_alarm();
        else if (packet == DISARM_CODE)
            disarm_alarm();
        // must be some other code, or invalid
        else if (source != NULL && alarmArmed)
        {
            int i, j;
            // ensure that an already-triggered sensor is not duplicated in list
            for (i = 0; i < triggeredSensorsIter; i++)
            {
                if (triggeredSensorsNames[i] != NULL)
                    for (j = 0; source[j] == triggeredSensorsNames[i][j]; j++)
                    {
                        // reached the end of both strings and they've been the
                        // same all along
                        if (source[j] == '\0')
                            packetIsNewTriggering = false;
                    }
            }
            if (packetIsNewTriggering)
            {
                triggeredSensorsNames[triggeredSensorsIter] = source;
                triggeredSensorsTimestamps[triggeredSensorsIter] = sntp_get_current_timestamp();
                triggeredSensorsIter += 1;
                newTriggerings = true;
            }
            else
                os_free(source);
        }
    }

    if (newTriggerings)
    {
        updateWebpage();
        pendingEmailAlert = true;
    }

    // checked so that a pending email alert is cancelled if the "disarm" button is pressed after a
    //   sensor has been triggered
    if(!alarmArmed)
        pendingEmailAlert = false;
    
    if (pendingEmailAlert)
    {
        if (triggeredSensorsIter > 0 &&
            sntp_get_current_timestamp() - triggeredSensorsTimestamps[0] > ALERT_EMAIL_DELAY)
        {
            generate_email_body();
            send_email("ESP8266 Alarm", generatedEmailBody); 
#ifdef PRINT_EMAIL_DEBUG
            os_printf("trying to send email\n");
#endif
            pendingEmailAlert = false;
        }
    }

    // loop by adding this function to the task queue
    system_os_post(user_procTaskPrio, 0, 0);
}