// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // Your setup code here // CThread t(tskBlink); t.start("blink"); // start the blink task CPin led0(LED_PIN_0); // declare led0 on p0.18 led0.output(); // // Enter main loop. // while(1) { // // Your loop code here // led0.toggle(); // toggle the led0 sleep(500); } }
// // main task // int main(void) { /************************************************************************* * * your setup code here * **************************************************************************/ // // LED Demo (can be removed) // DBG("Hello I'm in debug mode\n"); CBus leds(LED1, LED2, LED3, LED4, END); leds.output(); // set all pins as output usbCDC usb; usb.enable(); // enable USB core CSerial uart; uart.enable(115200); // enable serial port int i = 0; uint8_t ch; while(1) { /********************************************************************** * * your loop code here * **********************************************************************/ // // check uart input // if ( uart.readable() ) { // // // show led scripts leds = led_scripts[i]; i = (i+1) < (int)sizeof(led_scripts) ? i+1 : 0; usb << uart; // use CStream operator << } // // check usb input // if ( usb.readable() ) { ch = usb; // use CStream operator uint8_t uart << ch; // use CStream operator << } } return 0 ; }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // PWM (Using Timer1) // hwPWM pwm1(TIMER_1, 5, 6, 7); // set pwm1 pins on P0.5 (CH1), P0.6 (CH2) and P0.7 (CH3) pwm1.period(0.0002); // period time = 200us pwm1.enable(); // enable PWM module // update pwm2 channels duty-cycle (can be updated in any-time) pwm1.dutycycle(PWM_CH_1, 0.8f); // CH1 duty-cycle = 80% pwm1.dutycycle(PWM_CH_2, 0.6f); // CH2 duty-cycle = 60% pwm1.dutycycle(PWM_CH_3, 0.2f); // CH3 duty-cycle = 20% // // PWM (Using Timer2) // hwPWM pwm2(TIMER_2, LED_PIN_1, LED_PIN_2); // set pwm2 pins on LED1 (CH1) and LED2 pwm2.period(0.0005); // period time = 500us pwm2.enable(); // enable PWM module // update pwm2 channels duty-cycle (can be updated in any-time) pwm2.dutycycle(PWM_CH_1, 0.8f); // CH1 duty-cycle = 80% pwm2.dutycycle(PWM_CH_2, 0.1f); // CH2 duty-cycle = 10% // // LED // CPin led(LED_PIN_0); led.output(); CTimeout tmLED; // // Enter main loop. // while(1) { // // FireFly loop // if ( tmLED.isExpired(500) ) { tmLED.reset(); led.toggle(); } } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Your setup code here // CButton btn(BUTTON_PIN_0); CBuzzer buz(15); // buzzer on P0.15 buz.enable(); CPin led(LED_PIN_0); led.output(); CTimeout tmLED; // // Enter main loop. // while(1) { // // Your loop code here // switch(btn.isPressed()) { case BTN_PRESSED: buz.post(3); // turn on buzzer x 3 break; case BTN_RELEASED: break; case BTN_NOTHING: break; } if ( tmLED.isExpired(500) ) { tmLED.reset(); led.toggle(); } } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // task 1 // static const SENSE_PARAM_T sense_t1 = {BUTTON_PIN_0, FALLING, LED_PIN_0}; // task parameters, Sense=P0.16, LED=LED0 CThread t1(senseTask, (xHandle) &sense_t1); t1.start("t1", 62, PRI_HARDWARE); // // task 2 // static const SENSE_PARAM_T sense_t2 = {BUTTON_PIN_1, TOGGLE, LED_PIN_1}; // task parameters, Sense=P0.17, LED=LED1 CThread t2(senseTask, (xHandle) &sense_t2); t2.start("t2", 62, PRI_HARDWARE); // // test pin // CPin test(15); test.output(); // // Enter main loop. // while(1) { // // Your loop code here // test.toggle(); // Use wire to connect the test pin to sense pin for test. } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Your setup code here // // m_semButton.counting(2, 2); m_semButton.binary(); CThread t1(tskLED1); t1.start("t1"); CThread t2(tskLED2); t2.start("t2"); // button CButton btn(BUTTON_PIN_0); // // Enter main loop. // while(1) { // // Your loop code here // if ( btn.isPressed()==BTN_PRESSED ) { m_semButton.release(); // signal the semaphore } } }
// // main routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // Declare SoftDevice Object // bleDevice ble; ble.enable(); // enable BLE stack // GAP ble.m_gap.settings(DEVICE_NAME); // set Device Name on GAP ble.m_gap.tx_power(BLE_TX_0dBm); // set output radio Power // // Declare a HRM service object // bleServiceHRM hrm(ble); // // Declare a HTM service object // bleServiceHTM htm(ble); // // Declare a BAT service object // bleServiceBattery bat(ble); // // Add "connection parameters update" negotiation. (optional) // bleConnParams conn(ble); // // update advertisement contents // ble.m_advertising.commpany_identifier(APP_COMPANY_IDENTIFIER); // add company identifier ble.m_advertising.add_uuid_to_complete_list(hrm); // add hrm object to the uuid list of advertisement ble.m_advertising.add_uuid_to_complete_list(htm); // add htm object to the uuid_list of advertisement ble.m_advertising.add_uuid_to_complete_list(bat); // add bat object to the uuid_list of advertisement ble.m_advertising.appearance(BLE_APPEARANCE_GENERIC_HEART_RATE_SENSOR); ble.m_advertising.update(); // update advertisement data // Start advertising ble.m_advertising.interval(APP_ADV_INTERVAL); // set advertising interval // ble.m_advertising.timeout(30); // set timeout, will cause the system to enter to the power off mode. ble.m_advertising.start(); // // Tickless Low Power Feature // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Analog input (for VDD detected) // CAdc::init(); // AREF internal VBG CAdc::source(VDD_1_3); // source from internal: VDD x 1/3 CAdc::enable(); // // LED Task // CThread t(tskLED); t.start("LED", 45); // stack size=45 DWORD // uint16_t value; float temp, percentage; // // Enter main loop. // while(1) { // // wait for BLE connected, the task will be blocked until connected. // ble.wait(); // // Heart Rate Measurement Service // if ( hrm.isAvailable() ) { // is service available (when remote app connected to the service) hrm.send(70); // send Heart Rate Measurement } // // Health Thermometer Measurement Service // if ( htm.isAvailable() ) { // is service available (when remote app connected to the service) if ( bleServiceHTM::get_temperature(temp) ) { htm.send(temp); // send temp } } // // Battery Service // if ( bat.isAvailable() ) { // is service available (when remote app connected to the service) if ( CAdc::read(value) ) { percentage = (value / 1024.0f) * 100.0; bat.send(percentage); } } // Negotiate the "connection parameters update" conn.negotiate(); // sleep to make more idle time for tickless. sleep(200); } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // buffer // uint8_t *ciphertext = new uint8_t[sizeof(plaintext)]; uint8_t *cleartext = new uint8_t[sizeof(plaintext)]; // // A private key for internal used. // use the same key in encrypt and decrypt objects. aesCTR encryptCTR(private_key); aesCTR decryptCTR(private_key); aesCFB encryptCFB(private_key, AES_ENCRYPT); aesCFB decryptCFB(private_key, AES_DECRYPT); uint32_t i; // create new random nonce block (public key) // assign the nonce block to decrypt object. it seem to exchange a random public key. decryptCTR = encryptCTR.new_nonce(); // create new random IV block (public key) decryptCFB = encryptCFB.new_IV(); // // Enter main loop. // while(1) { // // Debug the AES result // if ( dbg.available() && dbg.read()!=0x1B) { // // AES CTR // DBG("\n\nAES CTR Mode:\n"); // encryption DBG("encryption:"); encryptCTR.crypt(ciphertext, plaintext, sizeof(plaintext)); for (i=0; i<sizeof(plaintext); i++) { DBG("%02X ", ciphertext[i]); // display the ciphertext contents } // decryption DBG("\ndecryption:"); decryptCTR.crypt(cleartext, ciphertext, sizeof(plaintext)); for (i=0; i<sizeof(plaintext); i++) { DBG("%02X ", cleartext[i]); // display the cleartext contents } // // AES CFB // DBG("\n\nAES CFB Mode:\n"); // encryption DBG("encryption:"); encryptCFB.crypt(ciphertext, plaintext, sizeof(plaintext)); for (i=0; i<sizeof(plaintext); i++) { DBG("%02X ", ciphertext[i]); // display the ciphertext contents } // decryption DBG("\ndecryption:"); decryptCFB.crypt(cleartext, ciphertext, sizeof(plaintext)); for (i=0; i<sizeof(plaintext); i++) { DBG("%02X ", cleartext[i]); // display the cleartext contents } } sleep(100); // give idle time for DFU button check. } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // SoftDevice // bleDevice ble; ble.enable("Shutter", "1234"); // enable BLE SoftDevice task // Pin to clear all center list CPin btn(17); btn.input(); // Device Manager for bond device. bleDeviceManager man(ble, (btn==LOW ? true : false)); man.connect_directed_mode(); // enable "connect directed" mode to fast connected. (BT 4.1 spec.) // GAP ble.m_gap.settings( DEVICE_NAME, 20, // min. connection interval in millisecond 100, // max. connection interval in millisecond 12); // slave latency (save power) ble.m_gap.tx_power(BLE_TX_0dBm); // // HID Keyboard Service // bleServiceKB kbs(ble); // // Battery Level Service // bleServiceBattery bat(ble); // // Optional: Add "Connection Parameters Update" negotiation. // bleConnParams conn(ble); // // BLE Advertising // ble.m_advertising.add_uuid_to_complete_list(bat); // add bat object to the uuid list of advertising ble.m_advertising.add_uuid_to_complete_list(kbs); // add kbs object to the uuid list of advertising // Optional: add appearance to indicate who you are in advertisement. ble.m_advertising.appearance(BLE_APPEARANCE_HID_KEYBOARD); ble.m_advertising.update(); // update advertisement data // Start advertising ble.m_advertising.interval(APP_ADV_INTERVAL); // set advertising interval ble.m_advertising.timeout(30); // Enter system power off mode when adv. timeout. (disable system off if timeout=0) ble.m_advertising.start(); // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Key Test Task // CThread t(keyTask); t.start("kb", 76); // start the keyTask // // Analog // CAdc::init(); CAdc::source(VDD_1_3); // to detect the VDD voltage CAdc::enable(); // // LED // CPin led(LED_PIN_0); led.output(); CTimeout tmBAT, tmLED; uint16_t value; float percentage; // // Enter main loop. // while(1) { // // BLE Battery Service // if ( bat.isAvailable() ) { if ( tmBAT.isExpired(1000) ) { tmBAT.reset(); if ( CAdc::read(value) ) { percentage = (value / 1024.0f) * 100; bat.send(percentage); } } } // // blink led // led = LED_ON; sleep(10); led = LED_OFF; if ( ble.isConnected() ) { sleep(290); } else { sleep(1990); } // Negotiate the "connection parameters update" in main-loop conn.negotiate(); } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // SoftDevice // bleDevice ble; ble.enable(); // enable BLE SoftDevice task // Device Manager for bond device. (Optional, for bond link device only) // bleDeviceManager man(ble); // GAP ble.m_gap.settings(DEVICE_NAME); // set Device Name on GAP ble.m_gap.tx_power(BLE_TX_0dBm); // // TODO: Add BLE Service // // // Optional: Add "Connection Parameters Update" negotiation. // bleConnParams conn(ble); // // BLE Advertising // ble.m_advertising.interval(APP_ADV_INTERVAL); // set advertising interval ble.m_advertising.commpany_identifier(APP_COMPANY_IDENTIFIER); // add company identifier // Optional: add standard profile uuid in advertisement. // Optional: add appearance to indicate who you are in advertisement. ble.m_advertising.update(); // update advertisement data // Start advertising ble.m_advertising.start(); // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Your Application setup code here // CPin led(LED_PIN_0); led.output(); // // Enter main loop. // while(1) { // // Negotiate the "connection parameters update" in main-loop // conn.negotiate(); // // Your loop code here // led.invert(); } }
int main(void) { #ifdef DEBUG CSerial ser; ser.enable(); CDebug dbg(ser); dbg.start(); #endif // // SoftDevice Driver // bleDevice ble; ble.enable(); // enable BLE stack // // Load from Storage (Flash memory) // CStorage sgBeacon(sizeof(m_beacon)); m_p_sgBeacon = &sgBeacon; // load beacon data from storage and initialize. sgBeacon.read(&m_beacon, sizeof(m_beacon)); beacon_data_init(); // // GAP // ble.m_gap.tx_power(m_beacon.tx_power); // set TX radio power // // BLE to UART service // bleServiceUART nus(ble); // // Connection Parameters Update Negotiation // bleConnParams conn(ble); // // Advertisement // // ble.m_advertising.type(ADV_TYPE_ADV_NONCONN_IND); ble.m_advertising.name_type(BLE_ADVDATA_NO_NAME); // set beacon name type (No Name) ble.m_advertising.commpany_identifier(m_beacon.company_id); ble.m_advertising.manuf_specific_data((uint8_t *)&m_beacon.data, APP_BEACON_INFO_LENGTH); // set beacon data ble.m_advertising.update(BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED); // update advertising data ble.m_advertising.interval(m_beacon.interval); // set advertising interval ble.m_advertising.start(); // // LED pin declared // CPin led(LED_PIN_0); led.output(); // // AT Command Parse // atCommand at(nus); // set the nus stream to atCommand object. at.attachHandle(at_command_handle); // set the AT command handle function. // // Enable Tickless Technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Enter main loop. // while(1) { // // AT Command parse // at.parse(); // // blink led // led = LED_ON; sleep(10); // turn on with a short time (10ms) led = LED_OFF; if ( ble.isConnected() ) { sleep(190); } else { sleep(2990); // sleep with a long time (2990ms) } // Negotiate "CP" conn.negotiate(); } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a UART object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // SoftDevice // bleDevice ble; ble.attachEvent(onBleEvent); ble.enable(); // enable BLE SoftDevice task // GAP ble.m_gap.settings(DEVICE_NAME); // set Device Name on GAP bleServiceUriBeacon beacon(ble); // add uri beacon service switch ( beacon.get().tx_power_mode ) { case TX_POWER_MODE_LOWSET: ble.m_gap.tx_power(BLE_TX_m8dBm); break; case TX_POWER_MODE_LOW: ble.m_gap.tx_power(BLE_TX_m4dBm); break; case TX_POWER_MODE_MEDIUM: ble.m_gap.tx_power(BLE_TX_0dBm); break; case TX_POWER_MODE_HIGH: ble.m_gap.tx_power(BLE_TX_4dBm); break; } bleConnParams conn(ble); advertising_init(beacon_mode); // // Optional: Enable tickless technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // // CPin led0(LED_PIN_0); led0.output(); CPin led1(LED_PIN_1); led1.output(); CPin btn(BUTTON_PIN_0); btn.input(); // // Enter main loop. // while (1) { // // check button // if ( btn==LOW ) { led1 = LED_ON; // stop advertising ble.m_advertising.stop(); // change beacon mode if ( beacon_mode==beacon_mode_config ) { beacon_mode = beacon_mode_normal; } else { beacon_mode = beacon_mode_config; } // update mode and re-start the advertising advertising_init(beacon_mode); // waiting for btn released while(btn==LOW); led1 = LED_OFF; } // // LED Status // if ( beacon_mode==beacon_mode_config ) { // // Negotiate the "connection parameters update" in main-loop // conn.negotiate(); led0 = LED_ON; sleep(50); led0 = LED_OFF; sleep(150); } else { led0 = LED_ON; sleep(5); led0 = LED_OFF; sleep(beacon.get().beacon_period-5); } } }
// // Main Routine // int main(void) { #ifdef DEBUG CSerial ser; // declare a Serial object ser.enable(); CDebug dbg(ser); // Debug stream use the UART object dbg.start(); #endif // // SoftDevice // bleDevice ble; ble.enable(); // enable BLE SoftDevice stack // GAP ble.m_gap.settings(DEVICE_NAME, 10, 50); // set Device Name on GAP, conn. interval min=10ms, max=50ms ble.m_gap.tx_power(BLE_TX_0dBm); // set Output power // // Add BLE UART Service // bleServiceUART nus(ble); // declare a BLE "Nordic UART Service" (NUS) object // // Add "connection parameters update" negotiation. (optional) // bleConnParams conn(ble); // // BLE Advertising // ble.m_advertising.commpany_identifier(APP_COMPANY_IDENTIFIER);// add company identifier ble.m_advertising.update(); // update advertising data // Start advertising ble.m_advertising.interval(APP_ADV_INTERVAL); // set advertising interval ble.m_advertising.start(); // // my command parse class // cmdParse cmd; // // LED output enable // ledLeft.output(); // set ledLeft as an output pin ledRight.output(); // set ledRight as an output pin uint8_t ch= 0; // // Enable Tickless Technology // #ifndef DEBUG CPowerSave::tickless(true); #endif // // Enter main loop. // while (1) { // // Uart Service // if ( ble.isConnected() ) { // block in the read() to wait a char. // Also, block task will save the power when tickless enabled. while ( nus.readable() ) { ch = nus.read(); if ( ch ) { cmd.input(ch); } } } else { // // alternate led when disconnected (idle) // ch = (ch ? 0 : 1); ledRight = (ch ? LED_ON : LED_OFF); ledLeft = (ch ? LED_OFF : LED_ON); sleep(10); // blink a short time (10ms) ledRight = LED_OFF; ledLeft = LED_OFF; sleep(990); // save power with a long time sleep (990ms) } // Negotiate the "Connect Parameters Update" conn.negotiate(); } }