Skip to content

Commit 84f48a2

Browse files
author
ShawnHymel
committed
Added limiter to number of text scrolls. Added pixel addressing. Created demo for pixel addressing.
1 parent e76e163 commit 84f48a2

File tree

4 files changed

+210
-71
lines changed

4 files changed

+210
-71
lines changed

Libraries/SparkFun_LED_8x7/SparkFun_LED_8x7.cpp

Lines changed: 106 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,26 @@
1717

1818
#include "SparkFun_LED_8x7.h"
1919

20-
/* We need to create an object here so that the ISR knows what to talk to */
20+
/* We need to create a global instance so that the ISR knows what to talk to */
2121
SparkFun_LED_8x7 Plex;
2222

23+
/**
24+
* @brief Define static member for the location of the LEDs
25+
*/
26+
const charlieLed SparkFun_LED_8x7::charlie_leds_[] = {
27+
{0,1}, {0,2}, {0,3}, {0,4}, {0,5}, {0,6}, {0,7},
28+
{1,0}, {1,2}, {1,3}, {1,4}, {1,5}, {1,6}, {1,7},
29+
{2,0}, {2,1}, {2,3}, {2,4}, {2,5}, {2,6}, {2,7},
30+
{3,0}, {3,1}, {3,2}, {3,4}, {3,5}, {3,6}, {3,7},
31+
{4,0}, {4,1}, {4,2}, {4,3}, {4,5}, {4,6}, {4,7},
32+
{5,0}, {5,1}, {5,2}, {5,3}, {5,4}, {5,6}, {5,7},
33+
{6,0}, {6,1}, {6,2}, {6,3}, {6,4}, {6,5}, {6,7},
34+
{7,0}, {7,1}, {7,2}, {7,3}, {7,4}, {7,5}, {7,6}
35+
};
36+
37+
/**
38+
* @brief Constructor - Instantiates LED array object
39+
*/
2340
SparkFun_LED_8x7::SparkFun_LED_8x7()
2441
{
2542
/* Initialize members */
@@ -31,11 +48,23 @@ SparkFun_LED_8x7::SparkFun_LED_8x7()
3148

3249
}
3350

51+
/**
52+
* @brief Destructor
53+
*/
3454
SparkFun_LED_8x7::~SparkFun_LED_8x7()
3555
{
3656

3757
}
38-
58+
59+
/**
60+
* @brief Configures the pins on the Charlieplex array.
61+
*
62+
* You must call this function before performing any other actions on the
63+
* LED array.
64+
*
65+
* @param[in] pins Array of pin numbers. Must be 8 bytes long.
66+
* @return True if array configured. False on error.
67+
*/
3968
bool SparkFun_LED_8x7::init(byte pins[NUM_CHAPLEX_PINS])
4069
{
4170
/* If we are scrolling, stop and delete our string buffer */
@@ -80,19 +109,72 @@ bool SparkFun_LED_8x7::init(byte pins[NUM_CHAPLEX_PINS])
80109

81110
/* Clear and load frame buffer */
82111
clear();
112+
display();
83113

84114
return true;
85115
}
86116

