#include "motorboard.h" #include #include "PinMonitor.h" #include "uartregsaccess.h" #include #include "stringex.hpp" #include "config.h" #include "StopWatch.h" #include "applog.h" #include "Capturer.h" static const std::string loggername = "MotorBoard"; MotorBoard::MotorBoard() : devPort(MOTOR_UART), m_glue({nullptr, nullptr, nullptr,nullptr,nullptr,nullptr}) { LOG_INIT(); m_uartEnable.reset(new GpioOut(152)); m_uartEnable->setDirection(Gpio::out); // m_uartEnable->setEdge(Gpio::rising); //m_uartEnable->setValue(Gpio::Low); std::this_thread::sleep_for(std::chrono::milliseconds(10)); m_regsAccess.reset(new UartRegsAccess(devPort, bauds, 0x07, 0x87)); m_intPinMonitor.reset(new PinMonitor(intport, std::bind(&MotorBoard::pin_call, this, std::placeholders::_1))); //m_scansensorMonitor.reset(new PinMonitor(149, std::bind(&MotorBoard::scansensor_call, this, std::placeholders::_1))); //m_uartEnable->setValue(Gpio::High); std::this_thread::sleep_for(std::chrono::milliseconds(10)); m_os_mode = os_mode(); } static int paperinnum = 0; void MotorBoard::start() { keep_last_paper=false; unsigned int val; SMBCONFIG *smbc = (SMBCONFIG *)(&val); read(0, val); smbc->enable = 0; write(0, val); smbc->enable = 1; write(0, val); paperinnum =0 ; } void MotorBoard::stop() { unsigned int val; SMBCONFIG *smbc = (SMBCONFIG *)(&val); read(0, val); smbc->enable = 0; write(0, val); } void MotorBoard::pick_paper(void) { unsigned int val; SMBCONFIG *smbc = (SMBCONFIG *)(&val); read(0x00, val); smbc->pick_paper = 0; write(0x00, val); smbc->pick_paper = 1; write(0x00, val); // smbc->pick_paper = 0; // write(0x00, val); } void MotorBoard::clear_error() { unsigned int val; SMBCONFIG *smbc = (SMBCONFIG *)(&val); read(0, val); smbc->error_clean = 1; write(0, val); smbc->error_clean = 0; write(0, val); } bool MotorBoard::wait_paper_in(int timeout_ms) { return cv_paper_in.wait(timeout_ms); } bool MotorBoard::wait_paper_out(int timeout_ms) { return cv_paper_out.wait(timeout_ms); } bool MotorBoard::wait_error(int timeout_ms) { return cv_error.wait(timeout_ms); } bool MotorBoard::wait_done(int timeout_ms) { return cv_scan_done.wait(timeout_ms); } int MotorBoard::os_mode() { unsigned int val; read(0x02, val); SMB_MODE *smb_mode = (SMB_MODE *)&val; return smb_mode->scan_mode; } bool MotorBoard::paper_ready() { unsigned int val; read(0x02, val); SMB_MODE *smb_mode = (SMB_MODE *)&val; return smb_mode->feeding_paper_ready; } bool MotorBoard::is_scanning() { unsigned int val; read(0x02, val); SMB_MODE *smb_mode = (SMB_MODE *)&val; return smb_mode->work_status; } int MotorBoard::paper_counter() { unsigned int val; read(0x02, val); SMBMODE *smb_mode = (SMBMODE *)&val; return smb_mode->scan_num; } bool MotorBoard::set_paper_inspect_param(unsigned int value /* = 1000 */) { unsigned int val; if (!read(0x04, val)) return false; SMBCONFIGEXT *smb_config_ext = (SMBCONFIGEXT *)&val; smb_config_ext->error_range_set = value; return write(0x04, val); } bool MotorBoard::get_keeplastpaper(){ return keep_last_paper; } bool MotorBoard::set_paper_inpect_info(unsigned int value) { unsigned int val; if (!read(0x04, val)) return false; SMBCONFIGEXT *smb_config_ext = (SMBCONFIGEXT *)&val; smb_config_ext->paper_infor = value; return write(0x04, val); } bool MotorBoard::set_paper_inspect(bool enable /* = true */) { unsigned int val; if (!read(0x04, val)) return false; SMBCONFIGEXT *smb_config_ext = (SMBCONFIGEXT *)&val; smb_config_ext->paper_size_check_en = enable; return write(0x04, val); } bool MotorBoard::set_double_inpect(bool enable) { unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->double_paper = enable; return write(0x00, val); } bool MotorBoard::get_doublle_inpect() { } bool MotorBoard::set_auto_paper(bool enable){ unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->paper_auto_module = enable; return write(0x00, val); } bool MotorBoard::set_staple_inpect(bool enable) { unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->staple_enable = enable; return write(0x00, val); } bool MotorBoard::get_staple_inpect() { } bool MotorBoard::set_color_mode(int mode) { unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->color_mode = mode; return write(0x00, val); } int MotorBoard::get_color_mode() { } bool MotorBoard::set_speed_mode(int mode) { unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->speed_set_enable = 1; smb_config->v_setting = mode; write(0x00, val); smb_config->speed_set_enable = 0; return write(0x00, val); } int MotorBoard::get_speed_mode() { unsigned int val; if (!read(0x00, val)) return -1; SMBCONFIG *smb_config = (SMBCONFIG *)&val; return smb_config->v_setting; } bool MotorBoard::set_cuospeed(unsigned int speed) { unsigned int val; if (!read(0x04, val)) return -1; SMB_CONFIG_EXT *smb_config = (SMB_CONFIG_EXT *)&val; smb_config->cuo_speed = speed; return write(0x04,val); } std::shared_ptr MotorBoard::regs() { return m_regsAccess; } static int pinindex=0; void MotorBoard::pin_call(unsigned int pinNum) { static int index = 0; LOG_TRACE(string_format("pin %d", index++)); int os_m = os_mode(); if (m_os_mode != os_m) { m_os_mode = os_m; cv_os_mode.notify_all(); if (m_glue.m_os_mode_call) m_glue.m_os_mode_call(m_os_mode); } if (m_os_mode) { LOG_TRACE("not scan mode"); return; } unsigned int val; SMBSTATUS *smb_status = (SMBSTATUS *)&val; if (!read(0x01, val)) LOG_TRACE("read error"); LOG_TRACE(string_format("status %08x", val)); //printf("\n reg 1 val =%d",val); if(val & 0x800){ //printf("\n keep_last_paper "); keep_last_paper=true; } if(val & 0x1000) { if(m_glue.m_set_sleepmode_call) m_glue.m_set_sleepmode_call(val & 0xf000); } if(val & 0x80000) { if(m_glue.m_mltop_call) m_glue.m_mltop_call(val); } if(smb_status->paper_auto) { if(m_glue.m_auto_paper) m_glue.m_auto_paper(1); } if (val & 0xAFE) { cv_error.notify_all(); if (m_glue.m_error_call) m_glue.m_error_call(val & 0x30efe); //0xefe index of 16:aquireimage error index of bit 17 :size check error LOG_TRACE("error"); return; } else { if (!smb_status->scan_pulse) { cv_paper_in.notify_all(); LOG_TRACE("paper in"); printf("\n paper pulse num = %d ", paperinnum++); } if(smb_status->paper_left) { cv_paper_out.notify_all(); LOG_TRACE("paper left"); } } if (val & 0x400) { LOG_TRACE("done"); cv_scan_done.notify_all(); if (m_glue.m_scan_done_call) m_glue.m_scan_done_call(); } } void MotorBoard::set_capture(std::shared_ptr cap) { m_cap= cap; } void MotorBoard::scansensor_call(unsigned int pinNum) { // static int indexscansensor=0; // m_uartEnable->setValue(Gpio::High); // LOG_TRACE(string_format(" gpio149 call times -%d ", indexscansensor++)); // cv_paper_in.notify_all(); // m_uartEnable->setValue(Gpio::Low); } bool MotorBoard::write(unsigned int addr, unsigned int val) { return m_regsAccess->write(addr, val); } bool MotorBoard::read(unsigned int addr, unsigned int &val) { return m_regsAccess->read(addr, val); } void MotorBoard::set_callbacks(MotorBoardGlue glue) { m_glue = glue; } bool MotorBoard::set_screw_inpect(bool enable) { unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->skew_enable = enable; return write(0x00, val); } bool MotorBoard::get_screw_inpect() { unsigned int val; read(0x00, val); SMBCONFIG *smb_mode = (SMBCONFIG *)&val; return smb_mode->skew_enable; } bool MotorBoard::set_screw_level(int level) { unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->skew_parameter = level; return write(0x00, val); } bool MotorBoard::set_long_paper(bool enable) { unsigned int val; if (!read(0x00, val)) return false; SMBCONFIG *smb_config = (SMBCONFIG *)&val; smb_config->paper = enable; return write(0x00, val); } int MotorBoard::get_screw_level() { unsigned int val; auto ret= read(0x00, val); if(!ret) return -1; SMBCONFIG *smb_mode = (SMBCONFIG *)&val; return smb_mode->skew_parameter; } bool MotorBoard::en_testbit(bool en) { unsigned int val; auto ret= read(0x00, val); if(!ret) return -1; SMBCONFIG *smb_mode = (SMBCONFIG *)&val; smb_mode->testbit = en?1:0; return write(0x00,val); } void MotorBoard::release_statecontrol() { set_auto_paper(false); m_regsAccess.reset(); m_intPinMonitor.reset(); } void MotorBoard::init_statecontrol() { m_regsAccess.reset(new UartRegsAccess(devPort, bauds, 0x07, 0x87)); m_intPinMonitor.reset(new PinMonitor(intport, std::bind(&MotorBoard::pin_call, this, std::placeholders::_1))); std::this_thread::sleep_for(std::chrono::milliseconds(10)); m_os_mode = os_mode(); }