啃萝卜代码高亮测试-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3568|回复: 3

啃萝卜代码高亮测试

[复制链接]
发表于 2017-2-15 14:23 | 显示全部楼层 |阅读模式
测试
[kenrobot_code]/*
#include <CurieBLE.h>
BLEService batteryService("180F"); // BLE Battery Service
// BLE Battery Level Characteristic"
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);   // standard 16-bit characteristic UUID  defined in the URL above
                                                                               // remote clients will be able to get notifications if this characteristic changes
int oldBatteryLevel = 0;  // last battery level reading from analog input
long previousMillis = 0;  // last time the battery level was checked, in ms

void setup() {
  BLE.begin();
  Serial.begin(9600);    // initialize serial communication
  pinMode(13, OUTPUT);   // initialize the LED on pin 13 to indicate when a central is connected

  /* Set a local name for the BLE device
     This name will appear in advertising packets
     and can be used by remote devices to identify this BLE device
     The name can be changed but maybe be truncated based on space left in advertisement packet
     If you want to make this work with the BatteryMonitor_Central sketch, do not modufy the name.
  */
  BLE.setLocalName("BatteryMonitorSketch");
  BLE.setAdvertisedServiceUuid(batteryService.uuid());  // add the service UUID
  BLE.addService(batteryService);   // Add the BLE Battery service
  batteryService.addCharacteristic(batteryLevelChar); // add the battery level characteristic
  batteryLevelChar.setValue(oldBatteryLevel);   // initial value for this characteristic

  /* Now activate the BLE device.  It will start continuously transmitting BLE
     advertising packets and will be visible to remote BLE central devices
     until it receives a new connection
  */

  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
  // listen for BLE peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());
    // turn on the LED to indicate the connection:
    digitalWrite(13, HIGH);

    // check the battery level every 200ms
    // as long as the central is still connected:
    while (central.connected()) {
      long currentMillis = millis();
      // if 200ms have passed, check the battery level:
      if (currentMillis - previousMillis >= 200) {
        previousMillis = currentMillis;
        updateBatteryLevel();

        static unsigned short count = 0;
        count++;
        // update the connection interval
        if (count % 5 == 0) {
          delay(1000);
          updateIntervalParams(central);
        }
      }
    }
    // when the central disconnects, turn off the LED:
    digitalWrite(13, LOW);
    Serial.print("Disconnected from central: ");
    Serial.println(central.address());
  }
}

void updateBatteryLevel() {
  /* Read the current voltage level on the A0 analog input pin.
     This is used here to simulate the charge level of a battery.
  */
  int battery = analogRead(A0);
  int batteryLevel = map(battery, 0, 1023, 0, 100);

  if (batteryLevel != oldBatteryLevel) {      // if the battery level has changed
    Serial.print("Battery Level % is now: "); // print it
    Serial.println(batteryLevel);
    batteryLevelChar.writeUnsignedChar(batteryLevel);  // and update the battery level characteristic
    oldBatteryLevel = batteryLevel;           // save the level for next comparison
  }
}

void updateIntervalParams(BLEDevice central) {
  // read and update the connection interval that peer central device
  static unsigned short interval = 0x60;
  ble_conn_param_t m_conn_param;
  // Get connection interval that peer central device wanted
  //central.getConnParams(m_conn_param);
  Serial.print("min interval = " );
  Serial.println(m_conn_param.interval_min );
  Serial.print("max interval = " );
  Serial.println(m_conn_param.interval_max );
  Serial.print("latency = " );
  Serial.println(m_conn_param.latency );
  Serial.print("timeout = " );
  Serial.println(m_conn_param.timeout );

  //Update connection interval
  Serial.println("set Connection Interval");
  central.setConnectionInterval(interval, interval);

  interval++;
  if (interval < 0x06)
    interval = 0x06;
  if (interval > 0x100)
    interval = 0x06;
}[/kenrobot_code]


发表于 2017-2-16 18:31 | 显示全部楼层
[mw_shl_code=cpp,true]/*
  Arduino BLE Peripheral LED example
  Copyright (c) 2016 Arduino LLC. All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <CurieBLE.h>

// LED pin
#define LED_PIN   13

// create service
BLEService               ledService("19b10000e8f2537e4f6cd104768a1214");

// create switch characteristic
BLECharCharacteristic    switchCharacteristic("19b10001e8f2537e4f6cd104768a1214", BLERead | BLEWrite);

BLEDescriptor            switchDescriptor("2901", "switch");

void setup() {
  Serial.begin(9600);

  // set LED pin to output mode
  pinMode(LED_PIN, OUTPUT);

  // begin initialization
  BLE.begin();
  Serial.println(BLE.address());

  // set advertised local name and service UUID
  BLE.setLocalName("LED");
  BLE.setAdvertisedServiceUuid(ledService.uuid());

  switchCharacteristic.addDescriptor(switchDescriptor);
  ledService.addCharacteristic(switchCharacteristic);

  // add service and characteristic
  BLE.addService(ledService);

  BLE.advertise();

  Serial.println(F("BLE LED Peripheral"));
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {
    // central connected to peripheral
    Serial.print(F("Connected to central: "));
    Serial.println(central.address());

    while (central.connected()) {
      // central still connected to peripheral
      if (switchCharacteristic.written()) {
        // central wrote new value to characteristic, update LED
        if (switchCharacteristic.value()) {
          Serial.println(F("LED on"));
          digitalWrite(LED_PIN, HIGH);
        } else {
          Serial.println(F("LED off"));
          digitalWrite(LED_PIN, LOW);
        }
      }
    }

    // central disconnected
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}[/mw_shl_code]
发表于 2017-2-16 18:35 | 显示全部楼层
[kenrobot_code]/**
* 日期: 2017/02/16
* 作者:
* 描述:
*/

void setup()
{
   
}

void loop()
{
   
}
[/kenrobot_code]
发表于 2017-2-16 18:36 | 显示全部楼层
[kenrobot_code]/**
* 日期: 2017/02/16
* 作者:
* 描述:
*/

void setup()
{
    pinMode(1,INPUT)
}

void loop()
{
   
}
[/kenrobot_code]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-28 01:05 , Processed in 0.083180 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表