I2c led 47/193947/1
authorMichal Skorupinski <m.skorupinsk@samsung.com>
Mon, 5 Nov 2018 18:25:39 +0000 (19:25 +0100)
committerMichal Skorupinski <m.skorupinsk@samsung.com>
Tue, 6 Nov 2018 12:12:18 +0000 (13:12 +0100)
Change-Id: I21f4ddebabfb76d9ea5aa0e36b7b12d1918958e5
Signed-off-by: Michal Skorupinski <m.skorupinsk@samsung.com>
inc/resource/resource_PCA9685.h
src/resource/resource_PCA9685.c
src/resource/resource_led.c
src/resource/resource_motor_driver_L298N.c
src/resource/resource_servo_motor.c

index aa45477..e57d38a 100644 (file)
@@ -21,7 +21,7 @@
 
 #define PCA9685_CH_MAX 15
 
-int resource_pca9685_init(unsigned int ch);
+int resource_pca9685_init(unsigned int ch, unsigned int freq);
 int resource_pca9685_fini(unsigned int ch);
 int resource_pca9685_set_frequency(unsigned int freq_hz);
 int resource_pca9685_set_value_to_channel(unsigned int channel, int on, int off);
index 20a5545..e7edffc 100644 (file)
@@ -102,20 +102,16 @@ int resource_pca9685_set_value_to_channel(unsigned int channel, int on, int off)
        retvm_if(ch_state[channel] == PCA9685_CH_STATE_NONE, -1,
                "ch[%u] is not in used state", channel);
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_ON_L + 4*channel, on & 0xFF);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, LED0_ON_L + 4*channel, on & 0xFF);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_ON_H + 4*channel, on >> 8);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, LED0_ON_H + 4*channel, on >> 8);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_OFF_L + 4*channel, off & 0xFF);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, LED0_OFF_L + 4*channel, off & 0xFF);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-                       LED0_OFF_H + 4*channel, off >> 8);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, LED0_OFF_H + 4*channel, off >> 8);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
        return 0;
@@ -126,26 +122,22 @@ static int resource_pca9685_set_value_to_all(int on, int off)
        int ret = PERIPHERAL_ERROR_NONE;
        retvm_if(g_i2c_h == NULL, -1, "Not initialized yet");
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_ON_L, on & 0xFF);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, ALL_LED_ON_L, on & 0xFF);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_ON_H, on >> 8);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, ALL_LED_ON_H, on >> 8);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_OFF_L, off & 0xFF);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, ALL_LED_OFF_L, off & 0xFF);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
-       ret = peripheral_i2c_write_register_byte(g_i2c_h,
-               ALL_LED_OFF_H, off >> 8);
+       ret = peripheral_i2c_write_register_byte(g_i2c_h, ALL_LED_OFF_H, off >> 8);
        retvm_if(ret != PERIPHERAL_ERROR_NONE, -1, "failed to write register");
 
        return 0;
 }
 