117+
/**
118+
* @brief Writes the frame buffer to the LED buffer.
119+
*/
120+
void SparkFun_LED_8x7::display()
121+
{
122+
for ( byte i = 0; i < NUM_LEDS; i++ ) {
123+
chaplex_->ledWrite(charlie_leds_[i], frame_buffer_[i]);
124+
}
125+
}
126+
127+
/**
128+
* @brief Clears the Charlieplex array.
129+
*/
87130
void SparkFun_LED_8x7::clear()
88131
{
89-
for ( byte i = 0; i < 56; i++ ) {
90-
frame_buffer_[i] = 0;
132+
memset(frame_buffer_, 0, NUM_LEDS);
133+
}
134+
135+
/**
136+
* @brief Turns a pixel at a given (x, y) on or off
137+
*
138+
* Coordinates start (0, 0) from the top-left of the display.
139+
*
140+
* @param[in] x X coordinate for the pixel
141+
* @param[in] y Y coordinate for the pixel
142+
* @param[in] on 1 for on, 0 for off.
143+
*/
144+
void SparkFun_LED_8x7::pixel(uint8_t x, uint8_t y, uint8_t on /* = 1 */)
145+
{
146+
/* Check to make sure that we are not accessing outside the array */
147+
if ( x >= ROW_SIZE || y >= COL_SIZE ) {
148+
return;
149+
}
150+
151+
/* Turn the specified LED on or off. Note that we need to switch our X and Y
152+
* for the user, as X goes down and Y goes across on the actual LED display.
153+
*/
154+
if ( on ) {
155+
frame_buffer_[(x * COL_SIZE) + y] = 1;
156+
} else {
157+
frame_buffer_[(x * COL_SIZE) + y] = 0;
91158
}
92-
display();
93159
}
94160

161+
/**
162+
* @brief Sets text to scroll across the LED array indefinitely
163+
*
164+
* @param[in] in_string Text to scroll
165+
*/
95166
void SparkFun_LED_8x7::scrollText(char *in_string)
167+
{
168+
scrollText(in_string, 0);
169+
}
170+
171+
/**
172+
* @brief Scrolls text a specified number of times
173+
*
174+
* @param[in] in_string Text to scroll
175+
* @param[in] times Number of times to scroll the text
176+
*/
177+
void SparkFun_LED_8x7::scrollText(char *in_string, int times)
96178
{
97179
int i;
98180
int j;
@@ -110,6 +192,8 @@ void SparkFun_LED_8x7::scrollText(char *in_string)
110192
/* Reset our counters */
111193
shift_count_ = 0;
112194
scroll_index_ = 0;
195+
scroll_times_ = times;
196+
scroll_count_ = 0;
113197

114198
/* Calculate characters in the string */
115199
text_len = strlen(in_string);
@@ -190,9 +274,11 @@ void SparkFun_LED_8x7::scrollText(char *in_string)
190274

191275
/* Start scrolling */
192276
scrolling_ = 1;
193-
194277
}
195278

279+
/**
280+
* @brief Stops scrolling text and deletes scroll buffer
281+
*/
196282
void SparkFun_LED_8x7::stopScrolling()
197283
{
198284
scrolling_ = 0;
@@ -201,13 +287,7 @@ void SparkFun_LED_8x7::stopScrolling()
201287
scroll_buf_ = NULL;
202288
}
203289
clear();
204-
}
205-
206-
void SparkFun_LED_8x7::display()
207-
{
208-
for ( byte i = 0; i < 56; i++ ) {
209-
chaplex_->ledWrite(g_leds[i], frame_buffer_[i]);
210-
}
290+
display();
211291
}
212292

213293
void SparkFun_LED_8x7::isr()
@@ -242,6 +322,12 @@ void SparkFun_LED_8x7::isr()
242322
scroll_index_++;
243323
if ( scroll_index_ >= scroll_len_ ) {
244324
scroll_index_ = 0;
325+
if ( scroll_times_ > 0 ) {
326+
scroll_count_++;
327+
if ( scroll_count_ >= scroll_times_ ) {
328+
stopScrolling();
329+
}
330+
}
245331
}
246332
}
247333
}
@@ -252,6 +338,13 @@ void SparkFun_LED_8x7::isr()
252338
TIMSK2 |= (1 << TOIE2); // Enable timer overflow interrupt
253339
}
254340

341+
/**
342+
* @brief Global interrupt service routine for Timer 2
343+
*
344+
* We define Timer 2 ISR here to allow us to make calls to functions in the
345+
* SparkFun_LED_8x7 class. To do this, we instantiate a SparkFun_LED_8x7 object
346+
* (globally) in the .cpp file.
347+
**/
255348
ISR(TIMER2_OVF_vect) {
256349
Plex.isr();
257350
}

Libraries/SparkFun_LED_8x7/SparkFun_LED_8x7.h

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,8 @@
3838
#define ALL_BUT_LAST_COL NUM_LEDS - COL_SIZE
3939

4040
/* Global variables */
41-
const charlieLed g_leds[56] = {{0,1}, {0,2}, {0,3}, {0,4}, {0,5}, {0,6}, {0,7},
42-
{1,0}, {1,2}, {1,3}, {1,4}, {1,5}, {1,6}, {1,7},
43-
{2,0}, {2,1}, {2,3}, {2,4}, {2,5}, {2,6}, {2,7},
44-
{3,0}, {3,1}, {3,2}, {3,4}, {3,5}, {3,6}, {3,7},
45-
{4,0}, {4,1}, {4,2}, {4,3}, {4,5}, {4,6}, {4,7},
46-
{5,0}, {5,1}, {5,2}, {5,3}, {5,4}, {5,6}, {5,7},
47-
{6,0}, {6,1}, {6,2}, {6,3}, {6,4}, {6,5}, {6,7},
48-
{7,0}, {7,1}, {7,2}, {7,3}, {7,4}, {7,5}, {7,6}
49-
};
41+
5042

51-
/**
52-
* @brief Global interrupt service routine for Timer 2
53-
*
54-
* We declare Timer 2 ISR here to allow us to make calls to functions in the
55-
* SparkFun_LED_8x7 class. To do this, we instantiate a SparkFun_LED_8x7 object
56-
* (globally) in the .cpp file. This is then used in the main sketch.
57-
**/
5843
ISR(TIMER2_OVF_vect);
5944

