From 55211d5d295b2e5f58453f53c6a42a5f7484698f Mon Sep 17 00:00:00 2001 From: Jeonghoon Park Date: Mon, 9 Oct 2017 14:02:58 +0900 Subject: [PATCH] add feature for adconvert and sound sensor Change-Id: Ia78e258c41d53ad759330197f22bd83eaa91afca --- CMakeLists.txt | 4 +- inc/resource.h | 1 + inc/resource/resource_adc_mcp3008.h | 30 +++ inc/resource/resource_sound_level_sensor.h | 36 ++++ .../resource_sound_level_sensor_internal.h | 27 +++ inc/resource_internal.h | 1 + packaging/org.tizen.position-finder-server.spec | 12 +- src/resource.c | 1 + src/resource/resource_adc_mcp3008.c | 208 +++++++++++++++++++++ src/resource/resource_sound_level_sensor.c | 55 ++++++ 10 files changed, 363 insertions(+), 12 deletions(-) create mode 100644 inc/resource/resource_adc_mcp3008.h create mode 100644 inc/resource/resource_sound_level_sensor.h create mode 100644 inc/resource/resource_sound_level_sensor_internal.h create mode 100644 src/resource/resource_adc_mcp3008.c create mode 100644 src/resource/resource_sound_level_sensor.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 041fa7c..a20c2c4 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ pkg_check_modules(APP_PKGS REQUIRED libcurl glib-2.0 json-glib-1.0 + capi-system-info ) ADD_DEFINITIONS(-DCBOR_FILE_IN_RES="${INSTALL_RESDIR}/${CBOR_FILE}") @@ -56,6 +57,8 @@ ADD_EXECUTABLE(${PROJECT_NAME} ${PROJECT_ROOT_DIR}/src/resource/resource_sound_detection_sensor.c ${PROJECT_ROOT_DIR}/src/resource/resource_tilt_sensor.c ${PROJECT_ROOT_DIR}/src/resource/resource_gas_detection_sensor.c + ${PROJECT_ROOT_DIR}/src/resource/resource_sound_level_sensor.c + ${PROJECT_ROOT_DIR}/src/resource/resource_adc_mcp3008.c ) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} -lm) @@ -66,7 +69,6 @@ Message("APP_LABEL : ${APP_LABEL}") CONFIGURE_FILE(${PROJECT_ROOT_DIR}/tizen-manifest.xml.in ${ORG_PREFIX}.${PROJECT_NAME}.xml @ONLY) # Install INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_EXEC_PREFIX}) -INSTALL(DIRECTORY DESTINATION ${INSTALL_OWNER_DATADIR}) INSTALL(FILES ${ORG_PREFIX}.${PROJECT_NAME}.xml DESTINATION ${SYS_PACKAGES_DIR}) INSTALL(FILES ${PROJECT_ROOT_DIR}/shared/res/default_icon.png DESTINATION ${SYS_ICONS_DIR} RENAME ${PROJECT_NAME}.png) INSTALL(FILES ${PROJECT_ROOT_DIR}/res/${CBOR_FILE} DESTINATION ${INSTALL_RESDIR}) diff --git a/inc/resource.h b/inc/resource.h index 4b41dfb..783fd8f 100755 --- a/inc/resource.h +++ b/inc/resource.h @@ -37,5 +37,6 @@ #include "resource/resource_sound_detection_sensor.h" #include "resource/resource_tilt_sensor.h" #include "resource/resource_gas_detection_sensor.h" +#include "resource/resource_sound_level_sensor.h" #endif /* __POSITION_FINDER_RESOURCE_H__ */ diff --git a/inc/resource/resource_adc_mcp3008.h b/inc/resource/resource_adc_mcp3008.h new file mode 100644 index 0000000..b811c67 --- /dev/null +++ b/inc/resource/resource_adc_mcp3008.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __POSITION_FINDER_RESOURCE_ADC_MCP3008_H__ +#define __POSITION_FINDER_RESOURCE_ADC_MCP3008_H__ + +int resource_adc_mcp3008_init(void); +int resource_read_adc_mcp3008(int ch_num, unsigned int *out_value); +void resource_adc_mcp3008_fini(void); + +#endif /* __POSITION_FINDER_RESOURCE_ADC_MCP3008_H__ */ + diff --git a/inc/resource/resource_sound_level_sensor.h b/inc/resource/resource_sound_level_sensor.h new file mode 100644 index 0000000..b0c64d5 --- /dev/null +++ b/inc/resource/resource_sound_level_sensor.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_H__ +#define __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_H__ + + /** + * @brief Reads the value from sound level sensor through AD converter(MCP3008). + * @remarks We assume that only one AD converter is connected with device. + * @param[in] ch_num The number of channel connected to the sound level sensor with AD converter + * @param[out] out_value The value of a sound level + * @return 0 on success, otherwise a negative error value + * + */ +extern int resource_read_sound_level_sensor(int ch_num, unsigned int *out_value); + +#endif /* __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_H__ */ + diff --git a/inc/resource/resource_sound_level_sensor_internal.h b/inc/resource/resource_sound_level_sensor_internal.h new file mode 100644 index 0000000..ccc8830 --- /dev/null +++ b/inc/resource/resource_sound_level_sensor_internal.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_INTERNAL_H__ +#define __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_INTERNAL_H__ + +extern void resource_close_sound_level_sensor(void); + +#endif /* __POSITION_FINDER_RESOURCE_SOUND_LEVEL_SENSOR_INTERNAL_H__ */ diff --git a/inc/resource_internal.h b/inc/resource_internal.h index cb751b5..df99356 100755 --- a/inc/resource_internal.h +++ b/inc/resource_internal.h @@ -36,6 +36,7 @@ #include "resource/resource_sound_detection_sensor_internal.h" #include "resource/resource_tilt_sensor_internal.h" #include "resource/resource_gas_detection_sensor_internal.h" +#include "resource/resource_sound_level_sensor_internal.h" #define PIN_MAX 40 diff --git a/packaging/org.tizen.position-finder-server.spec b/packaging/org.tizen.position-finder-server.spec index a1bb673..8bc5343 100644 --- a/packaging/org.tizen.position-finder-server.spec +++ b/packaging/org.tizen.position-finder-server.spec @@ -26,6 +26,7 @@ BuildRequires: pkgconfig(gio-2.0) BuildRequires: pkgconfig(libcurl) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(json-glib-1.0) +BuildRequires: pkgconfig(capi-system-info) %description Server for Position Finder @@ -83,16 +84,6 @@ make %{?jobs:-j%jobs} chsmack -a "User::Pkg::%{alias}" %{_pkg_res_dir}/*.dat chmod 444 %{_pkg_res_dir}/*.dat -touch %{_pkg_rw_data_dir}/%{_cbor_file} -chsmack -a "User::Pkg::%{alias}" %{_pkg_rw_data_dir}/*.dat -chmod 666 %{_pkg_rw_data_dir}/*.dat - -# This routine will be used in the file of .dev_id. -#touch %{_pkg_rw_data_dir}/%{_conf_file} -#echo "/door/777" > %{_pkg_rw_data_dir}/%{_conf_file} -#chsmack -a "User::Pkg::%{alias}" %{_pkg_rw_data_dir}/%{_conf_file} -#chmod 444 %{_pkg_rw_data_dir}/%{_conf_file} - %postun -p /sbin/ldconfig %files @@ -100,7 +91,6 @@ chmod 666 %{_pkg_rw_data_dir}/*.dat %{_pkg_res_dir}/*.conf %manifest %{alias}.manifest %defattr(-,root,root,-) -%{_pkg_rw_data_dir} %{_pkg_dir}/bin/%{P_NAME} %{_sys_packages_dir}/%{alias}.xml %{_sys_icons_dir}/*.png diff --git a/src/resource.c b/src/resource.c index 2183850..2354522 100644 --- a/src/resource.c +++ b/src/resource.c @@ -42,4 +42,5 @@ void resource_close_all(void) resource_info[i].close(i); } resource_close_illuminance_sensor(); + resource_close_sound_level_sensor(); } diff --git a/src/resource/resource_adc_mcp3008.c b/src/resource/resource_adc_mcp3008.c new file mode 100644 index 0000000..f170031 --- /dev/null +++ b/src/resource/resource_adc_mcp3008.c @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include "log.h" + + +#define MCP3008_SPEED 3600000 +#define MCP3008_BPW 8 + +#define MCP3008_TX_WORD1 0x01 /* 0b00000001 */ +#define MCP3008_TX_CH0 0x80 /* 0b10000000 */ +#define MCP3008_TX_CH1 0x90 /* 0b10010000 */ +#define MCP3008_TX_CH2 0xA0 /* 0b10100000 */ +#define MCP3008_TX_CH3 0xB0 /* 0b10110000 */ +#define MCP3008_TX_CH4 0xC0 /* 0b11000000 */ +#define MCP3008_TX_CH5 0xD0 /* 0b11010000 */ +#define MCP3008_TX_CH6 0xE0 /* 0b11100000 */ +#define MCP3008_TX_CH7 0xF0 /* 0b11110000 */ +#define MCP3008_TX_WORD3 0x00 /* 0b00000000 */ + +#define MCP3008_RX_WORD1_MASK 0x00 /* 0b00000000 */ +#define MCP3008_RX_WORD2_NULL_BIT_MASK 0x04 /* 0b00000100 */ +#define MCP3008_RX_WORD2_MASK 0x03 /* 0b00000011 */ +#define MCP3008_RX_WORD3_MASK 0xFF /* 0b11111111 */ +#define UINT10_VALIDATION_MASK 0x3FF + +#define MODEL_NAME_KEY "http://tizen.org/system/model_name" +#define MODEL_NAME_RPI3 "rpi3" +#define MODEL_NAME_ARTIK "artik" + +static peripheral_spi_h MCP3008_H = NULL; +static unsigned int ref_count = 0; + +int resource_adc_mcp3008_init(void) +{ + int ret = 0; + int bus = -1; + char *model_name = NULL; + + if (MCP3008_H) { + _D("SPI device aleady initialized [ref_count : %u]", ref_count); + ref_count++; + return 0; + } + + system_info_get_platform_string(MODEL_NAME_KEY, &model_name); + if (!model_name) { + _E("fail to get model name"); + return -1; + } + + if (!strcmp(model_name, MODEL_NAME_RPI3)) { + bus = 0; + } else if (!strcmp(model_name, MODEL_NAME_ARTIK)) { + bus = 2; + } else { + _E("unknown model name : %s", model_name); + free(model_name); + return -1; + } + free(model_name); + model_name = NULL; + + ret = peripheral_spi_open(bus, 0, &MCP3008_H); + if (PERIPHERAL_ERROR_NONE != ret) { + _E("spi open failed :%s ", get_error_message(ret)); + return -1; + } + + ret = peripheral_spi_set_mode(MCP3008_H, PERIPHERAL_SPI_MODE_0); + if (PERIPHERAL_ERROR_NONE != ret) { + _E("peripheral_spi_set_mode failed :%s ", get_error_message(ret)); + goto error_after_open; + } + ret = peripheral_spi_set_bit_order(MCP3008_H, PERIPHERAL_SPI_BIT_ORDER_MSB); + if (PERIPHERAL_ERROR_NONE != ret) { + _E("peripheral_spi_set_bit_order failed :%s ", get_error_message(ret)); + goto error_after_open; + } + + ret = peripheral_spi_set_bits_per_word(MCP3008_H, MCP3008_BPW); + if (PERIPHERAL_ERROR_NONE != ret) { + _E("peripheral_spi_set_bits_per_word failed :%s ", get_error_message(ret)); + goto error_after_open; + } + + ret = peripheral_spi_set_frequency(MCP3008_H, MCP3008_SPEED); + if (PERIPHERAL_ERROR_NONE != ret) { + _E("peripheral_spi_set_frequency failed :%s ", get_error_message(ret)); + goto error_after_open; + } + + ref_count++; + + return 0; + +error_after_open: + peripheral_spi_close(MCP3008_H); + MCP3008_H = NULL; + return -1; +} + + +int resource_read_adc_mcp3008(int ch_num, unsigned int *out_value) +{ + unsigned char rx[3] = {0, }; + unsigned char tx[3] = {0, }; + unsigned char rx_w1 = 0; + unsigned char rx_w2 = 0; + unsigned char rx_w2_nb = 0; + unsigned char rx_w3 = 0; + unsigned short int result = 0; + + retv_if(MCP3008_H == NULL, -1); + retv_if(out_value == NULL, -1); + retv_if((ch_num < 0 || ch_num > 7), -1); + + tx[0] = MCP3008_TX_WORD1; + switch (ch_num) { + case 0: + tx[1] = MCP3008_TX_CH0; + break; + case 1: + tx[1] = MCP3008_TX_CH1; + break; + case 2: + tx[1] = MCP3008_TX_CH2; + break; + case 3: + tx[1] = MCP3008_TX_CH3; + break; + case 4: + tx[1] = MCP3008_TX_CH4; + break; + case 5: + tx[1] = MCP3008_TX_CH5; + break; + case 6: + tx[1] = MCP3008_TX_CH6; + break; + case 7: + tx[1] = MCP3008_TX_CH7; + break; + default: + tx[1] = MCP3008_TX_CH0; + break; + } + tx[2] = MCP3008_TX_WORD3; + + peripheral_spi_transfer(MCP3008_H, tx, rx, 3); + + rx_w1 = rx[0] & MCP3008_RX_WORD1_MASK; + retv_if(rx_w1 != 0, -1); + + rx_w2_nb = rx[1] & MCP3008_RX_WORD2_NULL_BIT_MASK; + retv_if(rx_w2_nb != 0, -1); + + rx_w2 = rx[1] & MCP3008_RX_WORD2_MASK; + rx_w3 = rx[2] & MCP3008_RX_WORD3_MASK; + + result = ((rx_w2 << 8) | (rx_w3)) & UINT10_VALIDATION_MASK; + + // _D("%hu", result); + + *out_value = result; + + return 0; +} + +void resource_adc_mcp3008_fini(void) +{ + if (MCP3008_H) + ref_count--; + else + return; + + if (ref_count == 0) { + peripheral_spi_close(MCP3008_H); + MCP3008_H = NULL; + } + + return; +} + diff --git a/src/resource/resource_sound_level_sensor.c b/src/resource/resource_sound_level_sensor.c new file mode 100644 index 0000000..9118862 --- /dev/null +++ b/src/resource/resource_sound_level_sensor.c @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Contact: Jin Yoon + * Geunsun Lee + * Eunyoung Lee + * Junkyu Han + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "log.h" +#include "resource/resource_adc_mcp3008.h" + +static bool initialized = false; + +void resource_close_sound_level_sensor(void) +{ + resource_adc_mcp3008_fini(); + initialized = false; +} + +int resource_read_sound_level_sensor(int ch_num, unsigned int *out_value) +{ + unsigned int read_value = 0; + int ret = 0; + + if (!initialized) { + ret = resource_adc_mcp3008_init(); + retv_if(ret != 0, -1); + initialized = true; + } + ret = resource_read_adc_mcp3008(ch_num, &read_value); + retv_if(ret != 0, -1); + + *out_value = read_value; + + return 0; +} + -- 2.7.4