Knight Rider PIO sample for teckBASIC, konashi.js

Dependencies:   BLE_API_Native_IRC mbed

Fork of BLE_konashi_PIO_test by Michio Ono

konashi.js mbed HRM1017でもナイトライダー!: http://jsdo.it/micutil/g1Hn

サンプル動画: https://www.youtube.com/watch?v=HSLdzS3sGLw

techBASICサンプル: https://www.dropbox.com/s/c1k25jlen7x3vqo/KnightRider.txt

Committer:
micono
Date:
Sun Jul 27 00:02:25 2014 +0000
Revision:
2:6a3257fffa8c
Parent:
0:8c643bfe55b7
PIO sample for teckBASIC, konashi.js

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:8c643bfe55b7 1 /* mbed Microcontroller Library
jksoft 0:8c643bfe55b7 2 * Copyright (c) 2006-2013 ARM Limited
jksoft 0:8c643bfe55b7 3 *
jksoft 0:8c643bfe55b7 4 * Licensed under the Apache License, Version 2.0 (the "License");
jksoft 0:8c643bfe55b7 5 * you may not use this file except in compliance with the License.
jksoft 0:8c643bfe55b7 6 * You may obtain a copy of the License at
jksoft 0:8c643bfe55b7 7 *
jksoft 0:8c643bfe55b7 8 * http://www.apache.org/licenses/LICENSE-2.0
jksoft 0:8c643bfe55b7 9 *
jksoft 0:8c643bfe55b7 10 * Unless required by applicable law or agreed to in writing, software
jksoft 0:8c643bfe55b7 11 * distributed under the License is distributed on an "AS IS" BASIS,
jksoft 0:8c643bfe55b7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jksoft 0:8c643bfe55b7 13 * See the License for the specific language governing permissions and
jksoft 0:8c643bfe55b7 14 * limitations under the License.
jksoft 0:8c643bfe55b7 15 */
jksoft 0:8c643bfe55b7 16
jksoft 0:8c643bfe55b7 17 #include <stdio.h>
jksoft 0:8c643bfe55b7 18 #include <string.h>
jksoft 0:8c643bfe55b7 19
jksoft 0:8c643bfe55b7 20 #include "GapAdvertisingData.h"
jksoft 0:8c643bfe55b7 21
jksoft 0:8c643bfe55b7 22 /**************************************************************************/
jksoft 0:8c643bfe55b7 23 /*!
jksoft 0:8c643bfe55b7 24 \brief Creates a new GapAdvertisingData instance
jksoft 0:8c643bfe55b7 25
jksoft 0:8c643bfe55b7 26 \par EXAMPLE
jksoft 0:8c643bfe55b7 27
jksoft 0:8c643bfe55b7 28 \code
jksoft 0:8c643bfe55b7 29
jksoft 0:8c643bfe55b7 30 \endcode
jksoft 0:8c643bfe55b7 31 */
jksoft 0:8c643bfe55b7 32 /**************************************************************************/
jksoft 0:8c643bfe55b7 33 GapAdvertisingData::GapAdvertisingData(void)
jksoft 0:8c643bfe55b7 34 {
jksoft 0:8c643bfe55b7 35 memset(_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
jksoft 0:8c643bfe55b7 36 _payloadLen = 0;
jksoft 0:8c643bfe55b7 37 _appearance = GENERIC_TAG;
jksoft 0:8c643bfe55b7 38 }
jksoft 0:8c643bfe55b7 39
jksoft 0:8c643bfe55b7 40 /**************************************************************************/
jksoft 0:8c643bfe55b7 41 /*!
jksoft 0:8c643bfe55b7 42 Destructor
jksoft 0:8c643bfe55b7 43 */
jksoft 0:8c643bfe55b7 44 /**************************************************************************/
jksoft 0:8c643bfe55b7 45 GapAdvertisingData::~GapAdvertisingData(void)
jksoft 0:8c643bfe55b7 46 {
jksoft 0:8c643bfe55b7 47 }
jksoft 0:8c643bfe55b7 48
jksoft 0:8c643bfe55b7 49 /**************************************************************************/
jksoft 0:8c643bfe55b7 50 /*!
jksoft 0:8c643bfe55b7 51 \brief Adds advertising data based on the specified AD type (see
jksoft 0:8c643bfe55b7 52 DataType)
jksoft 0:8c643bfe55b7 53
jksoft 0:8c643bfe55b7 54 \args[in] advDataType The Advertising 'DataType' to add
jksoft 0:8c643bfe55b7 55 \args[in] payload Pointer to the payload contents
jksoft 0:8c643bfe55b7 56 \args[in] len Size of the payload in bytes
jksoft 0:8c643bfe55b7 57
jksoft 0:8c643bfe55b7 58 \returns ble_error_t
jksoft 0:8c643bfe55b7 59
jksoft 0:8c643bfe55b7 60 \retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 61 Everything executed properly
jksoft 0:8c643bfe55b7 62
jksoft 0:8c643bfe55b7 63 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:8c643bfe55b7 64 The specified data would cause the advertising buffer
jksoft 0:8c643bfe55b7 65 to overflow
jksoft 0:8c643bfe55b7 66
jksoft 0:8c643bfe55b7 67 \par EXAMPLE
jksoft 0:8c643bfe55b7 68
jksoft 0:8c643bfe55b7 69 \code
jksoft 0:8c643bfe55b7 70
jksoft 0:8c643bfe55b7 71 \endcode
jksoft 0:8c643bfe55b7 72 */
jksoft 0:8c643bfe55b7 73 /**************************************************************************/
jksoft 0:8c643bfe55b7 74 ble_error_t GapAdvertisingData::addData(DataType advDataType, uint8_t * payload, uint8_t len)
jksoft 0:8c643bfe55b7 75 {
jksoft 0:8c643bfe55b7 76 /* ToDo: Check if an AD type already exists and if the existing */
jksoft 0:8c643bfe55b7 77 /* value is exclusive or not (flags, etc.) */
jksoft 0:8c643bfe55b7 78
jksoft 0:8c643bfe55b7 79 /* Make sure we don't exceed the 31 byte payload limit */
jksoft 0:8c643bfe55b7 80 if (_payloadLen + len + 2 >= GAP_ADVERTISING_DATA_MAX_PAYLOAD)
jksoft 0:8c643bfe55b7 81 return BLE_ERROR_BUFFER_OVERFLOW;
jksoft 0:8c643bfe55b7 82
jksoft 0:8c643bfe55b7 83 /* Field length */
jksoft 0:8c643bfe55b7 84 memset(&_payload[_payloadLen], len+1, 1);
jksoft 0:8c643bfe55b7 85 _payloadLen++;
jksoft 0:8c643bfe55b7 86
jksoft 0:8c643bfe55b7 87 /* Field ID */
jksoft 0:8c643bfe55b7 88 memset(&_payload[_payloadLen], (uint8_t)advDataType, 1);
jksoft 0:8c643bfe55b7 89 _payloadLen++;
jksoft 0:8c643bfe55b7 90
jksoft 0:8c643bfe55b7 91 /* Payload */
jksoft 0:8c643bfe55b7 92 memcpy(&_payload[_payloadLen], payload, len);
jksoft 0:8c643bfe55b7 93 _payloadLen += len;
jksoft 0:8c643bfe55b7 94
jksoft 0:8c643bfe55b7 95 return BLE_ERROR_NONE;
jksoft 0:8c643bfe55b7 96 }
jksoft 0:8c643bfe55b7 97
jksoft 0:8c643bfe55b7 98 /**************************************************************************/
jksoft 0:8c643bfe55b7 99 /*!
jksoft 0:8c643bfe55b7 100 \brief Helper function to add APPEARANCE data to the advertising
jksoft 0:8c643bfe55b7 101 payload
jksoft 0:8c643bfe55b7 102
jksoft 0:8c643bfe55b7 103 \args[in] appearance The APPEARANCE value to add
jksoft 0:8c643bfe55b7 104
jksoft 0:8c643bfe55b7 105 \returns ble_error_t
jksoft 0:8c643bfe55b7 106
jksoft 0:8c643bfe55b7 107 \retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 108 Everything executed properly
jksoft 0:8c643bfe55b7 109
jksoft 0:8c643bfe55b7 110 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:8c643bfe55b7 111 The specified data would cause the advertising buffer
jksoft 0:8c643bfe55b7 112 to overflow
jksoft 0:8c643bfe55b7 113
jksoft 0:8c643bfe55b7 114 \par EXAMPLE
jksoft 0:8c643bfe55b7 115
jksoft 0:8c643bfe55b7 116 \code
jksoft 0:8c643bfe55b7 117
jksoft 0:8c643bfe55b7 118 \endcode
jksoft 0:8c643bfe55b7 119 */
jksoft 0:8c643bfe55b7 120 /**************************************************************************/
jksoft 0:8c643bfe55b7 121 ble_error_t GapAdvertisingData::addAppearance(Appearance appearance)
jksoft 0:8c643bfe55b7 122 {
jksoft 0:8c643bfe55b7 123 _appearance = appearance;
jksoft 0:8c643bfe55b7 124 return addData(GapAdvertisingData::APPEARANCE, (uint8_t*)&appearance, 2);
jksoft 0:8c643bfe55b7 125 }
jksoft 0:8c643bfe55b7 126
jksoft 0:8c643bfe55b7 127 /**************************************************************************/
jksoft 0:8c643bfe55b7 128 /*!
jksoft 0:8c643bfe55b7 129 \brief Helper function to add FLAGS data to the advertising
jksoft 0:8c643bfe55b7 130 payload
jksoft 0:8c643bfe55b7 131
jksoft 0:8c643bfe55b7 132 \args[in] flag The FLAGS value to add
jksoft 0:8c643bfe55b7 133
jksoft 0:8c643bfe55b7 134 \par LE_LIMITED_DISCOVERABLE
jksoft 0:8c643bfe55b7 135 The peripheral is discoverable for a limited period of
jksoft 0:8c643bfe55b7 136 time
jksoft 0:8c643bfe55b7 137
jksoft 0:8c643bfe55b7 138 \par LE_GENERAL_DISCOVERABLE
jksoft 0:8c643bfe55b7 139 The peripheral is permanently discoverable
jksoft 0:8c643bfe55b7 140
jksoft 0:8c643bfe55b7 141 \par BREDR_NOT_SUPPORTED
jksoft 0:8c643bfe55b7 142 This peripheral is a Bluetooth Low Energy only device
jksoft 0:8c643bfe55b7 143 (no EDR support)
jksoft 0:8c643bfe55b7 144
jksoft 0:8c643bfe55b7 145 \returns ble_error_t
jksoft 0:8c643bfe55b7 146
jksoft 0:8c643bfe55b7 147 \retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 148 Everything executed properly
jksoft 0:8c643bfe55b7 149
jksoft 0:8c643bfe55b7 150 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:8c643bfe55b7 151 The specified data would cause the advertising buffer
jksoft 0:8c643bfe55b7 152 to overflow
jksoft 0:8c643bfe55b7 153
jksoft 0:8c643bfe55b7 154 \par EXAMPLE
jksoft 0:8c643bfe55b7 155
jksoft 0:8c643bfe55b7 156 \code
jksoft 0:8c643bfe55b7 157
jksoft 0:8c643bfe55b7 158 \endcode
jksoft 0:8c643bfe55b7 159 */
jksoft 0:8c643bfe55b7 160 /**************************************************************************/
jksoft 0:8c643bfe55b7 161 ble_error_t GapAdvertisingData::addFlags(Flags flag)
jksoft 0:8c643bfe55b7 162 {
jksoft 0:8c643bfe55b7 163 return addData(GapAdvertisingData::FLAGS, (uint8_t*)&flag, 1);
jksoft 0:8c643bfe55b7 164 }
jksoft 0:8c643bfe55b7 165
jksoft 0:8c643bfe55b7 166 /**************************************************************************/
jksoft 0:8c643bfe55b7 167 /*!
jksoft 0:8c643bfe55b7 168 \brief Helper function to add TX_POWER_LEVEL data to the
jksoft 0:8c643bfe55b7 169 advertising payload
jksoft 0:8c643bfe55b7 170
jksoft 0:8c643bfe55b7 171 \args[in] flag The TX_POWER_LEVEL value to add
jksoft 0:8c643bfe55b7 172
jksoft 0:8c643bfe55b7 173 \returns ble_error_t
jksoft 0:8c643bfe55b7 174
jksoft 0:8c643bfe55b7 175 \retval BLE_ERROR_NONE
jksoft 0:8c643bfe55b7 176 Everything executed properly
jksoft 0:8c643bfe55b7 177
jksoft 0:8c643bfe55b7 178 \retval BLE_ERROR_BUFFER_OVERFLOW
jksoft 0:8c643bfe55b7 179 The specified data would cause the advertising buffer
jksoft 0:8c643bfe55b7 180 to overflow
jksoft 0:8c643bfe55b7 181
jksoft 0:8c643bfe55b7 182 \par EXAMPLE
jksoft 0:8c643bfe55b7 183
jksoft 0:8c643bfe55b7 184 \code
jksoft 0:8c643bfe55b7 185
jksoft 0:8c643bfe55b7 186 \endcode
jksoft 0:8c643bfe55b7 187 */
jksoft 0:8c643bfe55b7 188 /**************************************************************************/
jksoft 0:8c643bfe55b7 189 ble_error_t GapAdvertisingData::addTxPower(int8_t txPower)
jksoft 0:8c643bfe55b7 190 {
jksoft 0:8c643bfe55b7 191 /* ToDo: Basic error checking to make sure txPower is in range */
jksoft 0:8c643bfe55b7 192 return addData(GapAdvertisingData::TX_POWER_LEVEL, (uint8_t*)&txPower, 1);
jksoft 0:8c643bfe55b7 193 }
jksoft 0:8c643bfe55b7 194
jksoft 0:8c643bfe55b7 195 /**************************************************************************/
jksoft 0:8c643bfe55b7 196 /*!
jksoft 0:8c643bfe55b7 197 \brief Clears the payload and resets the payload length counter
jksoft 0:8c643bfe55b7 198 */
jksoft 0:8c643bfe55b7 199 /**************************************************************************/
jksoft 0:8c643bfe55b7 200 void GapAdvertisingData::clear(void)
jksoft 0:8c643bfe55b7 201 {
jksoft 0:8c643bfe55b7 202 memset(&_payload, 0, GAP_ADVERTISING_DATA_MAX_PAYLOAD);
jksoft 0:8c643bfe55b7 203 _payloadLen = 0;
jksoft 0:8c643bfe55b7 204 }
jksoft 0:8c643bfe55b7 205
jksoft 0:8c643bfe55b7 206 /**************************************************************************/
jksoft 0:8c643bfe55b7 207 /*!
jksoft 0:8c643bfe55b7 208 \brief Returns a pointer to the the current payload
jksoft 0:8c643bfe55b7 209
jksoft 0:8c643bfe55b7 210 \returns A pointer to the payload
jksoft 0:8c643bfe55b7 211 */
jksoft 0:8c643bfe55b7 212 /**************************************************************************/
jksoft 0:8c643bfe55b7 213 uint8_t * GapAdvertisingData::getPayload(void)
jksoft 0:8c643bfe55b7 214 {
jksoft 0:8c643bfe55b7 215 return (_payloadLen > 0) ? _payload : NULL;
jksoft 0:8c643bfe55b7 216 }
jksoft 0:8c643bfe55b7 217
jksoft 0:8c643bfe55b7 218 /**************************************************************************/
jksoft 0:8c643bfe55b7 219 /*!
jksoft 0:8c643bfe55b7 220 \brief Returns the current payload length (0..31 bytes)
jksoft 0:8c643bfe55b7 221
jksoft 0:8c643bfe55b7 222 \returns The payload length in bytes
jksoft 0:8c643bfe55b7 223 */
jksoft 0:8c643bfe55b7 224 /**************************************************************************/
jksoft 0:8c643bfe55b7 225 uint8_t GapAdvertisingData::getPayloadLen(void)
jksoft 0:8c643bfe55b7 226 {
jksoft 0:8c643bfe55b7 227 return _payloadLen;
jksoft 0:8c643bfe55b7 228 }
jksoft 0:8c643bfe55b7 229
jksoft 0:8c643bfe55b7 230 /**************************************************************************/
jksoft 0:8c643bfe55b7 231 /*!
jksoft 0:8c643bfe55b7 232 \brief Returns the 16-bit appearance value for this device
jksoft 0:8c643bfe55b7 233
jksoft 0:8c643bfe55b7 234 \returns The 16-bit appearance value
jksoft 0:8c643bfe55b7 235 */
jksoft 0:8c643bfe55b7 236 /**************************************************************************/
jksoft 0:8c643bfe55b7 237 uint16_t GapAdvertisingData::getAppearance(void)
jksoft 0:8c643bfe55b7 238 {
jksoft 0:8c643bfe55b7 239 return (uint16_t)_appearance;
jksoft 0:8c643bfe55b7 240 }