Ejemplo n.º 1
0
static void pairing(void)
{
    enter_command_mode();
    SEND_COMMAND("SR,Z\r\n");   // remove remote address
    SEND_COMMAND("R,1\r\n");    // reboot
    exit_command_mode();
}
Ejemplo n.º 2
0
static void restore_link(const uint8_t *eeaddr)
{
    enter_command_mode();
    SEND_COMMAND("SR,Z\r\n");   // remove remote address
    SEND_STR("SR,");            // set remote address from EEPROM
    for (int i = 0; i < 12; i++) {
        uint8_t c = eeprom_read_byte(eeaddr+i);
        rn42_putc(c);
        dprintf("%c ", c);
    }
    dprintf("\r\n");
    SEND_COMMAND("\r\n");
    SEND_COMMAND("R,1\r\n");    // reboot
    exit_command_mode();
}
Ejemplo n.º 3
0
static void init_rn42(void)
{
    // RN-42 configure
    if (!config_mode) enter_command_mode();
    SEND_COMMAND("SF,1\r\n");  // factory defaults
    SEND_COMMAND("S-,TmkBT\r\n");
    SEND_COMMAND("SS,Keyboard/Mouse\r\n");
    SEND_COMMAND("SM,4\r\n");  // auto connect(DTR)
    SEND_COMMAND("SW,8000\r\n");   // Sniff disable
    SEND_COMMAND("S~,6\r\n");   // HID profile
    SEND_COMMAND("SH,003C\r\n");   // combo device, out-report, 4-reconnect
    SEND_COMMAND("SY,FFF4\r\n");   // transmit power -12
    SEND_COMMAND("R,1\r\n");
    if (!config_mode) exit_command_mode();
}
Ejemplo n.º 4
0
static void store_link(uint8_t *eeaddr)
{
    enter_command_mode();
    SEND_STR("GR\r\n"); // remote address
    const char *s = rn42_gets(500);
    if (strcmp("GR", s) == 0) s = rn42_gets(500);   // ignore local echo
    xprintf("%s(%d)\r\n", s, strlen(s));
    if (strlen(s) == 12) {
        for (int i = 0; i < 12; i++) {
            eeprom_write_byte(eeaddr+i, *(s+i));
            dprintf("%c ", *(s+i));
        }
        dprint("\r\n");
    }
    exit_command_mode();
}
Ejemplo n.º 5
0
static void init_rn42(void)
{
    // RN-42 configure
    // Should probably put in a baud rate setting..? Maybe?
    // only remembering 4 pairings?
    if (!config_mode) enter_command_mode();
    SEND_COMMAND("SF,1\r\n");  // factory defaults
    SEND_COMMAND("S-,TmkBT\r\n"); //sets the serialized "friendly name"
    SEND_COMMAND("SS,Keyboard/Mouse\r\n"); // sets "service name"
    SEND_COMMAND("SM,4\r\n");  // auto connect(DTR) - use gpio 6 to make and break connections
    SEND_COMMAND("SW,8000\r\n");   // Sniff disable - deep sleep setting READ MORE
    SEND_COMMAND("S~,6\r\n");   // HID profile
    SEND_COMMAND("SH,003C\r\n");   // combo device, out-report, 4-reconnect. binary: 111100
    SEND_COMMAND("SY,FFF4\r\n");   // transmit power -12
    SEND_COMMAND("R,1\r\n");	// reboot module
    if (!config_mode) exit_command_mode();
}
Ejemplo n.º 6
0
bool command_extra(uint8_t code)
{
    uint32_t t;
    uint16_t b;
    switch (code) {
        case KC_H:
        case KC_SLASH: /* ? */
            print("\n\n----- Bluetooth RN-42 Help -----\n");
            print("i:       RN-42 info\n");
            print("b:       battery voltage\n");
            print("Del:     enter/exit RN-42 config mode\n");
            print("Slck:    RN-42 initialize\n");

            print("1-4:     restore link\n");
            print("F1-F4:   store link\n");

            print("p:       pairing\n");

            if (config_mode) {
                return true;
            } else {
                print("u:       toggle Force USB mode\n");
                return false;   // to display default command help
            }
        case KC_P:
            pairing();
            return true;

        /* Store link address to EEPROM */
        case KC_F1:
            store_link(RN42_LINK0);
            return true;
        case KC_F2:
            store_link(RN42_LINK1);
            return true;
        case KC_F3:
            store_link(RN42_LINK2);
            return true;
        case KC_F4:
            store_link(RN42_LINK3);
            return true;
        /* Restore link address to EEPROM */
        case KC_1:
            restore_link(RN42_LINK0);
            return true;
        case KC_2:
            restore_link(RN42_LINK1);
            return true;
        case KC_3:
            restore_link(RN42_LINK2);
            return true;
        case KC_4:
            restore_link(RN42_LINK3);
            return true;
	    
        case KC_5:
	  xprintf("blah! \n");
            //rn42_cts_hi(); 
	    if (stay_connected == 1){
	      dprintf("Disconnect after 5 min.\n");		
	      stay_connected = !stay_connected;
	    }else{
	      dprintf("Stay connected.\n");
	      stay_connected = 1;
	      //last_press_timer = timer_read32();
	    }
	    return true;

        case KC_6:
            clear_keyboard();
            host_set_driver(&rn42_config_driver);   // null driver; not to send a key to host
            rn42_disconnect();
            return true;	    

        case KC_I:
            print("\n----- RN-42 info -----\n");
            xprintf("protocol: %s\n", (host_get_driver() == &rn42_driver) ? "RN-42" : "LUFA");
            xprintf("force_usb: %X\n", force_usb);
            xprintf("rn42: %s\n", rn42_rts() ? "OFF" : (rn42_linked() ? "CONN" : "ON"));
            xprintf("rn42_autoconnecting(): %X\n", rn42_autoconnecting());
            xprintf("config_mode: %X\n", config_mode);
            xprintf("USB State: %s\n",
                    (USB_DeviceState == DEVICE_STATE_Unattached) ? "Unattached" :
                    (USB_DeviceState == DEVICE_STATE_Powered) ? "Powered" :
                    (USB_DeviceState == DEVICE_STATE_Default) ? "Default" :
                    (USB_DeviceState == DEVICE_STATE_Addressed) ? "Addressed" :
                    (USB_DeviceState == DEVICE_STATE_Configured) ? "Configured" :
                    (USB_DeviceState == DEVICE_STATE_Suspended) ? "Suspended" : "?");
            xprintf("battery: ");
            switch (battery_status()) {
                case FULL_CHARGED:  xprintf("FULL"); break;
                case CHARGING:      xprintf("CHARG"); break;
                case DISCHARGING:   xprintf("DISCHG"); break;
                case LOW_VOLTAGE:   xprintf("LOW"); break;
                default:            xprintf("?"); break;
            };
            xprintf("\n");
            xprintf("RemoteWakeupEnabled: %X\n", USB_Device_RemoteWakeupEnabled);
            xprintf("VBUS: %X\n", USBSTA&(1<<VBUS));
            t = timer_read32()/1000;
            uint8_t d = t/3600/24;
            uint8_t h = t/3600;
            uint8_t m = t%3600/60;
            uint8_t s = t%60;
            xprintf("uptime: %02u %02u:%02u:%02u\n", d, h, m, s);

            xprintf("LINK0: %s\r\n", get_link(RN42_LINK0));
            xprintf("LINK1: %s\r\n", get_link(RN42_LINK1));
            xprintf("LINK2: %s\r\n", get_link(RN42_LINK2));
            xprintf("LINK3: %s\r\n", get_link(RN42_LINK3));

            return true;
        case KC_B:
            // battery monitor
            t = timer_read32()/1000;
            b = battery_voltage();
            xprintf("BAT: %umV\t", b);
            xprintf("%02u:",   t/3600);
            xprintf("%02u:",   t%3600/60);
            xprintf("%02u\n",  t%60);
            return true;
        case KC_U:
            if (config_mode) return false;
            if (force_usb) {
                print("Auto mode\n");
                force_usb = false;
            } else {
                print("USB mode\n");                
                clear_keyboard();
                host_set_driver(&rn42_driver);
            }
            return true;
        case KC_DELETE:
            /* RN-42 Command mode */
            if (rn42_autoconnecting()) {
                enter_command_mode();

                command_state = CONSOLE;
                config_mode = true;
            } else {
                exit_command_mode();

                command_state = ONESHOT;
                config_mode = false;
            }
            return true;
        case KC_SCROLLLOCK:
            init_rn42();
            return true;
        default:
            if (config_mode)
                return true;
            else
                return false;   // yield to default command
    }
    return true;
}
Ejemplo n.º 7
0
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();
		}
	}
}