6045
/* LED Array class */
@@ -65,51 +50,27 @@ class SparkFun_LED_8x7 {
6550

6651
public:
6752

68-
/**
69-
* @brief Constructor - Instantiates LED array object
70-
*/
53+
/* Initialization */
7154
SparkFun_LED_8x7();
72-
73-
/**
74-
* @brief Destructor
75-
*/
7655
~SparkFun_LED_8x7();
77-
78-
/**
79-
* @brief Configures the pins on the Charlieplex array.
80-
*
81-
* You must call this function before performing any other actions on the
82-
* LED array.
83-
*
84-
* @param[in] pins Array of pin numbers. Must be 8 bytes long.
85-
* @return True if array configured. False on error.
86-
*/
8756
bool init(byte pins[NUM_CHAPLEX_PINS]);
8857

89-
/**
90-
* @brief Clears the Charlieplex array.
91-
*/
58+
/* LED drawing methods */
59+
void display();
9260
void clear();
61+
void pixel(uint8_t x, uint8_t y, uint8_t on = 1);
9362

94-
/**
95-
* @brief Sets text to scroll across the LED array
96-
*
97-
* @param[in] in_string Text to scroll
98-
*/
63+
/* Scrolling text methods */
9964
void scrollText(char *in_string);
100-
101-
/**
102-
* @brief Stops scrolling text and deletes scroll buffer
103-
*/
65+
void scrollText(char *in_string, int times);
10466
void stopScrolling();
105-
106-
/**
107-
* @brief Writes the frame buffer to the LED buffer.
108-
*/
109-
void display();
67+
11068

11169
private:
11270

71+
/* Interrupt service routine that is called by the system's ISR */
72+
inline void isr();
73+
11374
/* Members */
11475
Chaplex *chaplex_; /// Chaplex object for controlling the LEDs
11576
byte frame_buffer_[56]; /// Storing the state of each LED to be written
@@ -120,11 +81,12 @@ class SparkFun_LED_8x7 {
12081
unsigned int shift_delay_; /// Number of ticks to wait before shifting text
12182
unsigned int scroll_index_; /// Index of where to scroll text
12283
unsigned int scroll_len_; /// Number of bytes in scroll_buf_
123-
124-
inline void isr();
125-
84+
unsigned int scroll_times_; /// Number of times to scroll text
85+
unsigned int scroll_count_; /// Counter for times text has scrolled
86+
static const charlieLed charlie_leds_[]; /// Relative location of the LEDs
12687
};
12788

89+
/* Yup, we're going to declare a global instance of our object */
12890
extern SparkFun_LED_8x7 Plex;
12991

13092
#endif // SparkFun_LED_8x7_H
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
/****************************************************************
3+
RandomPixels.ino
4+
LED Array 8x7 Charlieplex
5+
Shawn Hymel @ SparkFun Electronics
6+
February 9, 2015
7+
https://github.com/sparkfun/LED_Array_8x7_Charlieplex
8+
9+
Randomly turn pixels on and off.
10+
11+
Hardware Connections:
12+
13+
IMPORTANT: The Charlieplex LED board is designed for 2.0 - 3.3V!
14+
Higher voltages can damage the LEDs.
15+
16+
Arduino Pin | Charlieplex Board
17+
------------|------------------
18+
2 | 2
19+
3 | 3
20+
4 | 4
21+
5 | 5
22+
6 | 6
23+
7 | 7
24+
8 | 8
25+
9 | 9
26+
27+
Resources:
28+
Include Chaplex.h, SparkFun_LED_8x7.h
29+
The Chaplex library can be found at:
30+
http://playground.arduino.cc/Code/Chaplex
31+
32+
Development environment specifics:
33+
Written in Arduino 1.0.6
34+
Tested with SparkFun BadgerStick (Interactive Badge)
35+
36+
This code is beerware; if you see me (or any other SparkFun
37+
employee) at the local, and you've found our code helpful, please
38+
buy us a round!
39+
40+
Distributed as-is; no warranty is given.
41+
****************************************************************/
42+
43+
#include <SparkFun_LED_8x7.h>
44+
#include <Chaplex.h>
45+
46+
// Global variables
47+
static byte led_pins[] = {9,8,7,6,5,4,3,2};
48+
int x;
49+
int y;
50+
int state;
51+
52+
void setup() {
53+
54+
// Initialize and clear display
55+
Plex.init(led_pins);
56+
Plex.clear();
57+
Plex.display();
58+
59+
// Seed our random number generator using the "random"
60+
// voltage on pin A0
61+
randomSeed(analogRead(0));
62+
}
63+
64+
void loop() {
65+
66+
// Choose a random number between 0 and 7 for x coordinate
67+
x = random(0, 8);
68+
69+
// Choose a random number between 0 and 6 for y coordinate
70+
y = random(0, 7);
71+
72+
// Flip a coin for the state of the LED
73+
state = random(0, 2);
74+
75+
// Write to the LED display and wait before doing it again
76+
Plex.pixel(x, y, state);
77+
Plex.display();
78+
delay(10);
79+
}

0 commit comments

Comments
 (0)