-int resource_pca9685_init(unsigned int ch)
+int resource_pca9685_init(unsigned int ch, unsigned int freq)
 {
        uint8_t mode1 = 0;
        int ret = PERIPHERAL_ERROR_NONE;
@@ -165,8 +157,7 @@ int resource_pca9685_init(unsigned int ch)
 
        ret = peripheral_i2c_open(RPI3_I2C_BUS, PCA9685_ADDRESS, &g_i2c_h);
        if (ret != PERIPHERAL_ERROR_NONE) {
-               _E("failed to open pca9685-[bus:%d, addr:%d]",
-                       RPI3_I2C_BUS, PCA9685_ADDRESS);
+               _E("failed to open pca9685-[bus:%d, addr:%d]", RPI3_I2C_BUS, PCA9685_ADDRESS);
                return -1;
        }
        ret = resource_pca9685_set_value_to_all(0, 0);
@@ -204,7 +195,7 @@ int resource_pca9685_init(unsigned int ch)
 
        usleep(500); // wait for oscillator
 
-       ret = resource_pca9685_set_frequency(60);
+       ret = resource_pca9685_set_frequency(freq);
        if (ret) {
                _E("failed to set frequency");
                goto ERROR;
index e54b430..ac63dfd 100644 (file)
@@ -22,6 +22,7 @@
 #include <time.h>
 #include "log.h"
 #include "resource/resource_led.h"
+#include "resource/resource_PCA9685.h"
 #include "config.h"
 
 #define DEFAULT_BI_LED_RED 17
@@ -29,7 +30,6 @@
 #define CONFIG_KEY_RPI_BI_LED_RED "bi.red"
 #define CONFIG_KEY_RPI_BI_LED_GREEN "bi.green"
 #define CONFIG_KEY_RPI_USE_BI_LED "bi.use"
-
 #define DEFAULT_RGB_LED_RED 12
 #define DEFAULT_RGB_LED_GREEN 13
 #define DEFAULT_RGB_LED_BLUE 24
 #define CONFIG_KEY_RPI_RGB_LED_BLUE "rgb.blue"
 #define CONFIG_KEY_RPI_USE_RGB_LED "rgb.use"
 
+#define DEFAULT_RGB_PCA_R 4
+#define DEFAULT_RGB_PCA_G 5
+#define DEFAULT_RGB_PCA_B 10
+
 #define CONFIG_GRP_RPI "Rpi.led"
 
 
@@ -125,51 +129,51 @@ static peripheral_gpio_h _init_gpio(int default_gpio, char *key)
 
 }
 
-static peripheral_pwm_h _init_pwm(int default_pwm_pin, char *key)
-{
-       peripheral_pwm_h pwm;
-       int pin;
-
-       bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_pwm_pin, &pin);
-       _D("PWN: %d, DUTY: %u, period: %u", pin, (unsigned)2e6, (unsigned)2e7);
-
-       if (modified) {
-               config_save();
-       }
-
-       int ret = peripheral_pwm_open(1, pin, &pwm);
-       retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-
-       ret = peripheral_pwm_set_period(pwm, (unsigned)2e7);
-       retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-
-       ret = peripheral_pwm_set_duty_cycle(pwm, (unsigned)2e6);
-       retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-
-       ret = peripheral_pwm_set_enabled(pwm, true);
-       retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-
-
-
-       return pwm;
-}
-
-static void _init_rgb_led(void)
-{
-       bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_RGB_LED, 1, &s_info.use_rgb_led);
-       if (modified) {
-               config_save();
-       }
-
-       if (s_info.use_rgb_led) {
-               s_info.pwn_rgb_led[LED_COLOR_RED] = _init_pwm(DEFAULT_RGB_LED_RED, CONFIG_KEY_RPI_RGB_LED_RED);
-               s_info.pwn_rgb_led[LED_COLOR_GREEN] = _init_pwm(DEFAULT_RGB_LED_GREEN, CONFIG_KEY_RPI_RGB_LED_GREEN);
-               s_info.pwn_rgb_led[LED_COLOR_BLUE] = _init_pwm(DEFAULT_RGB_LED_BLUE, CONFIG_KEY_RPI_RGB_LED_BLUE);
-
-
-               peripheral_pwm_set_duty_cycle(s_info.pwn_rgb_led[LED_COLOR_RED], (unsigned)1e7);
-       }
-}
+//static peripheral_pwm_h _init_pwm(int default_pwm_pin, char *key)
+//{
+//     peripheral_pwm_h pwm;
+//     int pin;
+//
+//     bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_pwm_pin, &pin);
+//     _D("PWN: %d, DUTY: %u, period: %u", pin, (unsigned)2e6, (unsigned)2e7);
+//
+//     if (modified) {
+//             config_save();
+//     }
+//
+//     int ret = peripheral_pwm_open(1, pin, &pwm);
+//     retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
+//
+//     ret = peripheral_pwm_set_period(pwm, (unsigned)2e7);
+//     retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
+//
+//     ret = peripheral_pwm_set_duty_cycle(pwm, (unsigned)2e6);
+//     retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
+//
+//     ret = peripheral_pwm_set_enabled(pwm, true);
+//     retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
+//
+//
+//
+//     return pwm;
+//}
+
+//static void _init_rgb_led(void)
+//{
+//     bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_RGB_LED, 1, &s_info.use_rgb_led);
+//     if (modified) {
+//             config_save();
+//     }
+//
+//     if (s_info.use_rgb_led) {
+//             s_info.pwn_rgb_led[LED_COLOR_RED] = _init_pwm(DEFAULT_RGB_LED_RED, CONFIG_KEY_RPI_RGB_LED_RED);
+//             s_info.pwn_rgb_led[LED_COLOR_GREEN] = _init_pwm(DEFAULT_RGB_LED_GREEN, CONFIG_KEY_RPI_RGB_LED_GREEN);
+//             s_info.pwn_rgb_led[LED_COLOR_BLUE] = _init_pwm(DEFAULT_RGB_LED_BLUE, CONFIG_KEY_RPI_RGB_LED_BLUE);
+//
+//
+//             peripheral_pwm_set_duty_cycle(s_info.pwn_rgb_led[LED_COLOR_RED], (unsigned)1e7);
+//     }
+//}
 
 static void _init_bi_led(void)
 {
@@ -186,17 +190,51 @@ static void _init_bi_led(void)
        }
 }
 
