This is a driver to control the SD20 20 Channel I2C to Servo Driver Chip from www.robot-electronics.co.uk (http://www.robot-electronics.co.uk/htm/sd20tech.htm). This driver will allow a servo channel to be started up, stopped and the duty cycle altered in standard mode. No attempt to include the extended mode is included.
Diff: SD20.cpp
- Revision:
- 1:6557cf755742
- Parent:
- 0:870a29ad1074
- Child:
- 2:b6789ad2af2b
- Child:
- 4:5ccaaacec14f
diff -r 870a29ad1074 -r 6557cf755742 SD20.cpp --- a/SD20.cpp Sun Jan 24 14:39:33 2016 +0000 +++ b/SD20.cpp Sun Jan 24 15:10:48 2016 +0000 @@ -1,57 +1,74 @@ #include "mbed.h" #include "SD20.h" -SD20::SD20(I2C *bus,int I2CAddress, int BusHz) +SD20::SD20(I2C *bus) { _bus = bus; - deviceAddress = I2CAddress; - BusHz = BusHz; - _bus -> frequency(BusHz); + _bus -> frequency(BUS_FREQ); } -int SD20::UpdatePWM(char *DataOutput) +int SD20::UpdateChannel(char *newDutyCycle) { - if(_bus -> write(deviceAddress,(char*)DataOutput,OUT_BUF_LEN)) + // Output new duty cycle to SD20, return 0 if successful, 1 if unsuccessful + if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) { printf("I2C Failed to write\r\n"); return 1; } else { - printf("0x%02x sent to Reg: 0x%02x\r\n",*(DataOutput+1),*DataOutput); + printf("0x%02x sent to Reg: 0x%02x\r\n",*(newDutyCycle+1),*newDutyCycle); return 0; } } -int SD20::StartStopPWM(int Channel,bool Start) +int SD20::StartStopChannel(uint8_t ServoChannel,bool Start) { + // Startup a new channel, return 0 if successful, 1 if unsuccessful if(Start == CHANNEL_START) { - int DataOutput[] = {Channel,0x01}; - if(_bus -> write(deviceAddress,(char*)DataOutput,OUT_BUF_LEN)) + char newDutyCycle[] = {ServoChannel,CHANNEL_START}; + if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) { printf("I2C Failed to write\r\n"); return 1; } else { - printf("0x%02x sent to Reg: 0x%02x\r\n",*(DataOutput+1),*DataOutput); + printf("0x%02x sent to Reg: 0x%02x\r\n",*(newDutyCycle+1),*newDutyCycle); return 0; } } + + // Stop channel, return 0 if successful, 1 if unsuccessful else { - int DataOutput[] = {Channel,0x00}; - if(_bus -> write(deviceAddress,(char*)DataOutput,OUT_BUF_LEN)) + char newDutyCycle[] = {ServoChannel,CHANNEL_STOP}; + if(_bus -> write(SD20_ADDRESS,newDutyCycle,OUT_BUF_LEN)) { printf("I2C Failed to write\r\n"); return 1; } else { - printf("0x%02x sent to Reg: 0x%02x\r\n",*(DataOutput+1),*DataOutput); + printf("0x%02x sent to Reg: 0x%02x\r\n",*(newDutyCycle+1),*newDutyCycle); return 0; } } } +int SD20::SetStandardMode() +{ + char outputBuf[] = {STD_ETD_MODE_CTRL,STD_MODE}; + if(_bus -> write(SD20_ADDRESS,outputBuf,OUT_BUF_LEN)) + { + printf("I2C Failed to write\r\n"); + return 1; + } + else + { + printf("0x%02x sent to Reg: 0x%02x\r\n",*(outputBuf+1),*outputBuf); + return 0; + } +} +