SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.

Dependencies:   TSI USBDevice mbed-dev

Fork of SmartWheels by haofan Zheng

RemovedSources/OV7725RegBuf.cpp.txt

Committer:
hazheng
Date:
2017-03-14
Revision:
36:7e747e19f660
Parent:
Hardwares/OV7725RegBuf.cpp@ 32:5badeff825dc

File content as of revision 36:7e747e19f660:

#include "OV7725RegBuf.h"

#include "PinAssignment.h"
#include "Core.h"
#include "SWUSBServer.h"

OV7725RegBuf::OV7725RegBuf(SW::Core & core) : 
    m_data(NULL),
    m_core(core),
    m_sccbCtrl(I2C(PIN_ORB_SDA, PIN_ORB_SCL))
{
    
}

OV7725RegBuf::~OV7725RegBuf()
{
    if(m_data)
    {
        delete [] m_data;
    }
}

uint8_t * OV7725RegBuf::GetData()
{
    if(!m_data)
        m_data = new uint8_t[OV7725_LAST_ADDR + 1];
    return m_data;
}

bool OV7725RegBuf::IsAddressReserved(const uint8_t addr)
{
    switch(addr)
    {
    case OV7725_RESV_01:
    case OV7725_RESV_02:
    case OV7725_RESV_03:
    case OV7725_RESV_04:
    case OV7725_RESV_05:
    case OV7725_RESV_06:
    case OV7725_RESV_07:
    case OV7725_RESV_08:
    case OV7725_RESV_09:
    case OV7725_RESV_10:
    case OV7725_RESV_11:
    case OV7725_RESV_12:
    case OV7725_RESV_13:
    case OV7725_RESV_14:
    case OV7725_RESV_15:
    case OV7725_RESV_16:
        return true;
        //break;
    default:
        return false;
        //break;
    }
}

//Blocking method. Do not use during the running state!!
void OV7725RegBuf::SCCBWrite(uint8_t RegAddr, uint8_t Data)
{   
    m_sccbCtrl.lock();
    m_sccbCtrl.frequency(OV7725_I2CFREQ);
    
    m_sccbCtrl.start();
    
    m_sccbCtrl.write(OV7725_WRITE);
    wait_us(OV7725_WRITEWAIT);
    m_sccbCtrl.write(RegAddr);
    wait_us(OV7725_WRITEWAIT);
    m_sccbCtrl.write(Data);
    wait_us(OV7725_WRITEWAIT);
    
    m_sccbCtrl.stop();
    
    m_sccbCtrl.unlock();
    
    wait_us(2);
}

//Blocking method. Do not use during the running state!!
uint8_t OV7725RegBuf::SCCBRead(const uint8_t RegAddr)
{
    m_sccbCtrl.lock();
    m_sccbCtrl.frequency(OV7725_I2CFREQ);
    
    m_sccbCtrl.start();
    
    m_sccbCtrl.write(OV7725_WRITE);
    wait_us(OV7725_WRITEWAIT);
    m_sccbCtrl.write(RegAddr);
    m_sccbCtrl.stop();
    wait_us(OV7725_WRITEWAIT);    
 
    m_sccbCtrl.start();
    m_sccbCtrl.write(OV7725_READ);
    wait_us(OV7725_WRITEWAIT);
    char readValue = m_sccbCtrl.read(OV7725_NOACK);
    
    m_sccbCtrl.stop();
    
    m_sccbCtrl.unlock();
    
    wait_us(2);
    
    return readValue;
}


//Blocking method. Do not use during the running state!!
void OV7725RegBuf::ReadRegisters()
{
    for(int i = 0; i < OV7725_LAST_ADDR + 1; ++i)
    {
        if(!OV7725RegBuf::IsAddressReserved(i))
        {
            char buf[10];
            sprintf(buf, "%#x-%#x", i, SCCBRead(i));
            m_core.GetUSBServer().PushUnreliableMsg('D', buf);
            
            wait(0.1);
        }
    }
}

