monoZ docs
MZ_Addon_board_UV.c
Go to the documentation of this file.
1/*
2 * Objectives covered in this Example :
3 * 1. Demonstrate how to create a simple recursive Timer using MonoZ_Lib
4 * 2. Demonstrate how to create a Application Thread using MonoZ_Lib
5 * 3. Demonstrate how to send and receive data in polling mode I2C using
6 * MonoZ_Lib.
7 *
8 * Sources for this example :
9 * MZ_Addon_board_UV.c - Main Application file
10 * MZ_Addon_board_UV.h
11 * MZ_Addon_board_Si1133_i2c.c - i2c driver file
12 * MZ_Addon_board_Si1133_i2c.h
13 * MZ_Addon_Si1133.c - Si1133 sensor driver file
14 * MZ_Addon_Si1133.h
15 *
16 * [Other Linked files]
17 * MZ_hardware_config.c - Hardware Driver integration file
18 * MZ_modem_config.c - Modem command integration file
19 */
20
21
22/*
23 * MZ_Addon_board_UV.c
24 *
25 * Created on: Aug 25, 2021
26 * Author: SKM
27 */
28
29#include "MZ_sys_cmsis_os2.h"
30#include "MZ_timer.h"
31#include "MZ_print.h"
32#include "stm32l4xx_hal_i2c.h"
33#include "MZ_Addon_Si1133.h"
34#include "stdio.h"
35
36/* Thread related MACRO and variables - START */
37#define UV_APP_STACK_SIZE (512) /* Stack size for the thread */
38static mz_thread_t uv_thread_id = NULL; /* Thread id handler */
39static StaticTask_t uv_cb_mem; /* Thread control block */
40static StackType_t uv_stack[UV_APP_STACK_SIZE]; /* Thread stack */
41/* Thread related MACRO and variables - END */
42
43/* Timer related MACRO and variables - START */
44#define UV_READ_TIME (pdMS_TO_TICKS(60000))
45static char timer_expiry_flag = 0;
46/* Timer related MACRO and variables - END */
47
48/* UV sensor related MACRO and variables - START */
49static char sensor_init = SI_FAIL;
50/* UV sensor related MACRO and variables - END */
51
52/*
53 * This is a helper function to convert a floating point variable to string
54 * with 4 decimal points
55 */
56static void float_to_str(char * str,float val)
57{
58 char *_sign = (val < 0) ? "-" : "";
59 float _tmp_val = (val < 0) ? -val : val;
60
61 int _int_part = _tmp_val;
62 float _frac_part = _tmp_val - _int_part;
63 int _frac_part_int = trunc(_frac_part * 10000);
64
65 /* Build the string from individual parts */
66 sprintf (str, "%s%d.%04d", _sign, _int_part, _frac_part_int);
67}
68
69/*
70 * UV read timer callback - START
71 *
72 * This Timer callback will be called after the UV read timer is expired.
73 * The timer value is set as UV_READ_TIME
74 */
75static void uv_read_timer_cb(TimerHandle_t xTimer)
76{
77 mz_puts("Timer expires - start read of Si1133 sensor\r\n");
78
79 /* Set the timer expired flag */
81}
82/* UV read timer callback - END */
83
84/*
85 * UV Application thread API. - START
86 *
87 * 1. It Create and start UV sensor reading timer
88 * 2. It initialize the Si1133 sensor
89 * 3. It check the timer expire flag and read the Ambient light intensity(lux)
90 * and UV index(uvi)
91 * 4. It print the information on CLI interface
92 */
93static void uv_app(void * arg)
94{
95 (void)arg;
96
97 /*
98 * create the UV reading timer.
99 * As per requirement, We are creating a recursive timer using
100 * mz_tm_create_start_recursive() API.
101 * In-case of other type of timer, please refer MZ_timer.h
102 * The expire time for this timer set to UV_READ_TIME.
103 * "uv_read_timer_cb" is passed as an argument.
104 * When the timer expires, the "uv_read_timer_cb" API will be processed.
105 */
106 if(MZ_OK == mz_tm_create_start_recursive( "UV Read timer",
109 {
110 /* initialize the Si1133 sensor */
112
113 if(SI_OK == sensor_init)
114 {
115 mz_puts("SENSOR Initialization complete...\r\n");
116 }
117 }
118
119 /*
120 * This is the infinite loop for this thread - the thread will execute this
121 * loop forever and not come outside of this loop
122 */
123 while(1)
124 {
125 /* Need to write the periodic executing logic in this loop block */
126
127 /* UV read timer expired */
128 if(1 == timer_expiry_flag)
129 {
131
132 /* Read the lux and uvi */
133 static float _lux,_uvi;
134 (void)SI1133_measure_lux_uv(&_lux,&_uvi);
135
136 /* Print the lux and uvi on the CLI */
137 char print_buff[50];
138 float_to_str(print_buff, _lux);
139 mz_puts("Lux = ");
140 mz_puts(print_buff);
141 mz_puts("\r\n");
142 float_to_str(print_buff, _uvi);
143 mz_puts("Uvi = ");
144 mz_puts(print_buff);
145 mz_puts("\r\n");
146 }
147
148 /* Add a delay for Non-blocking */
149 HAL_Delay(1);
150
151 }//End of while(1) - Do not place any code after this.
152}
153/* UV Application thread API. - END */
154
155/*
156 * UV Application initialization API. - START
157 *
158 * 1. It create the main UV application
159 */
161{
162 mz_error_t _ret = MZ_OK;
163
164 // Create the UV application threads
166 "UV reading app",
167 uv_app,
168 NULL,
169 osPriorityNormal,
170 uv_stack,
172 &uv_cb_mem,
173 sizeof(uv_cb_mem)))
174 {
176 }
177
178 return _ret;
179}
180/* UV Application initialization API. - END */
Si1133_error SI1133_measure_lux_uv(float *lux, float *uvi)
Si1133_error SI1133_init(void)
@ SI_OK
@ SI_FAIL
#define UV_READ_TIME
static void uv_app(void *arg)
static StaticTask_t uv_cb_mem
static char timer_expiry_flag
static void float_to_str(char *str, float val)
static mz_thread_t uv_thread_id
mz_error_t uv_app_init(void)
static char sensor_init
static void uv_read_timer_cb(TimerHandle_t xTimer)
#define UV_APP_STACK_SIZE
static StackType_t uv_stack[UV_APP_STACK_SIZE]
mz_error_t
Enumeration of monoZ Error Flags.
@ MZ_THREAD_CREATE_FAIL
@ MZ_OK
monoZ Print
monoZ system CMSIS OS2 This is an abstraction layer and includes FreeRTOS configuration,...
osThreadId_t mz_thread_t
mz_thread_t variable of type osThreadId_t
This file contains APIs for create,delete,start and stop of timers. Maximum 5 timers can be created....
int mz_puts(void *__ch)
This function prints the given string.
uint8_t mz_thread_create(mz_thread_t *t, const char *name, mz_fn thread_func, void *const arg, mz_thread_prio_t prio, StackType_t *stack_mem, size_t stack_size, StaticTask_t *cb_mem, uint32_t cb_size)
This function is used to check the message queue object validity Create a thread and add it to Active...
mz_error_t mz_tm_create_start_recursive(char *_name, TickType_t _tick, mz_tm_cb _cb)
Function to Create and Start One Shot Timers.