Lights

Creations

Sorunome

6 years ago

This tutorial will attempt to make it easier to understand how to use the shiny RGB LEDs on the back of your Gamebuino META.

General Idea

The general idea is, that the lights are just a tiny image - one with 2x4 pixels! That means you can use all the tricks, animations etc. as explained in the Images tutorial on the lights, too! The image is gb.lights, it is an rgb565 image.

Pixel layout

gb.lights being two pixels wide and four pixels high means it directly maps onto the real-life LEDs of the Gamebuino! So, the upper-left pixel controls the upper-left LED, the lower-right pixel the lower-right LED etc.

That means, to turn the LED in the bottom-right red you can do:

gb.lights.drawPixel(1, 3, RED);

1 because x-coordinate one, that is on the right side of the image, thus the gamebuino.
3 because y-coordinate three, that is all on the bottom of the image, thus the gamebuino.
Keep in mind that x- and y coordinates start at zero!

Colors

The gb.lights image being RGB565 you can use any of the pre-defined colors or make your own!

All white will mean that the LEDs are, well, all white. And all black means they are completely turned off. So, to get e.g. a dimmer red on the LEDs, you have to draw a darker red to the image.

Fading example

This example will fade the LEDs from all white to all black.

#include <Gamebuino-Meta.h>

void setup() {
  gb.begin();
}

void fadeToBlack() {
  Color c;
  for (uint8_t level = 255; level > 0; level--) {
    while(!gb.update());
    gb.lights.fill(gb.createColor(level, level, level));
  }
  while(!gb.update());
  gb.lights.fill(BLACK);
}

void loop() {
  fadeToBlack();
}

Intensity

There is no need for your game ever to have an intensity setting to set the base intensity. This is already done automatically via the home menu.

View full creation

clement

NEW 6 years ago

just typo error on ligts => lights


gb.ligts.drawPixel(1, 3, RED);


seems to be corrected befor my comment :) 


Sorunome

6 years ago

fixed, thanks

Sorunome

NEW 6 years ago

clement clement

fixed, thanks

ragnarok93

NEW 6 years ago

Wooow, thanks Sorunome for tutorial ;). You are the best :D. Thanks to your effort many people will understand how to code new things to their games ;).

UnPolacoLoco

NEW 6 years ago

I'd add a link to this in the Graphics Reference :)

Nicolas

NEW 6 years ago

Hi ! I want to know how can i turn off the led? I made this code to use LEDs :

if (gb.buttons.pressed(BUTTON_RIGHT)) {
    gb.lights.drawPixel(1, 1, WHITE);
  }

but i want to turn off the led when i press button B for exemple.

Thanks! 

Sorunome

6 years ago

To turn them off you simply set all to BLACK!

gb.lights.drawPixel(1, 1, BLACK);

Sorunome

NEW 6 years ago

Nicolas Nicolas

To turn them off you simply set all to BLACK!

gb.lights.drawPixel(1, 1, BLACK);

Nicolas

6 years ago

Thanks!

Nicolas

NEW 6 years ago

Sorunome Sorunome

Thanks!

iPad

NEW 6 years ago

Message deleted

tikkel

NEW 5 years ago

I would like to do a little LED animation. But only the LED on the top left lights up.

const byte led_sprite[]={
 2,4,  //width, height
 8,0,  //8 frames animation
 1,    //frameloop on
 0xFF, //no transparent color
 1,    //indexed colors
 0xb0, 0x00, 0x00, 0x00,
 0x0b, 0x00, 0x00, 0x00,
 0x00, 0x0b, 0x00, 0x00,
 0x00, 0x00, 0x0b, 0x00,
 0x00, 0x00, 0x00, 0x0b,
 0x00, 0x00, 0x00, 0xb0,
 0x00, 0x00, 0xb0, 0x00,
 0x00, 0xb0, 0x00, 0x00,
};
void loop() {
...
 gb.lights.drawImage(0, 0, led_sprite);
 ...
}



