Skip to content

Commit 7ff53e7

Browse files
committed
Created an animation example to showcase the usefullness of the readInSteps() function featuring the SSD1306 oLED display
1 parent 48185b3 commit 7ff53e7

File tree

4 files changed

+223
-0
lines changed

4 files changed

+223
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "Display.h"
2+
3+
/* Init utils */
4+
5+
MySSD1306 :: MySSD1306(TwoWire * wire){
6+
display = create_display(wire);
7+
}
8+
9+
Adafruit_SSD1306 MySSD1306 :: create_display(TwoWire * wire){
10+
Adafruit_SSD1306 display(I2C_SCREEN_WIDTH, I2C_SCREEN_HEIGHT, wire, -1);
11+
return display;
12+
}
13+
14+
/*
15+
* (X0, Y0) +---------------------+
16+
* | AREA TO BE |
17+
* | CLEARED |
18+
* +---------------------+ (X1, Y1)
19+
*/
20+
void MySSD1306 :: clear_partial(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2){
21+
display.fillRect(x1, y1, x2, y2, SSD1306_BLACK);
22+
display.display();
23+
}
24+
25+
/* Init display */
26+
void MySSD1306 :: initial_display(){
27+
display.begin(SSD1306_SWITCHCAPVCC, I2C_SSD1306_ADDR);
28+
display.clearDisplay();
29+
}
30+
31+
/* Animation during the button pressing */
32+
void MySSD1306 :: update_button(uint8_t step, uint8_t len, PAIR_t * coordinates){
33+
34+
if(step == len - 1){
35+
for(uint8_t i = 0; i < len-1; i++){
36+
display.drawBitmap(coordinates[i].x, coordinates[i].y,
37+
button_bmp,
38+
BUTTON_BMP_WIDTH, BUTTON_BMP_HEIGHT,
39+
SSD1306_BLACK);
40+
}
41+
}
42+
else{
43+
display.drawBitmap(coordinates[step].x, coordinates[step].y,
44+
button_bmp,
45+
BUTTON_BMP_WIDTH, BUTTON_BMP_HEIGHT,
46+
SSD1306_WHITE);
47+
}
48+
49+
display.display();
50+
}
51+
52+
/* Text displaying function */
53+
void MySSD1306 :: update_msg(const char * msg){
54+
55+
display.clearDisplay();
56+
display.setTextSize(MSG_CHAR_SIZE);
57+
display.setTextColor(SSD1306_WHITE);
58+
display.setCursor(MSG_X_POS, MSG_Y_POS);
59+
display.print(F(msg));
60+
61+
display.display();
62+
63+
display.startscrollleft(0x00, 0x0F);
64+
65+
delay(MSG_VIZ_TIME);
66+
67+
display.stopscroll();
68+
display.clearDisplay();
69+
}
70+
71+
/* Refresh the screen ; commit changes */
72+
void MySSD1306 :: commit_update(){
73+
display.display();
74+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef __MY_DISPLAY_H__
2+
#define __MY_DISPLAY_H__
3+
4+
/* Dependencies */
5+
#include "printables.h"
6+
#include <Wire.h>
7+
#include <Adafruit_GFX.h>
8+
#include <Adafruit_SSD1306.h>
9+
10+
/* Display address */
11+
#define I2C_SSD1306_ADDR 0x3C
12+
13+
/* Custom display class to add the needed functionalities */
14+
class MySSD1306{
15+
16+
private:
17+
18+
/* The display object that'll be used to do everything */
19+
Adafruit_SSD1306 display;
20+
21+
Adafruit_SSD1306 create_display(TwoWire * wire);
22+
23+
/* Send a command to the display */
24+
void clear_partial(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2);
25+
26+
public:
27+
28+
/* Constructor */
29+
MySSD1306(TwoWire * wire);
30+
31+
/* Draw initial screen with random values */
32+
void initial_display();
33+
34+
/* This function shall update the buffer without making the change */
35+
void update_button(uint8_t step, uint8_t len, PAIR_t * coordinates);
36+
void update_msg(const char * msg);
37+
38+
/* This function commits the changes needed in the display */
39+
void commit_update();
40+
};
41+
42+
#endif // __MY_DISPLAY_H__
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This example is a showcase for the stepped button-read in the MyButton library
3+
* using the readInSteps() function.
4+
* ----------------------------------------------------------------------------
5+
* To better showcase the usefulness of such function, the SSD1306 display
6+
* is used to animate a progress bar of a push-button.
7+
* ----------------------------------------------------------------------------
8+
* A wrapper class for the Adafruit_SSD1306 library was created for clarity purposes
9+
*/
10+
11+
#include "Display.h"
12+
#include <MyButton.h>
13+
14+
/*---------------------------------------------------------------------------------------*/
15+
16+
#define MY_BUTTON_PIN 32
17+
18+
/* Button object */
19+
MyButton my_btn(MY_BUTTON_PIN, NORMAL_UP);
20+
21+
/*---------------------------------------------------------------------------------------*/
22+
23+
/* Display object; Created the wrapper just for clarity purposes */
24+
TwoWire i2c_display_port = TwoWire(0); /* SDA 21, SCL 22 */
25+
MySSD1306 my_display(&i2c_display_port);
26+
27+
/*---------------------------------------------------------------------------------------*/
28+
29+
/* Message to be displayed when button is clicked for more than the specified period (in our case, 6s) */
30+
static const char msg[] PROGMEM = "MyButton Library, \nreadInSteps() demo";
31+
32+
/* Duration and number of steps in the animation */
33+
const uint32_t duration = 2000;
34+
const uint8_t num_steps = 10;
35+
36+
/* Button animation locations, the PAIR_t type is simply a pair holder (consult printables.h) */
37+
PAIR_t btn_locs[num_steps];
38+
39+
/*---------------------------------------------------------------------------------------*/
40+
41+
void setup() {
42+
/* Initialize the screen */
43+
my_display.initial_display();
44+
45+
/* Generate locations for the animations to follow */
46+
for(uint8_t i = 0; i < num_steps; i++){
47+
btn_locs[i] = {
48+
.x = LEFT_MOST_POS + i * (BUTTON_BMP_WIDTH + CLEARANCE),
49+
.y = (I2C_SCREEN_HEIGHT - BUTTON_BMP_HEIGHT)/ 2
50+
};
51+
}
52+
}
53+
54+
/*---------------------------------------------------------------------------------------*/
55+
56+
void loop() {
57+
58+
/* Get the timed step */
59+
uint8_t step = my_btn.readInSteps(duration, num_steps);
60+
61+
/* If in the middle of an animation, we visualize */
62+
if(step < num_steps){
63+
64+
/* Visulaize the next step of the animation */
65+
my_display.update_button(step, num_steps, btn_locs);
66+
67+
/* Reached end of sequence */
68+
if(step == num_steps - 1)my_display.update_msg(msg); /* Show msg */
69+
}
70+
71+
/* In case we stop clicking in the middle of the animation, we erase */
72+
else if(step == NON_CLICKED)my_display.update_button(num_steps - 1, num_steps, btn_locs);
73+
74+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef __PRINTABLES_DISPLAY_H__
2+
#define __PRINTABLES_DISPLAY_H__
3+
4+
#include"Arduino.h"
5+
6+
/* Screen dimentions */
7+
#define I2C_SCREEN_WIDTH 128
8+
#define I2C_SCREEN_HEIGHT 64
9+
10+
/* Message printing styles */
11+
#define MSG_CHAR_SIZE 1
12+
#define MSG_CHAR_HEIGHT (7 * MSG_CHAR_SIZE)
13+
#define MSG_CHAR_WIDTH (5 * MSG_CHAR_SIZE)
14+
#define MSG_Y_POS ((I2C_SCREEN_HEIGHT - MSG_CHAR_HEIGHT) / 2)
15+
#define MSG_X_POS MSG_CHAR_WIDTH
16+
#define MSG_VIZ_TIME 20000UL /* Milliseconds */
17+
18+
/* Button pressed animation params */
19+
#define LEFT_MOST_POS 4 /* Pixels from bottom of the screen */
20+
#define CLEARANCE 4 /* Pixels between the blocks */
21+
#define BUTTON_BMP_HEIGHT 16
22+
#define BUTTON_BMP_WIDTH 8
23+
24+
25+
typedef struct{
26+
uint8_t x;
27+
uint8_t y;
28+
}PAIR_t;
29+
30+
/* The bitmap to be displayed when a button is preseed, white block of 8x16 pixels */
31+
static const uint8_t PROGMEM button_bmp[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
32+
33+
#endif // __PRINTABLES_DISPLAY_H__

0 commit comments

Comments
 (0)