+static void _init_pca(void)
+{
+       int ret;
+//     int ret = resource_pca9685_init(DEFAULT_RGB_PCA_R, 1000);
+//     ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+////
+//     ret = resource_pca9685_init(DEFAULT_RGB_PCA_G, 1000);
+//     ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+//
+//     ret = resource_pca9685_init(DEFAULT_RGB_PCA_B, 1000);
+//     ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+//
+       ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_R, 0, 0);
+       ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+       ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_G, 0, 255);
+       ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+       ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_B, 0, 0);
+       ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+//     peripheral_i2c_h g_i2c_h;
+//     ret = peripheral_i2c_open(1, 0x40, &g_i2c_h);
+//     if (ret != PERIPHERAL_ERROR_NONE) {
+//             _E("failed to open pca9685-[bus:%d, addr:%d]", RPI3_I2C_BUS, PCA9685_ADDRESS);
+//             return;
+//     }
+}
+
 void resource_led_init(void)
 {
        _D("Initialize Led");
        _init_bi_led();
-       _init_rgb_led();
+//     _init_rgb_led();
+       _init_pca();
 }
 
 void resource_led_destroy(void)
 {
        peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
        peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);
+
+       resource_pca9685_fini(DEFAULT_RGB_PCA_R);
+       resource_pca9685_fini(DEFAULT_RGB_PCA_G);
+       resource_pca9685_fini(DEFAULT_RGB_PCA_B);
 }
 
 void resource_led_set(led_color_e color)
@@ -208,6 +246,16 @@ void resource_led_set(led_color_e color)
 
        s_info.current_color = color;
        _led_set(color);
+
+
+       int ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_R, 0, 0);
+       ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+       ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_G, 0, 255);
+       ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+       ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_B, 0, 0);
+       ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
 }
 
 void resource_led_blink(led_color_e color, unsigned timeout)
index 291b74b..972a9ac 100644 (file)
@@ -169,7 +169,7 @@ static int __init_motor_by_id(motor_id_e id)
        if (g_md_h[id].motor_state == MOTOR_STATE_NONE)
                __set_default_configuration_by_id(id);
 
-       ret = resource_pca9685_init(g_md_h[id].en_ch);
+       ret = resource_pca9685_init(g_md_h[id].en_ch, 60);
        if (ret) {
                _E("failed to init PCA9685");
                return -1;
index e16df2e..891a829 100644 (file)
@@ -26,7 +26,7 @@ static int servo_motor_index[SERVO_MOTOR_MAX + 1] = {0, };
 static int resource_servo_motor_init(unsigned int ch)
 {
        int ret = 0;
-       ret = resource_pca9685_init(ch);
+       ret = resource_pca9685_init(ch, 60);
        if (ret) {
                _E("failed to init PCA9685 with ch[%u]", ch);
                return -1;