/*
 * @Date: 2025-07-15 11:12:00
 * @Author: mypx
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-10-24 10:05:49
 * @FilePath: data_sampling.c
 * @Description: 
 * Copyright (c) 2025 by mypx, All Rights Reserved.
 */
#include "data_sampling.h"
#include "et_encoder_utils.h"
#include "et_log.h"
#include "pm_common.h"
#include "servo.h"
#include <stdlib.h>
#include <string.h>
#include "bsp_collect.h"

uint16_t adc_raw_buffer[DATA_PROCESS_UNIT_COUNT] = {0};

float                  sampling_rate = 0;
static void           *mq_pool[8];
struct rt_messagequeue adc_mq;


/* 整个缓冲区完成回调 */
void light_data_sampling_complete(void)
{
    uint16_t *ptr = &adc_raw_buffer[0];
    //bsp_system_led_toggle();
    if (rt_mq_send(&adc_mq, &ptr, sizeof(ptr)) != RT_EOK)
    {
        ET_ERR("ADC MQ full!");
    }
}


int light_data_sampling_init(void)
{
    bsp_light_data_sampling_init(adc_raw_buffer, DATA_PROCESS_UNIT_COUNT, light_data_sampling_complete);
    pm_encoder_init();
    //pm_timestamp_tim_init();
    pm_encoder_start();
    encoder_cpr = pm_encoder_get_cpr();

    rt_mq_init(&adc_mq, "adc_mq", mq_pool, sizeof(void *), sizeof(mq_pool), RT_IPC_FLAG_FIFO);
    // test-> set state manually
    //pm_set_state(PM_CALIBRATION_STATE);
    sampling_rate = bsp_sampling_tim_get_freq();
    ET_WARN("Sampling rate:%.2f", sampling_rate);

    return 0;
}


void m_start(int argc, char **argv)
{
    if (argc != 2)
    {
        rt_kprintf("Usage: my_cmd <int_value>\n");
        return;
    }

    int rpm = atof(argv[1]); // 字符串转整型
    //ET_INFO("rpm:%d", rpm);
    sv630p_speed_mode_start(&sv630p_handle, rpm);
}
MSH_CMD_EXPORT_ALIAS(m_start, m_start, test);

void m_stop(void)
{
    sv630p_speed_mode_stop(&sv630p_handle);
}
MSH_CMD_EXPORT(m_stop, motor stop);

void m_back(void)
{
    sv630p_speed_mode_stop(&sv630p_handle);
    sv630p_speed_mode_start(&sv630p_handle, -50);
}
MSH_CMD_EXPORT(m_back, motor backward);

void m_fast(void)
{
    sv630p_speed_mode_start(&sv630p_handle, 400);
}
MSH_CMD_EXPORT(m_fast, motor fast forward);
