Practical Robotics Modular Robot Library
Diff: led.cpp
- Revision:
- 0:8a2dd255c508
- Child:
- 1:a6728adaf7e7
diff -r 000000000000 -r 8a2dd255c508 led.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/led.cpp Sat Nov 26 17:28:53 2016 +0000 @@ -0,0 +1,157 @@ +#include "robot.h" + +// The LED driver is quite powerful in terms of how it can be used (LEDs can be grouped, have individual and group PWM control, can be blinked etc). +// +// To keep things relatively simple, in this version of the code, we will not use the groups or blinking functions, and will have all LEDs enabled. +// +// Their state is controlled solely by each LEDs PWM control - which is a 1 byte value between 0 (fully off) and 255 (fully on) + +char red_led_states [8]; +char green_led_states [8]; + + + +void Led::set_green_led (char led, char brightness) +{ + if(led < 8) { + green_led_states [led] = brightness; + char data[2]; + data[0] = 3 + led + led; + data[1] = brightness; + if(i2c_lock==0){ + i2c_lock = 1; + primary_i2c.write(LED_ADDRESS,data,2); + }i2c_lock = 0; + } +} + +void Led::set_red_led (char led, char brightness) +{ + + if(led < 8) { + red_led_states [led] = brightness; + char data[2]; + data[0] = 2 + led + led; + data[1] = brightness; + if(i2c_lock==0){ + i2c_lock = 1; + primary_i2c.write(LED_ADDRESS,data,2); + }i2c_lock = 0; + } +} + +int Led::reset_led_driver() +{ + // The TLC59116 has a software reset option over i2c: + // The bytes A5h, 5Ah are sent to special address D6h + char data[2]; + data[0] = 0xA5; + data[1] = 0x5A; + return primary_i2c.write(0xD6,data,2); +} + +int Led::init_led_driver() +{ + // This code enables the LED outputs so all LEDs are on, and are set to work on individual PWM control + // + // First we write 0x80 to the Mode 1 register (0x00) to enable the oscillator and register auto-increment + char data[2]; + data[0]=0x00; + data[1]=0x80; + int okay = primary_i2c.write(LED_ADDRESS,data,2,false); + if(okay == 0) { + // Now we turn all the LEDs the enable on PWM control + data[0]=0x14; + data[1]=0xAA; + primary_i2c.write(LED_ADDRESS,data,2,false); + data[0]=0x15; + data[1]=0xAA; + primary_i2c.write(LED_ADDRESS,data,2,false); + data[0]=0x16; + data[1]=0xAA; + primary_i2c.write(LED_ADDRESS,data,2,false); + data[0]=0x17; + data[1]=0xAA; + primary_i2c.write(LED_ADDRESS,data,2,false); + } + return okay; +} + +int led_ticker_sub_state = 0; +char led_ticker_state = 0; +char increment = 1; +Ticker led_ticker; + +void Led::start_test() +{ + led_ticker.attach(this,&Led::test_ticker_routine,0.02); +} + +void Led::test_ticker_routine() +{ + led_ticker_sub_state += increment; + increment += 1; + if(led_ticker_sub_state > 250) { + led_ticker_sub_state = 0; + led_ticker_state ++; + increment = 1; + if (led_ticker_state == 24) led_ticker_state = 0; + } + char working_led = led_ticker_state % 8; + + if(led_ticker_state < 8 || led_ticker_state > 15) set_green_led(working_led, led_ticker_sub_state ); + else set_green_led(working_led, 0); + if(led_ticker_state > 7) set_red_led(working_led, led_ticker_sub_state ); + else set_red_led(working_led, 0); + if(led_ticker_sub_state == 0) { + + } +} + +/* + +//Setup the LED driver [TLC59116]; address is 1100 000 (0xC0) {defined by LED_ADDRESS} + //Enable the output oscillator in mode 1 register (00h) + + char ret_data[32]; + for(int i=0;i<31;i++){ + data[0]=i; + primary_i2c.write(LED_ADDRESS,data,1,false); + primary_i2c.read(LED_ADDRESS,ret_data+i,1); + } + for(int i=0;i<31;i++){ + pc.printf("Register %X : %X\n",i,ret_data[i]); + } + + wait(2); + data[0]=0x00; + data[1]=0x00; + primary_i2c.write(LED_ADDRESS,data,2,false); + + wait(0.05); + data[0]=0x14; + data[1]=0x55; + primary_i2c.write(LED_ADDRESS,data,2,false); + wait(0.05); + data[0]=0x15; + data[1]=0x66; + primary_i2c.write(LED_ADDRESS,data,2,false); + wait(0.05); + data[0]=0x16; + data[1]=0x66; + primary_i2c.write(LED_ADDRESS,data,2,false); + wait(0.05); + data[0]=0x17; + data[1]=0x55; + primary_i2c.write(LED_ADDRESS,data,2,false); + + for(int i=0;i<31;i++){ + data[0]=i; + primary_i2c.write(LED_ADDRESS,data,1,false); + primary_i2c.read(LED_ADDRESS,ret_data+i,1); + } + for(int i=0;i<31;i++){ + pc.printf("Register %X : %X\n",i,ret_data[i]); + } + +*/ \ No newline at end of file