Initial commit.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
parent
d333d68a96
commit
79a5b1fc18
|
@ -0,0 +1,12 @@
|
|||
BasedOnStyle: Google
|
||||
IndentWidth: 4
|
||||
AlignConsecutiveMacros: Consecutive
|
||||
AlignConsecutiveDeclarations: Consecutive
|
||||
AlignConsecutiveAssignments: Consecutive
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
BreakBeforeBraces: Custom
|
||||
BraceWrapping:
|
||||
AfterEnum: false
|
||||
AfterStruct: false
|
||||
SplitEmptyFunction: false
|
||||
ColumnLimit: 120
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "lib/sensors"]
|
||||
path = lib/sensors
|
||||
url = https://git.minori.work/Embedded_Projects/imsensors.git
|
|
@ -3,7 +3,8 @@ cmake_minimum_required(VERSION 3.12)
|
|||
# Pull in SDK (must be before project)
|
||||
include(pico_sdk_import.cmake)
|
||||
|
||||
project(hello_world C CXX ASM)
|
||||
project(sensors C CXX ASM)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
|
@ -11,6 +12,7 @@ set(CMAKE_CXX_STANDARD 17)
|
|||
pico_sdk_init()
|
||||
|
||||
set(TARGET_C_SOURCES
|
||||
"src/app_sensors_impl.c"
|
||||
"src/main.c"
|
||||
)
|
||||
|
||||
|
@ -20,8 +22,12 @@ set(TARGET_INCLUDE_DIRS
|
|||
|
||||
set(TARGET_LIBRARIES
|
||||
pico_stdlib
|
||||
hardware_i2c
|
||||
imsensors
|
||||
)
|
||||
|
||||
add_subdirectory(lib/sensors)
|
||||
|
||||
add_executable(${CMAKE_PROJECT_NAME} ${TARGET_C_SOURCES})
|
||||
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE ${TARGET_INCLUDE_DIRS})
|
||||
|
||||
|
@ -32,5 +38,5 @@ target_link_libraries(${CMAKE_PROJECT_NAME} ${TARGET_LIBRARIES})
|
|||
pico_add_extra_outputs(${CMAKE_PROJECT_NAME})
|
||||
|
||||
# STDIO configuration
|
||||
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME} 1)
|
||||
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME} 0)
|
||||
pico_enable_stdio_usb(${CMAKE_PROJECT_NAME} 0)
|
||||
pico_enable_stdio_uart(${CMAKE_PROJECT_NAME} 1)
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
#ifndef APP_SENSORS_IMPL_H
|
||||
#define APP_SENSORS_IMPL_H
|
||||
|
||||
/* Hardware APIs */
|
||||
#include "hardware/i2c.h"
|
||||
|
||||
/* Sensors */
|
||||
#include "imsensors/common/sensors_common.h"
|
||||
|
||||
typedef struct {
|
||||
i2c_inst_t *i2c_inst;
|
||||
uint8_t scl_pin;
|
||||
uint8_t sda_pin;
|
||||
|
||||
/* Peripheral Specific */
|
||||
uint8_t dev_addr;
|
||||
} app_sensors_impl_t;
|
||||
|
||||
int app_sensors_impl_init(void *pdev);
|
||||
ims_ret_t app_sensors_i2c_xfer(void *pdev, ims_i2c_xfer_desc_t *xfer);
|
||||
ims_ret_t app_sensors_delay(void *pdev, uint32_t delay_msec);
|
||||
|
||||
#endif // APP_SENSORS_IMPL_H
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 8280fe22c9558f39e535ba70481128b2d635418c
|
|
@ -0,0 +1,53 @@
|
|||
/* Stdlib */
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
/* App */
|
||||
#include "app_sensors_impl.h"
|
||||
|
||||
int app_sensors_impl_init(void *pdev) {
|
||||
app_sensors_impl_t *impl = pdev;
|
||||
|
||||
i2c_init(impl->i2c_inst, 400000);
|
||||
|
||||
gpio_set_function(impl->sda_pin, GPIO_FUNC_I2C);
|
||||
gpio_set_function(impl->scl_pin, GPIO_FUNC_I2C);
|
||||
|
||||
gpio_pull_up(impl->sda_pin);
|
||||
gpio_pull_up(impl->scl_pin);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ims_ret_t app_sensors_i2c_xfer(void *pdev, ims_i2c_xfer_desc_t *xfer) {
|
||||
app_sensors_impl_t *impl = pdev;
|
||||
|
||||
if (xfer->tx_data) {
|
||||
bool no_stop = false;
|
||||
|
||||
if (xfer->rx_data) {
|
||||
no_stop = true;
|
||||
}
|
||||
|
||||
int ret = i2c_write_blocking(impl->i2c_inst, impl->dev_addr, xfer->tx_data, xfer->tx_size, no_stop);
|
||||
|
||||
if (ret != xfer->tx_size) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
if (xfer->rx_data) {
|
||||
int ret = i2c_read_blocking(impl->i2c_inst, impl->dev_addr, xfer->rx_data, xfer->rx_size, false);
|
||||
|
||||
if (ret != xfer->rx_size) {
|
||||
return IMS_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
||||
|
||||
ims_ret_t app_sensors_delay(void *pdev, uint32_t delay_msec) {
|
||||
sleep_ms(delay_msec);
|
||||
|
||||
return IMS_SUCCESS;
|
||||
}
|
86
src/main.c
86
src/main.c
|
@ -1,20 +1,92 @@
|
|||
#include <stdio.h>
|
||||
|
||||
/* Stdlib */
|
||||
#include "pico/stdlib.h"
|
||||
|
||||
/* Sensors */
|
||||
#include "imsensors/franklin/franklin_as3935.h"
|
||||
|
||||
/* App */
|
||||
#include "app_sensors_impl.h"
|
||||
|
||||
#define APP_SENSORS_I2C_INST PICO_DEFAULT_I2C_INSTANCE
|
||||
#define APP_SENSORS_I2C_SCL_PIN PICO_DEFAULT_I2C_SCL_PIN
|
||||
#define APP_SENSORS_I2C_SDA_PIN PICO_DEFAULT_I2C_SDA_PIN
|
||||
#define APP_SENSORS_LTN_IRQ_PIN 6
|
||||
|
||||
static app_sensors_impl_t s_sensor_impl = {
|
||||
.i2c_inst = APP_SENSORS_I2C_INST,
|
||||
.sda_pin = APP_SENSORS_I2C_SDA_PIN,
|
||||
.scl_pin = APP_SENSORS_I2C_SCL_PIN,
|
||||
|
||||
.dev_addr = 0x02U,
|
||||
};
|
||||
|
||||
static ims_as3935_t s_ltn = {
|
||||
.pdev = &s_sensor_impl,
|
||||
.cb =
|
||||
{
|
||||
.i2c_xfer = app_sensors_i2c_xfer,
|
||||
.delay = app_sensors_delay,
|
||||
},
|
||||
};
|
||||
|
||||
static volatile bool s_irq;
|
||||
|
||||
static void app_ltn_irq_callback(uint gpio, uint32_t event_mask);
|
||||
|
||||
int main(void) {
|
||||
stdio_init_all();
|
||||
|
||||
gpio_init(PICO_DEFAULT_LED_PIN);
|
||||
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
|
||||
app_sensors_impl_init(&s_sensor_impl);
|
||||
|
||||
printf("Pico: Hello world.\r\n");
|
||||
printf("Sensor interface initialized.\r\n");
|
||||
|
||||
gpio_set_irq_enabled_with_callback(APP_SENSORS_LTN_IRQ_PIN, GPIO_IRQ_EDGE_RISE, true, app_ltn_irq_callback);
|
||||
|
||||
ims_as3935_init(&s_ltn);
|
||||
|
||||
s_irq = false;
|
||||
|
||||
for (;;) {
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 1U);
|
||||
sleep_ms(1000);
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, 0U);
|
||||
sleep_ms(1000);
|
||||
while (s_irq == false) {
|
||||
//
|
||||
}
|
||||
|
||||
uint8_t irq_status;
|
||||
ims_as3935_get_irq_status(&s_ltn, &irq_status);
|
||||
|
||||
if (irq_status & IMS_AS3935_IRQ_TYPE_NH) {
|
||||
printf("Noise level too high, increasing threshold...\r\n");
|
||||
uint8_t nf;
|
||||
|
||||
ims_as3935_get_noise_floor(&s_ltn, &nf);
|
||||
printf("Current NF: %d\r\n", nf);
|
||||
|
||||
if (nf < 7) {
|
||||
nf++;
|
||||
|
||||
printf("Increasing NF to %d\r\n", nf);
|
||||
ims_as3935_set_noise_floor(&s_ltn, nf);
|
||||
}
|
||||
}
|
||||
|
||||
if (irq_status & IMS_AS3935_IRQ_TYPE_D) {
|
||||
printf("Disturber detected...\r\n");
|
||||
}
|
||||
|
||||
if (irq_status & IMS_AS3935_IRQ_TYPE_L) {
|
||||
printf("Lightning detected...\r\n");
|
||||
}
|
||||
|
||||
s_irq = false;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void app_ltn_irq_callback(uint gpio, uint32_t event_mask) {
|
||||
gpio_acknowledge_irq(gpio, event_mask);
|
||||
|
||||
s_irq = true;
|
||||
}
|
Loading…
Reference in New Issue