Steph

5 years ago

Yeah, it looks like it's not working... try this instead:

#include <Gamebuino-Meta.h>

const byte led_sprite_data[]={
  2,4,  // width, height
  8,0,  // 8 frames animation
  0,    // frameloop on
  0xff, // no transparent color
  1,    // indexed colors

  0xb0, 0x00, 0x00, 0x00,
  0x0b, 0x00, 0x00, 0x00,
  0x00, 0x0b, 0x00, 0x00,
  0x00, 0x00, 0x0b, 0x00,
  0x00, 0x00, 0x00, 0x0b,
  0x00, 0x00, 0x00, 0xb0,
  0x00, 0x00, 0xb0, 0x00,
  0x00, 0xb0, 0x00, 0x00

};

void setup() {
    gb.begin();
}

void loop() {
    gb.waitForUpdate();

    Image led(led_sprite_data);
    uint16_t frames = led_sprite_data[2] | (led_sprite_data[3] << 8);

    led.setFrame(gb.frameCount % frames);
    gb.lights.drawImage(0, 0, led);
}

tikkel

5 years ago

... that was missing: Image img;

const byte led_spriteBuff[]={
 2,4,  //width, height
 8,0,  //8 frames animation
 1,    //frameloop on
 0xFF, //no transparent color
 1,    //indexed colors
 0xb0, 0x00, 0x00, 0x00,
 0x0b, 0x00, 0x00, 0x00,
 0x00, 0x0b, 0x00, 0x00,
 0x00, 0x00, 0x0b, 0x00,
 0x00, 0x00, 0x00, 0x0b,
 0x00, 0x00, 0x00, 0xb0,
 0x00, 0x00, 0xb0, 0x00,
 0x00, 0xb0, 0x00, 0x00,
};
Image led_sprite (led_spriteBuff);

void loop() {
...
 gb.lights.drawImage(0, 0, led_sprite);
 ...
}

Steph

NEW 5 years ago

tikkel tikkel

Yeah, it looks like it's not working... try this instead:

#include <Gamebuino-Meta.h>

const byte led_sprite_data[]={
  2,4,  // width, height
  8,0,  // 8 frames animation
  0,    // frameloop on
  0xff, // no transparent color
  1,    // indexed colors

  0xb0, 0x00, 0x00, 0x00,
  0x0b, 0x00, 0x00, 0x00,
  0x00, 0x0b, 0x00, 0x00,
  0x00, 0x00, 0x0b, 0x00,
  0x00, 0x00, 0x00, 0x0b,
  0x00, 0x00, 0x00, 0xb0,
  0x00, 0x00, 0xb0, 0x00,
  0x00, 0xb0, 0x00, 0x00

};

void setup() {
    gb.begin();
}

void loop() {
    gb.waitForUpdate();

    Image led(led_sprite_data);
    uint16_t frames = led_sprite_data[2] | (led_sprite_data[3] << 8);

    led.setFrame(gb.frameCount % frames);
    gb.lights.drawImage(0, 0, led);
}

tikkel

NEW 5 years ago

tikkel tikkel

... that was missing: Image img;

const byte led_spriteBuff[]={
 2,4,  //width, height
 8,0,  //8 frames animation
 1,    //frameloop on
 0xFF, //no transparent color
 1,    //indexed colors
 0xb0, 0x00, 0x00, 0x00,
 0x0b, 0x00, 0x00, 0x00,
 0x00, 0x0b, 0x00, 0x00,
 0x00, 0x00, 0x0b, 0x00,
 0x00, 0x00, 0x00, 0x0b,
 0x00, 0x00, 0x00, 0xb0,
 0x00, 0x00, 0xb0, 0x00,
 0x00, 0xb0, 0x00, 0x00,
};
Image led_sprite (led_spriteBuff);

void loop() {
...
 gb.lights.drawImage(0, 0, led_sprite);
 ...
}