MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / encoder_sample

Function encoder_sample

bsp/hc32/tests/test_pulse_encoder.c:163–222  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

161}
162
163static int encoder_sample(int argc, char **argv)
164{
165 rt_int32_t count;
166
167 if ((argc != 4))
168 {
169 _pulse_cmd_print_usage();
170 return -RT_ERROR;
171 }
172
173 rt_pin_mode(TEST_IO_A_PIN, PIN_MODE_OUTPUT);
174 rt_pin_mode(TEST_IO_B_PIN, PIN_MODE_OUTPUT);
175
176 pulse_encoder_dev = rt_device_find(argv[1]);
177 if (pulse_encoder_dev == RT_NULL)
178 {
179 rt_kprintf("encoder_sample run failed! can't find %s device!\n", argv[1]);
180 _pulse_cmd_print_usage();
181 return -RT_ERROR;
182 }
183 rt_device_open(pulse_encoder_dev, RT_DEVICE_OFLAG_RDONLY);
184 rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_CLEAR_COUNT, RT_NULL);
185 rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_ENABLE, RT_NULL);
186
187 /* 自测DISABLE和CLEAR功能 */
188 GenClkUp(100);
189 rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_DISABLE, RT_NULL);
190 /* 测试DISABLE后是否还会计数 */
191 GenClkUp(10);
192 rt_device_read(pulse_encoder_dev, 0, &count, 1);
193 rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_CLEAR_COUNT, RT_NULL);
194 if (count != 100)
195 {
196 rt_kprintf("**************Self-test failed**************\n");
197 rt_device_close(pulse_encoder_dev);
198 _pulse_cmd_print_usage();
199 return -RT_ERROR;
200 }
201 else
202 {
203 rt_kprintf("**************Self-test success**************\n");
204 rt_device_control(pulse_encoder_dev, PULSE_ENCODER_CMD_ENABLE, RT_NULL);
205 GenClkUp(atoi(argv[2]));
206 GenClkDown(atoi(argv[3]));
207
208 rt_device_read(pulse_encoder_dev, 0, &count, 1);
209 if (count == (atoi(argv[2]) - atoi(argv[3])))
210 {
211 rt_kprintf("encoder_sample test success\n");
212 }
213 else
214 {
215 rt_kprintf("encoder_sample test failed\n");
216 }
217
218 rt_device_close(pulse_encoder_dev);
219 }
220

Callers

nothing calls this directly

Calls 10

_pulse_cmd_print_usageFunction · 0.85
rt_device_findFunction · 0.85
rt_kprintfFunction · 0.85
rt_device_openFunction · 0.85
rt_device_controlFunction · 0.85
GenClkUpFunction · 0.85
rt_device_readFunction · 0.85
rt_device_closeFunction · 0.85
GenClkDownFunction · 0.85
rt_pin_modeFunction · 0.50

Tested by

no test coverage detected