void ICACHE_FLASH_ATTR user_init(void) { uart0_init(BIT_RATE_74880); // 0x40200000 is the base address for spi flash memory mapping, ESPFS_POS is the position // where image is written in flash that is defined in Makefile. espFsInit((void*)(0x40200000 + ESPFS_POS)); httpdInit(builtInUrls, 80); #ifdef SHOW_HEAP_USE os_timer_disarm(&prHeapTimer); os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL); os_timer_arm(&prHeapTimer, 3000, 1); #endif init_relay(); os_timer_disarm(&startup_timer); os_timer_setfn(&startup_timer, (os_timer_func_t *)onesec, (void *)0); os_timer_arm(&startup_timer, 1000, 1); os_timer_disarm(&delayed_start_timer); os_timer_setfn(&delayed_start_timer, (os_timer_func_t *)delayed_start, (void *)0); os_timer_arm(&delayed_start_timer, 500, 1); load_config(); os_printf("running\n"); }
void main(int argc, char *argv[]) { struct ListenerOptions lopts; int c; void (*bg)(char *) = &daemonize; char *pidfile = "/tmp/ihlt.pid"; int log_level = LOG_DEBUG, log_opts = LOG_PID; lopts.nodename = NULL; lopts.servname = PORT; memset(&lopts.hints, 0, sizeof(struct addrinfo)); lopts.hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ lopts.hints.ai_socktype = SOCK_STREAM; /* Datagram socket */ lopts.hints.ai_flags = AI_PASSIVE; /* Example from: http://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Option-Example.html */ while (1) { static struct option long_options[] = { /**/ { "verbose", no_argument, NULL, 'v' }, /**/ { "quiet", no_argument, NULL, 'q' }, /**/ { "foreground", no_argument, NULL, 'f' }, /**/ { "stderr", no_argument, NULL, 's' }, /**/ { "err", no_argument, NULL, 's' }, /**/ { "pidfile", required_argument, NULL, 'P' }, /**/ { "bind", required_argument, NULL, 'b' }, /**/ { "port", required_argument, NULL, 'p' }, /**/ { 0, 0, 0, 0 } }; /* getopt_long stores the option index here. */ int option_index = 0; c = getopt_long(argc, argv, "vqsSp:P:p:f", long_options, &option_index); /* Detect the end of the options. */ if (c == -1) break; switch (c) { case 0: /* If this option set a flag, do nothing else now. */ if (long_options[option_index].flag != 0) break; printf("option %s", long_options[option_index].name); if (optarg) printf(" with arg %s", optarg); printf("\n"); break; case 'v': log_level--; break; case 'q': log_level++; break; case 's': log_opts |= LOG_PERROR; break; case 'f': bg = &no_daemonize; break; case 'P': pidfile = optarg; break; case 'p': lopts.servname = optarg; break; case 'b': lopts.nodename = optarg; break; case '?': /* getopt_long already printed an error message. */ break; default: abort(); } } /* Logging */ setlogmask(LOG_UPTO(log_level)); openlog(DAEMON_NAME, log_opts, LOG_USER); syslog(LOG_INFO, "Daemon starting up"); /* Deamonize */ bg(pidfile); syslog(LOG_INFO, "Daemon running"); init_relay(); EnterListener(&lopts); exit(EXIT_SUCCESS); }
void main(void) { // Have to init the clock first init_clock(); // These initialize variables // Mostly just zeroing them okay_to_transmit = FALSE; server_wants_header = FALSE; inside_non_blocking_interrupt = FALSE; last_connect_time = 0; check_in_period = 0; setup_button_pressed = FALSE; setup_button_time_trigger = 0; setup_button_time_duration = 0; main_mode = MAIN_MODE_INIT; init_internal_wattage_sensor(); init_temperature_sensor(); init_audio_sensor(); init_light_sensor(); uint8_t itor = 0; for (itor = 0; itor < NUMBER_OF_AUX_PORTS; itor++) { aux_sensor[itor] = 0; } // These initialize functions and interrupts init_timer(); init_time(); init_leds(); init_relay(); init_uart(); init_transmits(); init_buttons(); init_roving(&roving_call_back); setup_button_time_trigger = new_time(); setup_button_time_duration = new_time(); time_set_seconds(setup_button_time_duration, 2); _enable_interrupts(); // confirm roving works while (!enter_command_mode()) { reset_roving(); wait(500); } init_adc(); init_pll(); // This is for the check in // If we are disconnected, we wait 5 seconds before trying to reconnect last_connect_time = new_time(); check_in_period = new_time(); time_set_seconds(check_in_period, 5); // And go!!! set_led_anim(led_start); // MAIN LOOP while(1) { handle_roving_input(); // Check if it a long hold if(setup_button_pressed) { // If the button is pressed down if(time_cmp(global_time(), setup_button_time_trigger) >= 0) { // If enough time has passed if(in_setup_mode()) { leave_setup_mode(); } else { start_setup_mode(); } // Only want to check this once setup_button_pressed = FALSE; } } // MAIN BEHAVIOR // There is setup_mode and main_mode. // Setup mode is for configuring WiFi SSID and PASS // Main mode is for sampling if (in_setup_mode()) { if(main_mode != MAIN_MODE_SETUP) { stop_sampling(); set_led_anim(led_setup_start); main_mode = MAIN_MODE_SETUP; } do_setup(); } else if (in_main_mode()) { //regular mode if(main_mode != MAIN_MODE_SAMPLE) { set_led_anim(led_main_start); start_sampling(); main_mode = MAIN_MODE_SAMPLE; } if(!is_associated()) { associate(); } else if (!have_dhcp()) { get_dhcp(); } else { if(is_connected()) set_led_anim(led_main_connected); else set_led_anim(led_main_assoc); if(!is_connected() && (time_cmp(global_time(), last_connect_time) >= 0) ) { // If we are not connected, and enough time has passed, connect connect(); add_time_to_time(last_connect_time, check_in_period); } if(server_wants_header) { led_ping(); exit_command_mode(); transmit_header(); wait(100); } if(okay_to_transmit && have_data_to_transmit()) { led_ping(); exit_command_mode(); transmit_data(); } } } else { main_mode = MAIN_MODE_INIT; set_led_anim(led_error); stop_sampling(); } } }