#ifdef WRITE_DEFAULT_REG_ON
void OV7725RegBuf::WriteDefaultRegisters()
{
    SCCBWrite(0x0, 0x00);
    SCCBWrite(0x1, 0x80);
    SCCBWrite(0x2, 0x80);
    SCCBWrite(0x3, 0x00);
    SCCBWrite(0x5, 0x00);
    SCCBWrite(0x6, 0x00);
    SCCBWrite(0x7, 0x00);
    SCCBWrite(0x8, 0x00);
    SCCBWrite(0x9, 0x01);
    //SCCBWrite(0xa, 0x77); //Read-Only
    //SCCBWrite(0xb, 0x21); //Read-Only
    SCCBWrite(0xc, 0x10);
    SCCBWrite(0xd, 0x41);
    SCCBWrite(0xe, 0x01);
    SCCBWrite(0xf, 0x43);
    SCCBWrite(0x10, 0x40);
    SCCBWrite(0x11, 0x80);
    SCCBWrite(0x12, 0x00);
    SCCBWrite(0x13, 0x8f);
    SCCBWrite(0x14, 0x4A);
    SCCBWrite(0x15, 0x00);
    SCCBWrite(0x16, 0x00);
    SCCBWrite(0x17, 0x3F);
    SCCBWrite(0x18, 0x50);
    SCCBWrite(0x19, 0x03);
    SCCBWrite(0x1a, 0x78);
    SCCBWrite(0x1b, 0x40);
    //SCCBWrite(0x1c, 0x7f); //Read-Only
    //SCCBWrite(0x1d, 0xa2); //Read-Only
    SCCBWrite(0x1f, 0x00);
    SCCBWrite(0x20, 0x10);
    SCCBWrite(0x22, 0xff);
    SCCBWrite(0x23, 0x01);
    SCCBWrite(0x24, 0x75);
    SCCBWrite(0x25, 0x63);
    SCCBWrite(0x26, 0xD4);
    SCCBWrite(0x28, 0x00);
    SCCBWrite(0x29, 0x50);
    SCCBWrite(0x2a, 0x00);
    SCCBWrite(0x2b, 0x00);
    SCCBWrite(0x2c, 0x78);
    SCCBWrite(0x2d, 0x00);
    SCCBWrite(0x2e, 0x00);
    SCCBWrite(0x2f, 0x00);
    SCCBWrite(0x30, 0x80);
    SCCBWrite(0x31, 0x60);
    SCCBWrite(0x32, 0x00);
    SCCBWrite(0x33, 0x00);
    SCCBWrite(0x34, 0x00);
    SCCBWrite(0x35, 0x80);
    SCCBWrite(0x36, 0x80);
    SCCBWrite(0x37, 0x80);
    SCCBWrite(0x38, 0x80);
    SCCBWrite(0x39, 0x80);
    SCCBWrite(0x3a, 0x80);
    SCCBWrite(0x3b, 0x80);
    SCCBWrite(0x3c, 0x80);
    SCCBWrite(0x3d, 0x80);
    SCCBWrite(0x3e, 0xe2);
    SCCBWrite(0x3f, 0x1f);
    SCCBWrite(0x40, 0xC0);
    SCCBWrite(0x41, 0x08);
    SCCBWrite(0x42, 0x80);
    SCCBWrite(0x43, 0x80);
    SCCBWrite(0x44, 0x80);
    SCCBWrite(0x45, 0x80);
    SCCBWrite(0x46, 0x00);
    SCCBWrite(0x47, 0x00);
    SCCBWrite(0x48, 0x00);
    SCCBWrite(0x49, 0x50);
    SCCBWrite(0x4a, 0x30);
    SCCBWrite(0x4b, 0x50);
    SCCBWrite(0x4c, 0x50);
    SCCBWrite(0x4d, 0x00);
    SCCBWrite(0x4e, 0xef);
    SCCBWrite(0x4f, 0x10);
    SCCBWrite(0x50, 0x60);
    SCCBWrite(0x51, 0x00);
    SCCBWrite(0x52, 0x00);
    SCCBWrite(0x53, 0x24);
    SCCBWrite(0x54, 0x7a);
    SCCBWrite(0x55, 0xfc);
    SCCBWrite(0x60, 0x80);
    SCCBWrite(0x61, 0x80);
    SCCBWrite(0x62, 0xff);
    SCCBWrite(0x63, 0xf0);
    SCCBWrite(0x64, 0x1f);
    SCCBWrite(0x65, 0x00);
    SCCBWrite(0x66, 0x10);
    SCCBWrite(0x67, 0x00);
    SCCBWrite(0x68, 0x00);
    SCCBWrite(0x69, 0x5c);
    SCCBWrite(0x6a, 0x11);
    SCCBWrite(0x6b, 0xa2);
    SCCBWrite(0x6c, 0x01);
    SCCBWrite(0x6d, 0x50);
    SCCBWrite(0x6e, 0x80);
    SCCBWrite(0x6f, 0x80);
    SCCBWrite(0x70, 0x0f);
    SCCBWrite(0x71, 0x00);
    SCCBWrite(0x72, 0x00);
    SCCBWrite(0x73, 0x0f);
    SCCBWrite(0x74, 0x0f);
    SCCBWrite(0x75, 0xff);
    SCCBWrite(0x76, 0xff);
    SCCBWrite(0x77, 0xff);
    SCCBWrite(0x78, 0x10);
    SCCBWrite(0x79, 0x70);
    SCCBWrite(0x7a, 0x70);
    SCCBWrite(0x7b, 0xf0);
    SCCBWrite(0x7c, 0xf0);
    SCCBWrite(0x7d, 0xf0);
    SCCBWrite(0x7e, 0x0e);
    SCCBWrite(0x7f, 0x1a);
    SCCBWrite(0x80, 0x31);
    SCCBWrite(0x81, 0x5a);
    SCCBWrite(0x82, 0x69);
    SCCBWrite(0x83, 0x75);
    SCCBWrite(0x84, 0x7e);
    SCCBWrite(0x85, 0x88);
    SCCBWrite(0x86, 0x8f);
    SCCBWrite(0x87, 0x96);
    SCCBWrite(0x88, 0xa3);
    SCCBWrite(0x89, 0xaf);
    SCCBWrite(0x8a, 0xc4);
    SCCBWrite(0x8b, 0xd7);
    SCCBWrite(0x8c, 0xe8);
    SCCBWrite(0x8d, 0x20);
    SCCBWrite(0x8e, 0x00);
    SCCBWrite(0x8f, 0x00);
    SCCBWrite(0x90, 0x08);
    SCCBWrite(0x91, 0x10);
    SCCBWrite(0x92, 0x1f);
    SCCBWrite(0x93, 0x01);
    SCCBWrite(0x94, 0x2c);
    SCCBWrite(0x95, 0x24);
    SCCBWrite(0x96, 0x08);
    SCCBWrite(0x97, 0x14);
    SCCBWrite(0x98, 0x24);
    SCCBWrite(0x99, 0x38);
    SCCBWrite(0x9a, 0x9e);
    SCCBWrite(0x9b, 0x00);
    SCCBWrite(0x9c, 0x40);
    SCCBWrite(0x9e, 0x11);
    SCCBWrite(0x9f, 0x02);
    SCCBWrite(0xa0, 0x00);
    SCCBWrite(0xa1, 0x40);
    SCCBWrite(0xa2, 0x40);
    SCCBWrite(0xa3, 0x06);
    SCCBWrite(0xa4, 0x00);
    SCCBWrite(0xa6, 0x00);
    SCCBWrite(0xa7, 0x40);
    SCCBWrite(0xa8, 0x40);
    SCCBWrite(0xa9, 0x80);
    SCCBWrite(0xaa, 0x80);
    SCCBWrite(0xab, 0x06);
    SCCBWrite(0xac, 0xff);
}
#endif
    
