move files around

This commit is contained in:
hathach committed 2017-01-11 13:56:45 +07:00
1 parent 9dbaf7cd49
commit 09ae3f22ce
18 files changed
+59 -19

No files matched your search

@@ -36,7 +36,7 @@
#ifndef BLECHARACTERISTIC_H_
#define BLECHARACTERISTIC_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "BLEUuid.h"
class AdafruitBluefruit;
+1 -1
View File
@@ -36,7 +36,7 @@
#ifndef BLESERVICE_H_
#define BLESERVICE_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "BLEUuid.h"
class BLEService
+1 -1
View File
@@ -36,7 +36,7 @@
#ifndef BLEUUID_H_
#define BLEUUID_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
class BLEUuid
{
+1 -1
View File
@@ -35,7 +35,7 @@
/**************************************************************************/
#include <Arduino.h>
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "BLEUuid.h"
#include "BLECharacteristic.h"
@@ -1,13 +1,13 @@
/**************************************************************************/
/*!
@file common_inc.h
@file bluefruit_common.h
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2016, Adafruit Industries (adafruit.com)
Copyright (c) 2017, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
@@ -33,8 +33,8 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef COMMON_INC_H_
#define COMMON_INC_H_
#ifndef BLUEFRUIT_COMMON_H_
#define BLUEFRUIT_COMMON_H_
#include <Arduino.h>
#include "ble.h"
@@ -43,8 +43,4 @@
typedef uint32_t err_t;
#include "utility/compiler_macro.h"
#include "utility/verify.h"
#include "utility/common_func.h"
#endif /* COMMON_INC_H_ */
#endif /* BLUEFRUIT_COMMON_H_ */
@@ -36,7 +36,7 @@
#ifndef BLEBAS_H_
#define BLEBAS_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "BLECharacteristic.h"
#include "BLEService.h"
@@ -36,7 +36,7 @@
#ifndef BLEBEACON_H_
#define BLEBEACON_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "BLECharacteristic.h"
#include "BLEService.h"
@@ -36,7 +36,7 @@
#ifndef BLEDFU_H_
#define BLEDFU_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "BLECharacteristic.h"
#include "BLEService.h"
@@ -36,7 +36,7 @@
#ifndef BLEDIS_H_
#define BLEDIS_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "BLECharacteristic.h"
#include "BLEService.h"
@@ -36,7 +36,7 @@
#ifndef BLEUART_H_
#define BLEUART_H_
#include "utility/common_inc.h"
#include "bluefruit_common.h"
#include "utility/adafruit_fifo.h"
#include "BLECharacteristic.h"
@@ -1,56 +0,0 @@
/**************************************************************************/
/*!
@file TimeoutTimer.h
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2014, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef _TIMEOUT_TIMER_H_
#define _TIMEOUT_TIMER_H_
class TimeoutTimer
{
private:
uint32_t start;
uint32_t interval;
public:
TimeoutTimer() { start = millis(); interval = 0; }
TimeoutTimer(uint32_t msec) { set(msec); }
void set(uint32_t msec) { start = millis(); interval = msec; }
bool expired(void) const { return (millis() - start) >= interval; }
void restart(void) { start = millis(); }
void reset(void) { start += interval; } // used for periodic invoke to prevent drift
};
#endif /* _TIMEOUT_TIMER_H_ */
@@ -1,254 +0,0 @@
/**************************************************************************/
/*!
@file adafruit_fifo.cpp
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2015, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "adafruit_fifo.h"
#include <string.h>
/******************************************************************************/
/*!
@brief Constructor
@param[in] depth
Maximum number of items can be hold in buffer
@param[in] item_size
Number of bytes of each item
@param[in] overwrite
Should the buffer is overwritten to the first item when it is full
*/
/******************************************************************************/
Adafruit_FIFO::Adafruit_FIFO(uint16_t depth, uint8_t item_size, bool overwrite)
: _depth(depth), _item_size(item_size), _overwritable(overwrite)
{
_buffer = (uint8_t*) malloc(item_size*depth); // use malloc_name for debug
_count = _wr_idx = _rd_idx = 0;
_mutex = xSemaphoreCreateMutex();
}
/**
* Destructor
* @return
*/
Adafruit_FIFO::~Adafruit_FIFO()
{
vSemaphoreDelete(_mutex);
free(_buffer); // use free_name for debug
}
bool Adafruit_FIFO::_mutex_lock(bool isr)
{
(void) isr;
return xSemaphoreTake(_mutex, portMAX_DELAY);
}
bool Adafruit_FIFO::_mutex_unlock(bool isr)
{
(void) isr;
return xSemaphoreGive(_mutex);
}
/******************************************************************************/
/*!
@brief Clear the FIFO
*/
/******************************************************************************/
void Adafruit_FIFO::clear(void)
{
_mutex_lock(false);
_rd_idx = _wr_idx = _count = 0;
_mutex_unlock(false);
}
/******************************************************************************/
/*!
@brief Write an item to the FIFO
@param[in] item
Memory address of the item
*/
/******************************************************************************/
uint16_t Adafruit_FIFO::write(void const* item)
{
if ( full() && !_overwritable ) return 0;
_mutex_lock(false);
memcpy( _buffer + (_wr_idx * _item_size),
item,
_item_size);
_wr_idx = (_wr_idx + 1) % _depth;
if ( full() )
{
_rd_idx = _wr_idx; // keep the full state (rd == wr && len = size)
}
else
{
_count++;
}
_mutex_unlock(false);
return 1;
}
/******************************************************************************/
/*!
@brief Write array of items to the FIFO
@param[in] data
Memory address of the item's array
@param[in] n
Number of items to write
@return Number of written items
*/
/******************************************************************************/
uint16_t Adafruit_FIFO::write(void const * data, uint16_t n)
{
if ( n == 0 ) return 0;
uint8_t* buf = (uint8_t*) data;
uint16_t len = 0;
while( (len < n) && write(buf) )
{
len++;
buf += _item_size;
}
return len;
}
/******************************************************************************/
/*!
@brief Read an item from FIFO
@param[in] buffer
Memory address to store item
*/
/******************************************************************************/
uint16_t Adafruit_FIFO::read(void* buffer)
{
if( empty() ) return 0;
_mutex_lock(false);
memcpy(buffer,
_buffer + (_rd_idx * _item_size),
_item_size);
_rd_idx = (_rd_idx + 1) % _depth;
_count--;
_mutex_unlock(false);
return 1;
}
/******************************************************************************/
/*!
@brief Read multiple items to an array
@param[in] buffer
Memory address of the item's array
@param[in] n
Number of items to read
@return Number of read items
*/
/******************************************************************************/
uint16_t Adafruit_FIFO::read(void * buffer, uint16_t n)
{
if( n == 0 ) return 0;
uint8_t* buf = (uint8_t*) buffer;
uint16_t len = 0;
while( (len < n) && read(buf) )
{
len++;
buf += _item_size;
}
return len;
}
/******************************************************************************/
/*!
@brief Read an item without removing it from the FIFO
@param[in] buffer
Memory address to store item
*/
/******************************************************************************/
bool Adafruit_FIFO::peek(void* buffer)
{
if( empty() ) return false;
memcpy(buffer,
_buffer + (_rd_idx * _item_size),
_item_size);
return true;
}
/******************************************************************************/
/*!
@brief Read an item without removing it from the FIFO at the specific index
@param[in] position
Position to read from in the FIFO buffer
@param[in] buffer
Memory address to store item
*/
/******************************************************************************/
bool Adafruit_FIFO::peekAt(uint16_t position, void * p_buffer)
{
if( empty() || (position >= _count) ) return false;
uint16_t index = (_rd_idx + position) % _depth; // rd_idx is position=0
memcpy(p_buffer,
_buffer + (index * _item_size),
_item_size);
return true;
}
@@ -1,86 +0,0 @@
/**************************************************************************/
/*!
@file adafruit_fifo.h
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2015, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef _Adafruit_FIFO_H_
#define _Adafruit_FIFO_H_
#include <stdint.h>
#include <stdbool.h>
#include <Arduino.h>
class Adafruit_FIFO
{
private:
uint8_t* _buffer ; ///< buffer pointer
const uint16_t _depth ; ///< max items
const uint8_t _item_size ; ///< size of each item
const bool _overwritable ; ///< Overwrite when full
volatile uint16_t _count ; ///< number of items in queue
volatile uint16_t _wr_idx ; ///< write pointer
volatile uint16_t _rd_idx ; ///< read pointer
SemaphoreHandle_t _mutex;
bool _mutex_lock(bool isr);
bool _mutex_unlock(bool isr);
public:
// Constructor
Adafruit_FIFO(uint16_t depth, uint8_t item_size, bool overwrite = false);
virtual ~Adafruit_FIFO();
void clear(void);
bool peek(void* buffer);
bool peekAt(uint16_t position, void * p_buffer);
uint16_t write(void const* item);
uint16_t write(void const * data, uint16_t n);
uint16_t read(void* buffer);
uint16_t read(void * buffer, uint16_t n);
// uint16_t read_isr(void* buffer);
// uint16_t read_isr(void * buffer, uint16_t n);
inline bool empty(void) { return _count == 0; }
inline bool full(void) { return _count == _depth; }
inline uint16_t count(void) { return _count; }
inline uint16_t remaining(void) { return _depth - _count; }
};
#endif /* _Adafruit_FIFO_H_ */
@@ -1,219 +0,0 @@
/**************************************************************************/
/*!
@file common_func.h
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2016, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#ifndef _COMMON_FUNC_H_
#define _COMMON_FUNC_H_
#ifdef __cplusplus
extern "C" {
#endif
#define memclr(buffer, size) memset(buffer, 0, size)
#define varclr(_var) memclr(_var, sizeof(*(_var)))
#define arrcount(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
#define __swap32(x) __REV(x) ///< built-in function to swap Endian of 32-bit number
#define __swap16(u16) ((uint16_t) __REV16(u16)) ///< built-in function to swap Endian of 16-bit number
#define maxof(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; })
#define minof(a,b) \
({ typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a < _b ? _a : _b; })
//--------------------------------------------------------------------+
// MACROS
//--------------------------------------------------------------------+
#define U16_HIGH(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
#define U16_LOW(u16) ((uint8_t) ((u16) & 0x00ff))
#define U16_BYTES_BE(u16) U16_HIGH(u16), U16_LOW(u16)
#define U16_BYTES_LE(u16) U16_LOW(u16), U16_HIGH(u16)
#define U32_BYTE1(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
#define U32_BYTE2(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
#define U32_BYTE3(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff))
#define U32_BYTE4(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB
#define U32_FROM_U8(b1, b2, b3, b4) ((uint32_t) (((b1) << 24) + ((b2) << 16) + ((b3) << 8) + (b4)))
#define U32_FROM_U16(high, low) ((uint32_t) (((high) << 16) | (low)))
#define U16_FROM_U8(high, low) ((uint32_t) (((high) << 8) | (low)))
#define U32_BYTES_BE(u32) U32_BYTE1(u32), U32_BYTE2(u32), U32_BYTE3(u32), U32_BYTE4(u32)
#define U32_BYTES_LE(u32) U32_BYTE4(u32), U32_BYTE3(u32), U32_BYTE2(u32), U32_BYTE1(u32)
#define SWAP16(x) ((uint16_t)(((x) << 8) | (((x) & 0xff00) >> 8)))
//--------------------------------------------------------------------+
// DEBUG HELPER
//--------------------------------------------------------------------+
#if CFG_DEBUG == 3
#define malloc_named( name, size ) ({ printf("[malloc] %s : %d\r\n", name, size); malloc(size); })
#else
#define malloc_named( name, size ) malloc ( size )
#endif
#define PRINT_LOCATION() Serial.printf("%s: %d:\n", __PRETTY_FUNCTION__, __LINE__)
#define PRTNT_HEAP() if (CFG_DEBUG == 3) Serial.printf("\n%s: %d: Heap free: %d\n", __PRETTY_FUNCTION__, __LINE__, util_heap_get_free_size())
#define PRINT_INT(x) Serial.printf("%s: %d: " #x " = %ld\n" , __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
#define PRINT_HEX(x) Serial.printf("%s: %d: " #x " = %08lx\n", __PRETTY_FUNCTION__, __LINE__, (uint32_t) (x) )
#define PRINT_STR(x) Serial.printf("%s: %d: " #x " = %s\n" , __PRETTY_FUNCTION__, __LINE__, (char*)(x) )
#define PRINT_BUFFER(buf, n) \
do {\
uint8_t const* p8 = (uint8_t const*) (buf);\
Serial.print(#buf ": ");\
for(uint32_t i=0; i<(n); i++) Serial.printf("%02x ", p8[i]);\
Serial.println();\
}while(0)
//--------------------------------------------------------------------+
// INLINE FUNCTION
//--------------------------------------------------------------------+
/// Checks is all values in the supplied array are zero
static inline bool mem_test_zero(void const* buffer, uint32_t size)
{
uint8_t const* p_mem = (uint8_t const*) buffer;
for(uint32_t i=0; i<size; i++) if (p_mem[i] != 0) return false;
return true;
}
//------------- Min, Max -------------//
static inline uint8_t min8 (uint8_t x, uint8_t y) { return (x < y) ? x : y; }
static inline uint16_t min16(uint16_t x, uint16_t y) { return (x < y) ? x : y; }
static inline uint32_t min32(uint32_t x, uint32_t y) { return (x < y) ? x : y; }
static inline uint8_t max8 (uint8_t x, uint8_t y) { return (x > y) ? x : y; }
static inline uint16_t max16(uint16_t x, uint16_t y) { return (x > y) ? x : y; }
static inline uint32_t max32(uint32_t x, uint32_t y) { return (x > y) ? x : y; }
//------------- Conversion -------------//
/// form an uint32_t from 4 x uint8_t
static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
{
return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
}
static inline uint16_t u32_high_u16(uint32_t u32)
{
return (uint16_t) ((u32 >> 16) & 0xffff);
}
static inline uint16_t u32_low_u16(uint32_t u32)
{
return (uint16_t) (u32 & 0xffff);
}
static inline uint16_t u16_from_u8(uint8_t b1, uint8_t b2)
{
return (b1 << 8) + b2;
}
static inline uint8_t u16_high_u8(uint16_t u16)
{
return (uint8_t) ((u16 >> 8) & 0x00ff);
}
static inline uint8_t u16_low_u8(uint16_t u16)
{
return (uint8_t) (u16 & 0x00ff);
}
//------------- Align -------------//
static inline uint32_t align32 (uint32_t value)
{
return (value & 0xFFFFFFE0UL);
}
static inline uint32_t align16 (uint32_t value)
{
return (value & 0xFFFFFFF0UL);
}
static inline uint32_t align_n (uint32_t alignment, uint32_t value)
{
return value & (~(alignment-1));
}
static inline uint32_t align4k (uint32_t value)
{
return (value & 0xFFFFF000UL);
}
static inline uint32_t offset4k(uint32_t value)
{
return (value & 0xFFFUL);
}
//------------- Mathematics -------------//
static inline bool is_within(uint32_t lower, uint32_t value, uint32_t upper)
{
return (lower <= value) && (value <= upper);
}
static inline uint8_t log2_of(uint32_t value)
{
uint8_t result = 0; // log2 of a value is its MSB's position
while (value >>= 1) result++;
return result;
}
// return the number of set bits in value
static inline uint8_t cardinality_of(uint32_t value)
{
// Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only
// the high bit set, then it will only go once through the loop
// Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie)
// mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method
// "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and
// published in 1964 in a book edited by Beckenbach.)"
uint8_t count;
for (count = 0; value; count++)
{
value &= value - 1; // clear the least significant bit set
}
return count;
}
#ifdef __cplusplus
}
#endif
#endif /* _COMMON_FUNC_H_ */
@@ -1,127 +0,0 @@
/******************************************************************************/
/*!
@file compiler_macro.h
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, K. Townsend (microBuilder.eu)
Copyright (c) 2014, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/******************************************************************************/
/** \ingroup Group_Compiler
* \defgroup Group_GCC GNU GCC
* @{
*/
#ifndef _COMPILER_MACRO_H_
#define _COMPILER_MACRO_H_
#define STRING_(x) #x // stringify without expand
#define XSTRING_(x) STRING_(x) // expand then stringify
#define STRING_CONCAT_(a, b) a##b // concat without expand
#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) // expand then concat
#define ALIGN_OF(x) __alignof__(x)
/// Normally, the compiler places the objects it generates in sections like data or bss & function in text. Sometimes, however, you need additional sections, or you need certain particular variables to appear in special sections, for example to map to special hardware. The section attribute specifies that a variable (or function) lives in a particular section
#define ATTR_SECTION(section) __attribute__ ((#section))
/// If this attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, an error that includes message is diagnosed. This is useful for compile-time checking
#define ATTR_ERROR(Message) __attribute__ ((error(Message)))
/// If this attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, a warning that includes message is diagnosed. This is useful for compile-time checking
#define ATTR_WARNING(Message) __attribute__ ((warning(Message)))
/**
* \defgroup Group_VariableAttr Variable Attributes
* @{
*/
/// This attribute specifies a minimum alignment for the variable or structure field, measured in bytes
#define ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
/// The packed attribute specifies that a variable or structure field should have the smallest possible alignment—one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute
#define ATTR_PACKED __attribute__ ((packed))
#define ATTR_PREPACKED
#define ATTR_PACKED_STRUCT(x) x __attribute__ ((packed))
/** @} */
/**
* \defgroup Group_FuncAttr Function Attributes
* @{
*/
/// Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level is specified
#define ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
/// The nonnull attribute specifies that some function parameters should be non-null pointers. f the compiler determines that a null pointer is passed in an argument slot marked as non-null, and the -Wnonnull option is enabled, a warning is issued. All pointer arguments are marked as non-null
#define ATTR_NON_NULL __attribute__ ((nonull))
#define ATTR_NO_RETURN __attribute__ ((noreturn))
/// Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure
#define ATTR_PURE __attribute__ ((pure))
/// Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory.
/// Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void
#define ATTR_CONST __attribute__ ((const))
/// The deprecated attribute results in a warning if the function is used anywhere in the source file. This is useful when identifying functions that are expected to be removed in a future version of a program. The warning also includes the location of the declaration of the deprecated function, to enable users to easily find further information about why the function is deprecated, or what they should do instead. Note that the warnings only occurs for uses
#define ATTR_DEPRECATED __attribute__ ((deprecated))
/// Same as the deprecated attribute with optional message in the warning
#define ATTR_DEPRECATED_MESS(mess) __attribute__ ((deprecated(mess)))
/// The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions that can be overridden in user code
#define ATTR_WEAK __attribute__ ((weak))
/// The alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified
#define ATTR_ALIAS(func) __attribute__ ((alias(#func)))
/// The weakref attribute marks a declaration as a weak reference. It is equivalent with weak + alias attribute, but require function is static
#define ATTR_WEAKREF(func) __attribute__ ((weakref(#func)))
/// The warn_unused_result attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug
#define ATTR_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
/// This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly.
#define ATTR_USED __attribute__ ((used))
/// This attribute, attached to a function, means that the function is meant to be possibly unused. GCC does not produce a warning for this function.
#define ATTR_UNUSED __attribute__ ((unused))
#define ATTR_OPTIMIZE(n) __attribute__ ((optimize(XSTRING_(O##n))))
#endif /* _COMPILER_MACRO_H_ */
/// @}
@@ -1,131 +0,0 @@
/******************************************************************************/
/*!
@file verify.h
@author hathach
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, K. Townsend (microBuilder.eu)
Copyright (c) 2015, Adafruit Industries (adafruit.com)
Copyright (c) 2016, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/******************************************************************************/
#ifndef _VERIFY_H_
#define _VERIFY_H_
#include "compiler_macro.h"
#ifdef __cplusplus
extern "C"
{
#endif
#define CFG_DEBUG 1
#define RETURN_VOID
//--------------------------------------------------------------------+
// Compile-time Assert
//--------------------------------------------------------------------+
#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
#define _VERIFY_COUNTER __COUNTER__
#else
#define _VERIFY_COUNTER __LINE__
#endif
#define VERIFY_STATIC(const_expr) enum { XSTRING_CONCAT_(static_verify_, _VERIFY_COUNTER) = 1/(!!(const_expr)) }
//--------------------------------------------------------------------+
// VERIFY Helper
//--------------------------------------------------------------------+
#if CFG_DEBUG >= 1
// #define VERIFY_MESS(format, ...) printf("[%08ld] %s: %d: verify failed\n", get_millis(), __func__, __LINE__)
#define VERIFY_MESS(_status) Serial.printf("%s: %d: verify failed, status = 0x%04X\n", __PRETTY_FUNCTION__, __LINE__, _status);
#else
#define VERIFY_MESS(_status)
#endif
#define VERIFY_DEFINE(_status, _error) \
if ( 0 != _status ) { \
VERIFY_MESS(_status) \
return _error; \
}
/**
* Helper to implement optional parameter for VERIFY Macro family
*/
#define VERIFY_GETARGS(arg1, arg2, arg3, ...) arg3
/*------------------------------------------------------------------*/
/* VERIFY STATUS
* - VERIFY_STS_1ARGS : return status of condition if failed
* - VERIFY_STS_2ARGS : return provided status code if failed
*------------------------------------------------------------------*/
#define VERIFY_STS_1ARGS(sts) \
do { \
err_t _status = (err_t)(sts); \
VERIFY_DEFINE(_status, _status);\
} while(0)
#define VERIFY_STS_2ARGS(sts, _error) \
do { \
err_t _status = (err_t)(sts); \
VERIFY_DEFINE(_status, _error);\
} while(0)
/**
* Check if status is success (zero), otherwise return
* - status value if called with 1 parameter e.g VERIFY_STATUS(status)
* - 2 parameter if called with 2 parameters e.g VERIFY_STATUS(status, errorcode)
*/
#define VERIFY_STATUS(...) VERIFY_GETARGS(__VA_ARGS__, VERIFY_STS_2ARGS, VERIFY_STS_1ARGS)(__VA_ARGS__)
/*------------------------------------------------------------------*/
/* VERIFY
* - VERIFY_1ARGS : return condition if failed (false)
* - VERIFY_2ARGS : return provided value if failed
*------------------------------------------------------------------*/
#define VERIFY_1ARGS(cond) if (!(cond)) return false;
#define VERIFY_2ARGS(cond, _error) if (!(cond)) return _error;
/**
* Check if condition is success (true), otherwise return
* - false value if called with 1 parameter e.g VERIFY(condition)
* - 2 parameter if called with 2 parameters e.g VERIFY(condition, errorcode)
*/
#define VERIFY(...) VERIFY_GETARGS(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif /* _VERIFY_H_ */