Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
BurningCa007
Posts: 5
Registered: ‎05-26-2013
My Device: Q10
Accepted Solution

Require some assistance creating an LED Color Changer Application

Hello,

I guess I can consider this day 1 of programming with C++ and for Blackberry 10 devices but alas a lot of the syntax seems vaguely similar to Java which I have some experience with.

 

I'm trying to create an application that listens whenever another application on the device requests that the front-facing LED is turned on as such and allow the user to set the static color of the LED when it is turned on by a myriad of apps.

 

So far, I've read the API regarding the functions that <bps/led.h> includes and have successfully utilised the led_request_color(); function to change the color of the LED when an action such as a swipe down is incurred on the touchscreen...

 

Here's the code:

/*
* Copyright (c) 2011-2012 Research In Motion Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <bps/bps.h>
#include <bps/event.h>
#include <bps/navigator.h>
#include <bps/screen.h>
#include <fcntl.h>
#include <screen/screen.h>
#include <bps/led.h>

static bool shutdown;

static void
handle_screen_event(bps_event_t *event)
{
    int screen_val;

    screen_event_t screen_event = screen_event_get_event(event);
    screen_get_event_property_iv(screen_event, SCREEN_PROPERTY_TYPE, &screen_val);

    switch (screen_val) {
    case SCREEN_EVENT_MTOUCH_TOUCH:
        fprintf(stderr,"Touch event");
        led_request_color("1", LED_COLOR_BLUE, 1);
        break;
    case SCREEN_EVENT_MTOUCH_MOVE:
        fprintf(stderr,"Move event");

        break;
    case SCREEN_EVENT_MTOUCH_RELEASE:
        fprintf(stderr,"Release event");
        led_request_color("1", LED_COLOR_RED, 1);
        break;
    default:
        break;
    }
    fprintf(stderr,"\n");
}

static void
handle_navigator_event(bps_event_t *event) {
    switch (bps_event_get_code(event)) {
    case NAVIGATOR_SWIPE_DOWN:
        fprintf(stderr,"Swipe down event");
        break;
    case NAVIGATOR_EXIT:
        fprintf(stderr,"Exit event");
        shutdown = true;
        break;
    default:
        break;
    }
    fprintf(stderr,"\n");
}

static void
handle_event()
{
    int domain;

    bps_event_t *event = NULL;
    if (BPS_SUCCESS != bps_get_event(&event, -1)) {
        fprintf(stderr, "bps_get_event() failed\n");
        return;
    }
    if (event) {
        domain = bps_event_get_domain(event);
        if (domain == navigator_get_domain()) {
            handle_navigator_event(event);
        } else if (domain == screen_get_domain()) {
            handle_screen_event(event);
        }
    }
}

int
main(int argc, char **argv)
{
    const int usage = SCREEN_USAGE_NATIVE;

    screen_context_t screen_ctx;
    screen_window_t screen_win;
    screen_buffer_t screen_buf = NULL;
    int rect[4] = { 0, 0, 0, 0 };

    /* Setup the window */
    screen_create_context(&screen_ctx, 0);
    screen_create_window(&screen_win, screen_ctx);
    screen_set_window_property_iv(screen_win, SCREEN_PROPERTY_USAGE, &usage);
    screen_create_window_buffers(screen_win, 1);

    screen_get_window_property_pv(screen_win, SCREEN_PROPERTY_RENDER_BUFFERS, (void **)&screen_buf);
    screen_get_window_property_iv(screen_win, SCREEN_PROPERTY_BUFFER_SIZE, rect+2);

    /* Fill the screen buffer with blue */
    int attribs[] = { SCREEN_BLIT_COLOR, 0xff0000ff, SCREEN_BLIT_END };
    screen_fill(screen_ctx, screen_buf, attribs);
    screen_post_window(screen_win, screen_buf, 1, rect, 0);

    char set = 222;

    /* Signal bps library that navigator and screen events will be requested */
    bps_initialize();
    screen_request_events(screen_ctx);
    navigator_request_events(0);

    /* Get Events - This doesn't work */
    if(bps_get_event() == led_get_domain()){
    	led_request_color("1", LED_COLOR_BLUE, 0);
    }

    while (!shutdown) {
        /* Handle user input */
        handle_event();
    }

    /* Clean up */
    screen_stop_events(screen_ctx);
    bps_shutdown();
    screen_destroy_window(screen_win);
    screen_destroy_context(screen_ctx);
    return 0;
}

 I don't know if I'm doing this right but I'm trying to get the led_get_domain() function to get the ids of LED events triggered by other apps? Hence I think this is what I've tried to do but to no avail in the above code:

 /* Get Events - This doesn't work */
    if(bps_get_event() == led_get_domain()){
    	led_request_color("1", LED_COLOR_BLUE, 0);
    }

 

So, I guess I'm a newbie at this and I'd greatly appreciate your help and any possible improvements. Thanks! :smileyvery-happy:

Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: Require some assistance creating an LED Color Changer Application

Accroding to https://developer.blackberry.com/native/reference/bb10/com.qnx.doc.bps.lib_ref/com.qnx.doc.bps.lib_r... you have only one event for LED which is LED_INFO it just tells you current status of LED.

To start receiving this event you need first to call led_request_events().

New Developer
BurningCa007
Posts: 5
Registered: ‎05-26-2013
My Device: Q10

Re: Require some assistance creating an LED Color Changer Application

So where would you propose that I add this new code? As to prevent any process terminations, etc.
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: Require some assistance creating an LED Color Changer Application

After 

bps_initialize();
New Developer
BurningCa007
Posts: 5
Registered: ‎05-26-2013
My Device: Q10

Re: Require some assistance creating an LED Color Changer Application

Thanks for the help!