#ifdef WRITE_ORIGIN_REG_ON
void OV7725RegBuf::WriteOriginalRegisters()
{
    SCCBWrite(0x0, 0xff);
    SCCBWrite(0x1, 0x40);
    SCCBWrite(0x2, 0x40);
    SCCBWrite(0x3, 0x40);
    SCCBWrite(0x5, 0x3);
    SCCBWrite(0x6, 0x4);
    SCCBWrite(0x7, 0x4);
    SCCBWrite(0x8, 0x1);
    SCCBWrite(0x9, 0);
    //SCCBWrite(0xa, 0x77); //Read-Only
    //SCCBWrite(0xb, 0x21); //Read-Only
    SCCBWrite(0xc, 0x10);
    SCCBWrite(0xd, 0x41);
    SCCBWrite(0xe, 0x79);
    SCCBWrite(0xf, 0xa9);
    SCCBWrite(0x10, 0xfc);
    SCCBWrite(0x11, 0);
    SCCBWrite(0x12, 0);
    SCCBWrite(0x13, 0xcf);
    SCCBWrite(0x14, 0x40);
    SCCBWrite(0x15, 0);
    SCCBWrite(0x16, 0);
    SCCBWrite(0x17, 0x26);
    SCCBWrite(0x18, 0xa0);
    SCCBWrite(0x19, 0x7);
    SCCBWrite(0x1a, 0xf0);
    SCCBWrite(0x1b, 0x40);
    //SCCBWrite(0x1c, 0x7f); //Read-Only
    //SCCBWrite(0x1d, 0xa2); //Read-Only
    SCCBWrite(0x1f, 0);
    SCCBWrite(0x20, 0x10);
    SCCBWrite(0x22, 0xff);
    SCCBWrite(0x23, 0x1);
    SCCBWrite(0x24, 0x58);
    SCCBWrite(0x25, 0x48);
    SCCBWrite(0x26, 0xc3);
    SCCBWrite(0x28, 0);
    SCCBWrite(0x29, 0xa0);
    SCCBWrite(0x2a, 0);
    SCCBWrite(0x2b, 0);
    SCCBWrite(0x2c, 0xf0);
    SCCBWrite(0x2d, 0);
    SCCBWrite(0x2e, 0);
    SCCBWrite(0x2f, 0x1);
    SCCBWrite(0x30, 0x80);
    SCCBWrite(0x31, 0x60);
    SCCBWrite(0x32, 0);
    SCCBWrite(0x33, 0);
    SCCBWrite(0x34, 0);
    SCCBWrite(0x35, 0x83);
    SCCBWrite(0x36, 0x82);
    SCCBWrite(0x37, 0x7c);
    SCCBWrite(0x38, 0x7c);
    SCCBWrite(0x39, 0x85);
    SCCBWrite(0x3a, 0x84);
    SCCBWrite(0x3b, 0x86);
    SCCBWrite(0x3c, 0x86);
    SCCBWrite(0x3d, 0x80);
    SCCBWrite(0x3e, 0xe2);
    SCCBWrite(0x3f, 0x1f);
    SCCBWrite(0x40, 0xe8);
    SCCBWrite(0x41, 0);
    SCCBWrite(0x42, 0x80);
    SCCBWrite(0x43, 0x80);
    SCCBWrite(0x44, 0x80);
    SCCBWrite(0x45, 0x80);
    SCCBWrite(0x46, 0);
    SCCBWrite(0x47, 0);
    SCCBWrite(0x48, 0);
    SCCBWrite(0x49, 0x50);
    SCCBWrite(0x4a, 0x30);
    SCCBWrite(0x4b, 0x50);
    SCCBWrite(0x4c, 0x50);
    SCCBWrite(0x4d, 0);
    SCCBWrite(0x4e, 0xef);
    SCCBWrite(0x4f, 0x10);
    SCCBWrite(0x50, 0x60);
    SCCBWrite(0x51, 0);
    SCCBWrite(0x52, 0);
    SCCBWrite(0x53, 0x24);
    SCCBWrite(0x54, 0x7a);
    SCCBWrite(0x55, 0xfc);
    SCCBWrite(0x60, 0);
    SCCBWrite(0x61, 0x5);
    SCCBWrite(0x62, 0xff);
    SCCBWrite(0x63, 0xf0);
    SCCBWrite(0x64, 0xbf);
    SCCBWrite(0x65, 0);
    SCCBWrite(0x66, 0x10);
    SCCBWrite(0x67, 0);
    SCCBWrite(0x68, 0);
    SCCBWrite(0x69, 0x5c);
    SCCBWrite(0x6a, 0x11);
    SCCBWrite(0x6b, 0xa2);
    SCCBWrite(0x6c, 0x1);
    SCCBWrite(0x6d, 0x50);
    SCCBWrite(0x6e, 0x80);
    SCCBWrite(0x6f, 0x80);
    SCCBWrite(0x70, 0xf);
    SCCBWrite(0x71, 0);
    SCCBWrite(0x72, 0);
    SCCBWrite(0x73, 0xf);
    SCCBWrite(0x74, 0xf);
    SCCBWrite(0x75, 0xff);
    SCCBWrite(0x76, 0);
    SCCBWrite(0x77, 0x10);
    SCCBWrite(0x78, 0x10);
    SCCBWrite(0x79, 0x70);
    SCCBWrite(0x7a, 0x70);
    SCCBWrite(0x7b, 0xf0);
    SCCBWrite(0x7c, 0xf0);
    SCCBWrite(0x7d, 0xf0);
    SCCBWrite(0x7e, 0xe);
    SCCBWrite(0x7f, 0x1a);
    SCCBWrite(0x80, 0x31);
    SCCBWrite(0x81, 0x5a);
    SCCBWrite(0x82, 0x69);
    SCCBWrite(0x83, 0x75);
    SCCBWrite(0x84, 0x7e);
    SCCBWrite(0x85, 0x88);
    SCCBWrite(0x86, 0x8f);
    SCCBWrite(0x87, 0x96);
    SCCBWrite(0x88, 0xa3);
    SCCBWrite(0x89, 0xaf);
    SCCBWrite(0x8a, 0xc4);
    SCCBWrite(0x8b, 0xd7);
    SCCBWrite(0x8c, 0xe8);
    SCCBWrite(0x8d, 0x20);
    SCCBWrite(0x8e, 0x4e);
    SCCBWrite(0x8f, 0x1);
    SCCBWrite(0x90, 0x8);
    SCCBWrite(0x91, 0x10);
    SCCBWrite(0x92, 0x1f);
    SCCBWrite(0x93, 0x1);
    SCCBWrite(0x94, 0x2c);
    SCCBWrite(0x95, 0x24);
    SCCBWrite(0x96, 0x8);
    SCCBWrite(0x97, 0x14);
    SCCBWrite(0x98, 0x24);
    SCCBWrite(0x99, 0x38);
    SCCBWrite(0x9a, 0x9e);
    SCCBWrite(0x9b, 0);
    SCCBWrite(0x9c, 0x40);
    SCCBWrite(0x9e, 0x11);
    SCCBWrite(0x9f, 0xa);
    SCCBWrite(0xa0, 0x2);
    SCCBWrite(0xa1, 0x50);
    SCCBWrite(0xa2, 0x40);
    SCCBWrite(0xa3, 0x6);
    SCCBWrite(0xa4, 0);
    SCCBWrite(0xa6, 0);
    SCCBWrite(0xa7, 0x40);
    SCCBWrite(0xa8, 0x40);
    SCCBWrite(0xa9, 0x80);
    SCCBWrite(0xaa, 0);
    SCCBWrite(0xab, 0x6);
    SCCBWrite(0xac, 0xff);
}
#endif