Javascript wrappers for HTS221 Sensor library
Dependencies: HTS221
Revision 5:bbe15d3cac27, committed 2017-10-31
- Comitter:
- akhtar.syedzeeshan@gmail.com
- Date:
- Tue Oct 31 16:18:15 2017 +0100
- Parent:
- 4:6043b9511072
- Child:
- 6:b2c4b0678856
- Commit message:
- Implemented SPI support
Changed in this revision
--- a/HTS221_JS-js.cpp Wed Oct 25 13:58:13 2017 +0200
+++ b/HTS221_JS-js.cpp Tue Oct 31 16:18:15 2017 +0100
@@ -51,7 +51,6 @@
/**
* HTS221_JS#destructor
- *
* Called if/when the HTS221_JS is GC'ed.
*/
void NAME_FOR_CLASS_NATIVE_DESTRUCTOR(HTS221_JS)(void *void_ptr) {
@@ -60,18 +59,122 @@
/**
* Type infomation of the native HTS221_JS pointer
- *
* Set HTS221_JS#destructor as the free callback.
*/
static const jerry_object_native_info_t native_obj_type_info = {
.free_cb = NAME_FOR_CLASS_NATIVE_DESTRUCTOR(HTS221_JS)
};
+/**
+ * HTS221_JS#init_spi (native JavaScript method)
+ * @brief Initializes the sensor using SPI interface
+ * @param SPI interface
+ * @param CS pin
+ * @param DRDY pin
+ */
+DECLARE_CLASS_FUNCTION(HTS221_JS, init_spi) {
+ CHECK_ARGUMENT_COUNT(HTS221_JS, init_spi, (args_count == 1 || args_count == 3));
+ CHECK_ARGUMENT_TYPE_ALWAYS(HTS221_JS, init_spi, 0, object);
+ CHECK_ARGUMENT_TYPE_ON_CONDITION(HTS221_JS, init_spi, 1, number, args_count == 3);
+ CHECK_ARGUMENT_TYPE_ON_CONDITION(HTS221_JS, init_spi, 2, number, args_count == 3);
+
+ // Unwrap native HTS221_JS object
+ void *void_ptr;
+ const jerry_object_native_info_t *type_ptr;
+ bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
+
+ if (!has_ptr || type_ptr != &native_obj_type_info) {
+ return jerry_create_error(JERRY_ERROR_TYPE,
+ (const jerry_char_t *) "Failed to get native HTS221_JS pointer");
+ }
+
+ HTS221_JS *native_ptr = static_cast<HTS221_JS*>(void_ptr);
+
+ // Unwrap arguments
+ void *spi_ptr;
+ const jerry_object_native_info_t *spi_type_ptr;
+ bool spi_has_ptr = jerry_get_object_native_pointer(args[0], &spi_ptr, &spi_type_ptr);
+
+ // Check if we have the spi pointer
+ if (!spi_has_ptr) {
+ printf("Not a SPI input!");
+ return jerry_create_error(JERRY_ERROR_TYPE,
+ (const jerry_char_t *) "Failed to get native DigitalOut pointer");
+ }
+
+ // Cast the argument to C++
+ SPI* spi = reinterpret_cast<SPI*>(spi_ptr);
+
+ // Call the native function
+ if(args_count == 1){
+ native_ptr->init(*spi);
+ }
+ else if(args_count == 3){
+ int cs_pin = jerry_get_number_value(args[1]);
+ int drdy_pin = jerry_get_number_value(args[2]);
+ native_ptr->init(*spi, (PinName) cs_pin, (PinName) drdy_pin);
+ }
+
+ return jerry_create_number(0);
+}
+
+/**
+ * HTS221_JS#init_i2c (native JavaScript method)
+ * @brief Initializes the sensor using I2C interface
+ * @param I2C interface
+ * @param Address
+ * @param DRDY pin
+ */
+DECLARE_CLASS_FUNCTION(HTS221_JS, init_i2c) {
+ CHECK_ARGUMENT_COUNT(HTS221_JS, init_i2c, (args_count == 1 || args_count == 3));
+ CHECK_ARGUMENT_TYPE_ALWAYS(HTS221_JS, init_i2c, 0, object);
+ CHECK_ARGUMENT_TYPE_ON_CONDITION(HTS221_JS, init_i2c, 1, number, args_count == 3);
+ CHECK_ARGUMENT_TYPE_ON_CONDITION(HTS221_JS, init_i2c, 2, number, args_count == 3);
+
+ // Unwrap native HTS221_JS object
+ void *void_ptr;
+ const jerry_object_native_info_t *type_ptr;
+ bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
+
+ if (!has_ptr || type_ptr != &native_obj_type_info) {
+ return jerry_create_error(JERRY_ERROR_TYPE,
+ (const jerry_char_t *) "Failed to get native HTS221_JS pointer");
+ }
+
+ HTS221_JS *native_ptr = static_cast<HTS221_JS*>(void_ptr);
+
+ // Unwrap arguments
+ void *i2c_ptr;
+ const jerry_object_native_info_t *i2c_type_ptr;
+ bool i2c_has_ptr = jerry_get_object_native_pointer(args[0], &i2c_ptr, &i2c_type_ptr);
+
+ // Check if we have the i2c pointer
+ if (!i2c_has_ptr) {
+ printf("Not a I2C input!");
+ return jerry_create_error(JERRY_ERROR_TYPE,
+ (const jerry_char_t *) "Failed to get native DigitalOut pointer");
+ }
+
+ // Cast the argument to C++
+ DevI2C* i2c = reinterpret_cast<DevI2C*>(i2c_ptr);
+
+ // call the respective function
+ if(args_count == 1){
+ native_ptr->init(*i2c);
+ }
+ else if(args_count == 3){
+ int address = jerry_get_number_value(args[1]);
+ int drdy_pin = jerry_get_number_value(args[2]);
+ native_ptr->init(*i2c, (uint8_t) address, (PinName) drdy_pin);
+ }
+
+ return jerry_create_number(0);
+}
/**
* HTS221_JS#get_temperature (native JavaScript method)
- *
- * @returns temperature
+ * @brief Gets the temperature reading
+ * @returns Temperature
*/
DECLARE_CLASS_FUNCTION(HTS221_JS, get_temperature) {
CHECK_ARGUMENT_COUNT(HTS221_JS, get_temperature, (args_count == 0));
@@ -91,17 +194,54 @@
// Get the result from the C++ API
float result = native_ptr->get_temperature();
-
+ //printf("temp hts221: %s\n", result);
+
// Cast it back to JavaScript and return
return jerry_create_number(result);
}
+/**
+ * HTS221_JS#get_temperature_string (native JavaScript method)
+ * @brief Gets the temperature reading in string form
+ * @returns Temperature in string
+ */
+DECLARE_CLASS_FUNCTION(HTS221_JS, get_temperature_string) {
+ CHECK_ARGUMENT_COUNT(HTS221_JS, get_temperature_string, (args_count == 0));
+
+ // Unwrap native HTS221_JS object
+ void *void_ptr;
+ const jerry_object_native_info_t *type_ptr;
+ bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
+ if (!has_ptr || type_ptr != &native_obj_type_info) {
+ return jerry_create_error(JERRY_ERROR_TYPE,
+ (const jerry_char_t *) "Failed to get native HTS221_JS pointer");
+ }
+
+ HTS221_JS *native_ptr = static_cast<HTS221_JS*>(void_ptr);
+
+ char * result = new char[128];
+ result = native_ptr->get_temperature_string(result);
+
+ //pc.printf("Temperature: %s", result);
+
+ // Cast it back to JavaScript
+ jerry_value_t out = jerry_create_string((unsigned char *)result);
+
+ printf("temperature: %s\n", result);
+
+ // Recycle the result from function
+ delete result;
+
+ // Return the output
+ return out;
+
+}
/**
* HTS221_JS#get_humidity (native JavaScript method)
- *
- * @returns humidity
+ * @brief Get the humidity reading
+ * @returns Humidity
*/
DECLARE_CLASS_FUNCTION(HTS221_JS, get_humidity) {
CHECK_ARGUMENT_COUNT(HTS221_JS, get_humidity, (args_count == 0));
@@ -121,15 +261,53 @@
// Get the result from the C++ API
float result = native_ptr->get_humidity();
-
+ //printf("humidity hts221: %s\n", result);
+
// Cast it back to JavaScript and return
return jerry_create_number(result);
}
/**
+ * HTS221_JS#get_humidity_string (native JavaScript method)
+ * @brief Get the humidity reading in string form
+ * @returns humidity in string
+ */
+DECLARE_CLASS_FUNCTION(HTS221_JS, get_humidity_string) {
+ CHECK_ARGUMENT_COUNT(HTS221_JS, get_humidity_string, (args_count == 0));
+
+
+ // Unwrap native HTS221_JS object
+ void *void_ptr;
+ const jerry_object_native_info_t *type_ptr;
+ bool has_ptr = jerry_get_object_native_pointer(this_obj, &void_ptr, &type_ptr);
+
+ if (!has_ptr || type_ptr != &native_obj_type_info) {
+ return jerry_create_error(JERRY_ERROR_TYPE,
+ (const jerry_char_t *) "Failed to get native HTS221_JS pointer");
+ }
+
+ HTS221_JS *native_ptr = static_cast<HTS221_JS*>(void_ptr);
+
+ char * result = new char[128];
+ result = native_ptr->get_humidity_string(result);
+
+ // Cast it back to JavaScript
+ jerry_value_t out = jerry_create_string((unsigned char *)result);
+
+ printf("humidity: %s\n", result);
+
+ // Recycle the result from function
+ delete result;
+
+ // Return the output
+ return out;
+}
+
+/**
* HTS221_JS#led_on (native JavaScript method)
- * @param pin DigitalOut pin to blink
+ * @brief Sets the LED to 1, for testing purposes
+ * @param DigitalOut pin to blink
*/
DECLARE_CLASS_FUNCTION(HTS221_JS, led_on) {
// Check that we have 1 argument, and that it's an object
@@ -169,40 +347,30 @@
return jerry_create_undefined();
}
+
/**
* HTS221_JS (native JavaScript constructor)
- *
- * @returns a JavaScript object representing HTS221_JS.
+ * @brief Constructor for Javascript wrapper
+ * @returns JavaScript object representing HTS221_JS.
*/
DECLARE_CLASS_CONSTRUCTOR(HTS221_JS) {
- CHECK_ARGUMENT_COUNT(HTS221_JS, __constructor, args_count == 1);
- CHECK_ARGUMENT_TYPE_ALWAYS(HTS221_JS, __constructor, 0, object);
-
- // Extract native DevI2C argument (objects are always pointers)
- void *devI2c_ptr;
- const jerry_object_native_info_t *devI2c_type_ptr;
- bool devI2c_has_ptr = jerry_get_object_native_pointer(args[0], &devI2c_ptr, &devI2c_type_ptr);
-
- // Check if we have the devI2c pointer
- if (!devI2c_has_ptr) {
- printf("Not a DevI2C input!");
- return jerry_create_error(JERRY_ERROR_TYPE,
- (const jerry_char_t *) "Failed to get native DigitalOut pointer");
- }
-
- // Cast the argument to C++
- DevI2C* devI2c = reinterpret_cast<DevI2C*>(devI2c_ptr);
+ CHECK_ARGUMENT_COUNT(HTS221_JS, __constructor, args_count == 0);
+ //pc.printf("constructor called!\n");
// Extract native HTS221_JS pointer (from this object)
- HTS221_JS *native_ptr = new HTS221_JS(*devI2c);
+ HTS221_JS *native_ptr = new HTS221_JS();
jerry_value_t js_object = jerry_create_object();
jerry_set_object_native_pointer(js_object, native_ptr, &native_obj_type_info);
// attach methods
+ ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, init_spi);
+ ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, init_i2c);
ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, get_temperature);
+ ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, get_temperature_string);
ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, get_humidity);
+ ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, get_humidity_string);
ATTACH_CLASS_FUNCTION(js_object, HTS221_JS, led_on);
return js_object;
-}
\ No newline at end of file
+}
--- a/HTS221_JS-js.h Wed Oct 25 13:58:13 2017 +0200
+++ b/HTS221_JS-js.h Tue Oct 31 16:18:15 2017 +0100
@@ -54,5 +54,5 @@
DECLARE_JS_WRAPPER_REGISTRATION (HTS221_JS_library) {
REGISTER_CLASS_CONSTRUCTOR(HTS221_JS);
}
-
+
#endif
\ No newline at end of file
--- a/HTS221_JS.cpp Wed Oct 25 13:58:13 2017 +0200
+++ b/HTS221_JS.cpp Tue Oct 31 16:18:15 2017 +0100
@@ -83,18 +83,88 @@
/* Class Implementation ------------------------------------------------------*/
/** Constructor
- * @brief Initializing the component.
- * @param devI2c object of an helper class which handles the DevI2C peripheral
+ * @brief Initializing the component.
+ * @param devI2c object of an helper class which handles the DevI2C peripheral
*/
HTS221_JS::HTS221_JS(DevI2C &devI2c){
+ init(devI2c);
+}
+
+/** init
+ * @brief Initializing the component.
+ * @param devI2c object of an helper class which handles the DevI2C peripheral
+ */
+void HTS221_JS::init(DevI2C &devI2c){
hum_temp = new HTS221Sensor(&devI2c);
hum_temp->init(NULL);
hum_temp->enable();
}
+/** Constructor
+ * @brief Initializing the component.
+ * @param devI2c object of an helper class which handles the DevI2C peripheral
+ * @param address
+ * @param drdy pin
+ */
+HTS221_JS::HTS221_JS(DevI2C &devI2c, uint8_t address, PinName drdy_pin){
+ init(devI2c, address, drdy_pin);
+}
+
+/** init
+ * @brief Initializing the component.
+ * @param devI2c object of an helper class which handles the DevI2C peripheral
+ * @param address
+ * @param drdy pin
+ */
+void HTS221_JS::init(DevI2C &devI2c, uint8_t address, PinName drdy_pin){
+ hum_temp = new HTS221Sensor(&devI2c, address, drdy_pin);
+ hum_temp->init(NULL);
+ hum_temp->enable();
+}
+
+/** Constructor
+ * @brief Initializing the component.
+ * @param spi object of an helper class which handles the SPI peripheral
+ */
+HTS221_JS::HTS221_JS(SPI &spi){
+ init(spi);
+}
+
+/** init
+ * @brief Initializing the component.
+ * @param spi object of an helper class which handles the SPI peripheral
+ */
+void HTS221_JS::init(SPI &spi){
+ hum_temp = new HTS221Sensor(&spi);
+ hum_temp->init(NULL);
+ hum_temp->enable();
+}
+
+/** Constructor
+ * @brief Initializing the component.
+ * @param spi object of an helper class which handles the SPI peripheral
+ * @param cs pin
+ * @param drdy pin
+ */
+HTS221_JS::HTS221_JS(SPI &spi, PinName cs_pin, PinName drdy_pin){
+ init(spi, cs_pin, drdy_pin);
+}
+
+/** init
+ * @brief Initializing the component.
+ * @param spi object of an helper class which handles the SPI peripheral
+ * @param cs pin
+ * @param drdy pin
+ */
+void HTS221_JS::init(SPI &spi, PinName cs_pin, PinName drdy_pin){
+ hum_temp = new HTS221Sensor(&spi, cs_pin, drdy_pin);
+ hum_temp->init(NULL);
+ hum_temp->enable();
+}
+
/** Destructor
- * @brief Recycling the component.
- * Deletes the Sensor Object
+ * @brief Recycle the component.
+ * Deletes the Sensor Object
*/
HTS221_JS::~HTS221_JS(){
if(hum_temp != NULL){
@@ -103,8 +173,8 @@
}
/**
- * @brief Read ID address of HTS221
- * @retval The ID of the Sensor
+ * @brief Read ID address of HTS221
+ * @retval The ID of the Sensor
*/
uint8_t HTS221_JS::readID(){
uint8_t result;
@@ -113,8 +183,8 @@
}
/**
- * @brief Get the temperature reading from HTS221
- * @retval Temperature value
+ * @brief Get the temperature reading from HTS221
+ * @retval Temperature value
*/
float HTS221_JS::get_temperature(){
float value;
@@ -123,8 +193,8 @@
}
/**
- * @brief Get the temperature reading from HTS221
- * @retval Temperature value in string
+ * @brief Get the temperature reading from HTS221
+ * @retval Temperature value in string
*/
char *HTS221_JS::get_temperature_string(char *buffer){
float value;
@@ -134,8 +204,8 @@
}
/**
- * @brief Get the humidity reading from HTS221
- * @retval Humidity value
+ * @brief Get the humidity reading from HTS221
+ * @retval Humidity value
*/
float HTS221_JS::get_humidity(){
float value;
@@ -144,8 +214,8 @@
}
/**
- * @brief Get the humidity reading from HTS221
- * @retval Humidity value in string
+ * @brief Get the humidity reading from HTS221
+ * @retval Humidity value in string
*/
char *HTS221_JS::get_humidity_string(char *buffer){
float value;
--- a/HTS221_JS.h Wed Oct 25 13:58:13 2017 +0200
+++ b/HTS221_JS.h Tue Oct 31 16:18:15 2017 +0100
@@ -61,7 +61,16 @@
public:
/* Constructors */
HTS221_JS(){ printf("calling empty constructor"); }
+
HTS221_JS(DevI2C &devI2c);
+ void init(DevI2C &devI2c);
+ HTS221_JS(DevI2C &devI2c, uint8_t address, PinName drdy_pin);
+ void init(DevI2C &devI2c, uint8_t address, PinName drdy_pin);
+
+ HTS221_JS(SPI &spi);
+ void init(SPI &spi);
+ HTS221_JS(SPI &spi, PinName cs_pin, PinName drdy_pin);
+ void init(SPI &spi, PinName cs_pin, PinName drdy_pin);
/* Destructor */
~HTS221_JS();