1 /**
2 * @file lv_monkey.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_monkey.h"
10
11 #if LV_USE_MONKEY != 0
12
13 /*********************
14 * DEFINES
15 *********************/
16 #define MONKEY_PERIOD_RANGE_MIN_DEF 100
17 #define MONKEY_PERIOD_RANGE_MAX_DEF 1000
18
19 /**********************
20 * TYPEDEFS
21 **********************/
22 typedef struct _lv_monkey {
23 lv_monkey_config_t config;
24 lv_indev_drv_t indev_drv;
25 lv_indev_data_t indev_data;
26 lv_indev_t * indev;
27 lv_timer_t * timer;
28 #if LV_USE_USER_DATA
29 void * user_data;
30 #endif
31 } lv_monkey_t;
32
33 static const lv_key_t lv_key_map[] = {
34 LV_KEY_UP,
35 LV_KEY_DOWN,
36 LV_KEY_RIGHT,
37 LV_KEY_LEFT,
38 LV_KEY_ESC,
39 LV_KEY_DEL,
40 LV_KEY_BACKSPACE,
41 LV_KEY_ENTER,
42 LV_KEY_NEXT,
43 LV_KEY_PREV,
44 LV_KEY_HOME,
45 LV_KEY_END,
46 };
47
48 /**********************
49 * STATIC PROTOTYPES
50 **********************/
51
52 static void lv_monkey_read_cb(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
53 static int32_t lv_monkey_random(int32_t howsmall, int32_t howbig);
54 static void lv_monkey_timer_cb(lv_timer_t * timer);
55
56 /**********************
57 * GLOBAL FUNCTIONS
58 **********************/
59
lv_monkey_config_init(lv_monkey_config_t * config)60 void lv_monkey_config_init(lv_monkey_config_t * config)
61 {
62 lv_memset_00(config, sizeof(lv_monkey_config_t));
63 config->type = LV_INDEV_TYPE_POINTER;
64 config->period_range.min = MONKEY_PERIOD_RANGE_MIN_DEF;
65 config->period_range.max = MONKEY_PERIOD_RANGE_MAX_DEF;
66 }
67
lv_monkey_create(const lv_monkey_config_t * config)68 lv_monkey_t * lv_monkey_create(const lv_monkey_config_t * config)
69 {
70 lv_monkey_t * monkey = lv_mem_alloc(sizeof(lv_monkey_t));
71 LV_ASSERT_MALLOC(monkey);
72
73 lv_memset_00(monkey, sizeof(lv_monkey_t));
74
75 monkey->config = *config;
76
77 lv_indev_drv_t * drv = &monkey->indev_drv;
78 lv_indev_drv_init(drv);
79 drv->type = config->type;
80 drv->read_cb = lv_monkey_read_cb;
81 drv->user_data = monkey;
82
83 monkey->timer = lv_timer_create(lv_monkey_timer_cb, monkey->config.period_range.min, monkey);
84 lv_timer_pause(monkey->timer);
85
86 monkey->indev = lv_indev_drv_register(drv);
87
88 return monkey;
89 }
90
lv_monkey_get_indev(lv_monkey_t * monkey)91 lv_indev_t * lv_monkey_get_indev(lv_monkey_t * monkey)
92 {
93 LV_ASSERT_NULL(monkey);
94 return monkey->indev;
95 }
96
lv_monkey_set_enable(lv_monkey_t * monkey,bool en)97 void lv_monkey_set_enable(lv_monkey_t * monkey, bool en)
98 {
99 LV_ASSERT_NULL(monkey);
100 en ? lv_timer_resume(monkey->timer) : lv_timer_pause(monkey->timer);
101 }
102
lv_monkey_get_enable(lv_monkey_t * monkey)103 bool lv_monkey_get_enable(lv_monkey_t * monkey)
104 {
105 LV_ASSERT_NULL(monkey);
106 return !monkey->timer->paused;
107 }
108
109 #if LV_USE_USER_DATA
110
lv_monkey_set_user_data(lv_monkey_t * monkey,void * user_data)111 void lv_monkey_set_user_data(lv_monkey_t * monkey, void * user_data)
112 {
113 LV_ASSERT_NULL(monkey);
114 monkey->user_data = user_data;
115 }
116
lv_monkey_get_user_data(lv_monkey_t * monkey)117 void * lv_monkey_get_user_data(lv_monkey_t * monkey)
118 {
119 LV_ASSERT_NULL(monkey);
120 return monkey->user_data;
121 }
122
123 #endif
124
lv_monkey_del(lv_monkey_t * monkey)125 void lv_monkey_del(lv_monkey_t * monkey)
126 {
127 LV_ASSERT_NULL(monkey);
128
129 lv_timer_del(monkey->timer);
130 lv_indev_delete(monkey->indev);
131 lv_mem_free(monkey);
132 }
133
134 /**********************
135 * STATIC FUNCTIONS
136 **********************/
137
lv_monkey_read_cb(lv_indev_drv_t * indev_drv,lv_indev_data_t * data)138 static void lv_monkey_read_cb(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
139 {
140 lv_monkey_t * monkey = indev_drv->user_data;
141
142 data->btn_id = monkey->indev_data.btn_id;
143 data->point = monkey->indev_data.point;
144 data->enc_diff = monkey->indev_data.enc_diff;
145 data->state = monkey->indev_data.state;
146 }
147
lv_monkey_random(int32_t howsmall,int32_t howbig)148 static int32_t lv_monkey_random(int32_t howsmall, int32_t howbig)
149 {
150 if(howsmall >= howbig) {
151 return howsmall;
152 }
153 int32_t diff = howbig - howsmall;
154 return (int32_t)lv_rand(0, diff) + howsmall;
155 }
156
lv_monkey_timer_cb(lv_timer_t * timer)157 static void lv_monkey_timer_cb(lv_timer_t * timer)
158 {
159 lv_monkey_t * monkey = timer->user_data;
160 lv_indev_data_t * data = &monkey->indev_data;
161
162 switch(monkey->indev_drv.type) {
163 case LV_INDEV_TYPE_POINTER:
164 data->point.x = (lv_coord_t)lv_monkey_random(0, LV_HOR_RES - 1);
165 data->point.y = (lv_coord_t)lv_monkey_random(0, LV_VER_RES - 1);
166 break;
167 case LV_INDEV_TYPE_ENCODER:
168 data->enc_diff = (int16_t)lv_monkey_random(monkey->config.input_range.min, monkey->config.input_range.max);
169 break;
170 case LV_INDEV_TYPE_BUTTON:
171 data->btn_id = (uint32_t)lv_monkey_random(monkey->config.input_range.min, monkey->config.input_range.max);
172 break;
173 case LV_INDEV_TYPE_KEYPAD: {
174 int32_t index = lv_monkey_random(0, sizeof(lv_key_map) / sizeof(lv_key_map[0]) - 1);
175 data->key = lv_key_map[index];
176 break;
177 }
178 default:
179 break;
180 }
181
182 data->state = lv_monkey_random(0, 100) < 50 ? LV_INDEV_STATE_RELEASED : LV_INDEV_STATE_PRESSED;
183
184 lv_timer_set_period(monkey->timer, lv_monkey_random(monkey->config.period_range.min, monkey->config.period_range.max));
185 }
186
187 #endif /*LV_USE_MONKEY*/
188