gfdgd
Fork of PCA995xA by
base_class/PCA995xA.cpp@7:56a45c690801, 2018-08-14 (annotated)
- Committer:
- tb942
- Date:
- Tue Aug 14 18:26:35 2018 +0000
- Revision:
- 7:56a45c690801
- Parent:
- 0:a624e2eeccac
removed #include mbed.h - it's not needed;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nxp_ip | 0:a624e2eeccac | 1 | #include "mbed.h" |
nxp_ip | 0:a624e2eeccac | 2 | #include "PCA995xA.h" |
nxp_ip | 0:a624e2eeccac | 3 | |
nxp_ip | 0:a624e2eeccac | 4 | PCA995xA::PCA995xA( PinName i2c_sda, PinName i2c_scl, char i2c_address ) |
nxp_ip | 0:a624e2eeccac | 5 | : i2c_p( new I2C( i2c_sda, i2c_scl ) ), i2c( *i2c_p ), address( i2c_address ) |
nxp_ip | 0:a624e2eeccac | 6 | { |
nxp_ip | 0:a624e2eeccac | 7 | } |
nxp_ip | 0:a624e2eeccac | 8 | |
nxp_ip | 0:a624e2eeccac | 9 | PCA995xA::PCA995xA( I2C &i2c_, char i2c_address ) |
nxp_ip | 0:a624e2eeccac | 10 | : i2c_p( NULL ), i2c( i2c_ ), address( i2c_address ) |
nxp_ip | 0:a624e2eeccac | 11 | { |
nxp_ip | 0:a624e2eeccac | 12 | } |
nxp_ip | 0:a624e2eeccac | 13 | |
nxp_ip | 0:a624e2eeccac | 14 | PCA995xA::~PCA995xA() |
nxp_ip | 0:a624e2eeccac | 15 | { |
nxp_ip | 0:a624e2eeccac | 16 | if ( NULL != i2c_p ) |
nxp_ip | 0:a624e2eeccac | 17 | delete i2c_p; |
nxp_ip | 0:a624e2eeccac | 18 | } |
nxp_ip | 0:a624e2eeccac | 19 | |
nxp_ip | 0:a624e2eeccac | 20 | void PCA995xA::reset( void ) |
nxp_ip | 0:a624e2eeccac | 21 | { |
nxp_ip | 0:a624e2eeccac | 22 | char v = 0x06; |
nxp_ip | 0:a624e2eeccac | 23 | i2c.write( 0x00, &v, 1 ); |
nxp_ip | 0:a624e2eeccac | 24 | } |
nxp_ip | 0:a624e2eeccac | 25 | |
nxp_ip | 0:a624e2eeccac | 26 | void PCA995xA::pwm( int port, float v ) |
nxp_ip | 0:a624e2eeccac | 27 | { |
nxp_ip | 0:a624e2eeccac | 28 | char reg_addr; |
nxp_ip | 0:a624e2eeccac | 29 | |
nxp_ip | 0:a624e2eeccac | 30 | reg_addr = pwm_register_access( port ); |
nxp_ip | 0:a624e2eeccac | 31 | write( reg_addr, (char)(v * 255.0) ); |
nxp_ip | 0:a624e2eeccac | 32 | } |
nxp_ip | 0:a624e2eeccac | 33 | |
nxp_ip | 0:a624e2eeccac | 34 | void PCA995xA::pwm( float *vp ) |
nxp_ip | 0:a624e2eeccac | 35 | { |
nxp_ip | 0:a624e2eeccac | 36 | int n_of_ports = number_of_ports(); |
nxp_ip | 0:a624e2eeccac | 37 | char data[ n_of_ports + 1 ]; |
nxp_ip | 0:a624e2eeccac | 38 | |
nxp_ip | 0:a624e2eeccac | 39 | *data = pwm_register_access( 0 ); |
nxp_ip | 0:a624e2eeccac | 40 | |
nxp_ip | 0:a624e2eeccac | 41 | for ( int i = 1; i <= n_of_ports; i++ ) |
nxp_ip | 0:a624e2eeccac | 42 | data[ i ] = (char)(*vp++ * 255.0); |
nxp_ip | 0:a624e2eeccac | 43 | |
nxp_ip | 0:a624e2eeccac | 44 | write( data, sizeof( data ) ); |
nxp_ip | 0:a624e2eeccac | 45 | } |
nxp_ip | 0:a624e2eeccac | 46 | |
nxp_ip | 0:a624e2eeccac | 47 | void PCA995xA::current( int port, float v ) |
nxp_ip | 0:a624e2eeccac | 48 | { |
nxp_ip | 0:a624e2eeccac | 49 | char reg_addr; |
nxp_ip | 0:a624e2eeccac | 50 | |
nxp_ip | 0:a624e2eeccac | 51 | reg_addr = current_register_access( port ); |
nxp_ip | 0:a624e2eeccac | 52 | write( reg_addr, (char)(v * 255.0) ); |
nxp_ip | 0:a624e2eeccac | 53 | } |
nxp_ip | 0:a624e2eeccac | 54 | |
nxp_ip | 0:a624e2eeccac | 55 | void PCA995xA::current( float *vp ) |
nxp_ip | 0:a624e2eeccac | 56 | { |
nxp_ip | 0:a624e2eeccac | 57 | int n_of_ports = number_of_ports(); |
nxp_ip | 0:a624e2eeccac | 58 | char data[ n_of_ports + 1 ]; |
nxp_ip | 0:a624e2eeccac | 59 | |
nxp_ip | 0:a624e2eeccac | 60 | *data = pwm_register_access( 0 ); |
nxp_ip | 0:a624e2eeccac | 61 | |
nxp_ip | 0:a624e2eeccac | 62 | for ( int i = 1; i <= n_of_ports; i++ ) |
nxp_ip | 0:a624e2eeccac | 63 | data[ i ] = (char)(*vp++ * 255.0); |
nxp_ip | 0:a624e2eeccac | 64 | |
nxp_ip | 0:a624e2eeccac | 65 | write( data, sizeof( data ) ); |
nxp_ip | 0:a624e2eeccac | 66 | } |
nxp_ip | 0:a624e2eeccac | 67 | |
nxp_ip | 0:a624e2eeccac | 68 | void PCA995xA::write( char *data, int length ) |
nxp_ip | 0:a624e2eeccac | 69 | { |
nxp_ip | 0:a624e2eeccac | 70 | *data |= AUTO_INCREMENT; |
nxp_ip | 0:a624e2eeccac | 71 | i2c.write( address, data, length ); |
nxp_ip | 0:a624e2eeccac | 72 | } |
nxp_ip | 0:a624e2eeccac | 73 | |
nxp_ip | 0:a624e2eeccac | 74 | void PCA995xA::write( char reg_addr, char data ) |
nxp_ip | 0:a624e2eeccac | 75 | { |
nxp_ip | 0:a624e2eeccac | 76 | char c[2]; |
nxp_ip | 0:a624e2eeccac | 77 | |
nxp_ip | 0:a624e2eeccac | 78 | c[0] = reg_addr; |
nxp_ip | 0:a624e2eeccac | 79 | c[1] = data; |
nxp_ip | 0:a624e2eeccac | 80 | |
nxp_ip | 0:a624e2eeccac | 81 | i2c.write( address, c, 2 ); |
nxp_ip | 0:a624e2eeccac | 82 | } |
nxp_ip | 0:a624e2eeccac | 83 | |
nxp_ip | 0:a624e2eeccac | 84 | void PCA995xA::read( char reg_addr, char *data, int length ) |
nxp_ip | 0:a624e2eeccac | 85 | { |
nxp_ip | 0:a624e2eeccac | 86 | reg_addr |= 0x80; |
nxp_ip | 0:a624e2eeccac | 87 | i2c.write( address, (char *)(®_addr), 1, true ); |
nxp_ip | 0:a624e2eeccac | 88 | i2c.read( address, data, length ); |
nxp_ip | 0:a624e2eeccac | 89 | } |
nxp_ip | 0:a624e2eeccac | 90 | |
nxp_ip | 0:a624e2eeccac | 91 | char PCA995xA::read( char reg_addr ) |
nxp_ip | 0:a624e2eeccac | 92 | { |
nxp_ip | 0:a624e2eeccac | 93 | i2c.write( address, (char *)(®_addr), 1, true ); |
nxp_ip | 0:a624e2eeccac | 94 | i2c.read( address, (char *)(®_addr), 1 ); |
nxp_ip | 0:a624e2eeccac | 95 | |
nxp_ip | 0:a624e2eeccac | 96 | return ( reg_addr ); |
nxp_ip | 0:a624e2eeccac | 97 | } |