Skip to content

Commit 1fb6679

Browse files
mama
authored andcommitted
added StackArray as local library
1 parent 1e26fc7 commit 1fb6679

File tree

7 files changed

+1061
-3
lines changed

7 files changed

+1061
-3
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,5 @@ jobs:
3838
arduino-cli core install arduino:avr
3939
- name: Install MIDI library
4040
run: arduino-cli lib install "MIDI [email protected]"
41-
- name: Install StackArray library
42-
run: arduino-cli lib install --git-url "https://github.com/oogre/StackArray.git"
4341
- name: Compile Sketch
4442
run: arduino-cli compile --fqbn arduino:avr:mega ./arduino_monotron.ino --warnings more

arduino_monotron.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <MIDI.h>
22
#include <SPI.h>
3-
#include <StackArray.h>
3+
#include "libraries/StackArray/StackArray.h"
44

55
//http://www.ripmat.it/mate/d/dc/dcee.html
66
//https://www.arduino.cc/en/Tutorial/DueSimpleWaveformGenerator

libraries/StackArray/COPYING

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Reverse a string by using a generic, dynamic stack data structure.
3+
*
4+
* Copyright (C) 2010 Efstathios Chatzikyriakidis ([email protected])
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
// include stack library header.
21+
#include <StackArray.h>
22+
23+
// declare a string message.
24+
const String msg = "Happy Hacking!";
25+
26+
// create a stack of characters.
27+
StackArray <char> stack;
28+
29+
// startup point entry (runs once).
30+
void
31+
setup () {
32+
// start serial communication.
33+
Serial.begin (9600);
34+
35+
// set the printer of the stack.
36+
stack.setPrinter (Serial);
37+
38+
// print the message before reversing.
39+
Serial.println ("Normal String: " + msg);
40+
41+
// push all the message's characters to the stack.
42+
for (int i = 0; i < msg.length (); i++)
43+
stack.push (msg.charAt (i));
44+
45+
// print the message after reversing.
46+
Serial.print ("Reversed String: ");
47+
48+
// pop all the message's characters from the stack.
49+
while (!stack.isEmpty ())
50+
Serial.print (stack.pop ());
51+
52+
// print end of line character.
53+
Serial.println ();
54+
}
55+
56+
// loop the main sketch.
57+
void
58+
loop () {
59+
// nothing here.
60+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Swapping numbers by using a generic, dynamic stack data structure.
3+
*
4+
* Copyright (C) 2010 Efstathios Chatzikyriakidis ([email protected])
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
// include stack library header.
21+
#include <StackArray.h>
22+
23+
// declare two numbers.
24+
double a = 1.1;
25+
double b = 2.2;
26+
27+
// create a stack of numbers.
28+
StackArray <double> stack;
29+
30+
// startup point entry (runs once).
31+
void
32+
setup () {
33+
// start serial communication.
34+
Serial.begin (9600);
35+
36+
// set the printer of the stack.
37+
stack.setPrinter (Serial);
38+
}
39+
40+
// loop the main sketch.
41+
void
42+
loop () {
43+
// print the values of the numbers.
44+
Serial.print ("a: "); Serial.println (a);
45+
Serial.print ("b: "); Serial.println (b);
46+
47+
// push the numbers to the stack.
48+
stack.push (a);
49+
stack.push (b);
50+
51+
// pop the numbers from the stack.
52+
a = stack.pop ();
53+
b = stack.pop ();
54+
55+
// delay 1 second.
56+
delay (1000);
57+
}

libraries/StackArray/StackArray.h

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
/*
2+
* StackArray.h
3+
*
4+
* Library implementing a generic, dynamic stack (array version).
5+
*
6+
* ---
7+
*
8+
* Copyright (C) 2010 Efstathios Chatzikyriakidis ([email protected])
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
* ---
24+
*
25+
* Version 1.0
26+
*
27+
* 2010-09-25 Efstathios Chatzikyriakidis <[email protected]>
28+
*
29+
* - added resize(): for growing, shrinking the array size.
30+
*
31+
* 2010-09-23 Efstathios Chatzikyriakidis <[email protected]>
32+
*
33+
* - added exit(), blink(): error reporting and handling methods.
34+
*
35+
* 2010-09-20 Alexander Brevig <[email protected]>
36+
*
37+
* - added setPrinter(): indirectly reference a Serial object.
38+
*
39+
* 2010-09-15 Efstathios Chatzikyriakidis <[email protected]>
40+
*
41+
* - initial release of the library.
42+
*
43+
* ---
44+
*
45+
* For the latest version see: http://www.arduino.cc/
46+
*/
47+
48+
// header defining the interface of the source.
49+
#ifndef _STACKARRAY_H
50+
#define _STACKARRAY_H
51+
52+
// include Arduino basic header.
53+
#include <Arduino.h>
54+
55+
// the definition of the stack class.
56+
template<typename T>
57+
class StackArray {
58+
public:
59+
// init the stack (constructor).
60+
StackArray ();
61+
62+
// clear the stack (destructor).
63+
~StackArray ();
64+
65+
// push an item to the stack.
66+
void push (const T i);
67+
68+
// pop an item from the stack.
69+
T pop ();
70+
71+
// get an item from the stack.
72+
T peek () const;
73+
74+
// check if the stack is empty.
75+
bool isEmpty () const;
76+
77+
// get the number of items in the stack.
78+
int count () const;
79+
80+
// check if the stack is full.
81+
bool isFull () const;
82+
83+
// set the printer of the stack.
84+
void setPrinter (Print & p);
85+
86+
private:
87+
// resize the size of the stack.
88+
void resize (const int s);
89+
90+
// exit report method in case of error.
91+
void exit (const char * m) const;
92+
93+
// led blinking method in case of error.
94+
void blink () const;
95+
96+
// the initial size of the stack.
97+
static const int initialSize = 2;
98+
99+
// the pin number of the on-board led.
100+
static const int ledPin = 13;
101+
102+
Print * printer; // the printer of the stack.
103+
T * contents; // the array of the stack.
104+
int size; // the size of the stack.
105+
int top; // the top index of the stack.
106+
};
107+
108+
// init the stack (constructor).
109+
template<typename T>
110+
StackArray<T>::StackArray () {
111+
size = 0; // set the size of stack to zero.
112+
top = 0; // set the initial top index of the stack.
113+
printer = NULL; // set the printer of stack to point nowhere.
114+
115+
// allocate enough memory for the array.
116+
contents = (T *) malloc (sizeof (T) * initialSize);
117+
118+
// if there is a memory allocation error.
119+
if (contents == NULL)
120+
exit ("STACK: insufficient memory to initialize stack.");
121+
122+
// set the initial size of the stack.
123+
size = initialSize;
124+
}
125+
126+
// clear the stack (destructor).
127+
template<typename T>
128+
StackArray<T>::~StackArray () {
129+
free (contents); // deallocate the array of the stack.
130+
131+
contents = NULL; // set stack's array pointer to nowhere.
132+
printer = NULL; // set the printer of stack to point nowhere.
133+
size = 0; // set the size of stack to zero.
134+
top = 0; // set the initial top index of the stack.
135+
}
136+
137+
// resize the size of the stack.
138+
template<typename T>
139+
void StackArray<T>::resize (const int s) {
140+
// defensive issue.
141+
if (s <= 0)
142+
exit ("STACK: error due to undesirable size for stack size.");
143+
144+
// reallocate enough memory for the array.
145+
contents = (T *) realloc (contents, sizeof (T) * s);
146+
147+
// if there is a memory allocation error.
148+
if (contents == NULL)
149+
exit ("STACK: insufficient memory to resize stack.");
150+
151+
// set the new size of the stack.
152+
size = s;
153+
}
154+
155+
// push an item to the stack.
156+
template<typename T>
157+
void StackArray<T>::push (const T i) {
158+
// check if the stack is full.
159+
if (isFull ())
160+
// double size of array.
161+
resize (size * 2);
162+
163+
// store the item to the array.
164+
contents[top++] = i;
165+
}
166+
167+
// pop an item from the stack.
168+
template<typename T>
169+
T StackArray<T>::pop () {
170+
// check if the stack is empty.
171+
if (isEmpty ())
172+
exit ("STACK: can't pop item from stack: stack is empty.");
173+
174+
// fetch the top item from the array.
175+
T item = contents[--top];
176+
177+
// shrink size of array if necessary.
178+
if (!isEmpty () && (top <= size / 4))
179+
resize (size / 2);
180+
181+
// return the top item from the array.
182+
return item;
183+
}
184+
185+
// get an item from the stack.
186+
template<typename T>
187+
T StackArray<T>::peek () const {
188+
// check if the stack is empty.
189+
if (isEmpty ())
190+
exit ("STACK: can't peek item from stack: stack is empty.");
191+
192+
// get the top item from the array.
193+
return contents[top - 1];
194+
}
195+
196+
// check if the stack is empty.
197+
template<typename T>
198+
bool StackArray<T>::isEmpty () const {
199+
return top == 0;
200+
}
201+
202+
// check if the stack is full.
203+
template<typename T>
204+
bool StackArray<T>::isFull () const {
205+
return top == size;
206+
}
207+
208+
// get the number of items in the stack.
209+
template<typename T>
210+
int StackArray<T>::count () const {
211+
return top;
212+
}
213+
214+
// set the printer of the stack.
215+
template<typename T>
216+
void StackArray<T>::setPrinter (Print & p) {
217+
printer = &p;
218+
}
219+
220+
// exit report method in case of error.
221+
template<typename T>
222+
void StackArray<T>::exit (const char * m) const {
223+
// print the message if there is a printer.
224+
if (printer)
225+
printer->println (m);
226+
227+
// loop blinking until hardware reset.
228+
blink ();
229+
}
230+
231+
// led blinking method in case of error.
232+
template<typename T>
233+
void StackArray<T>::blink () const {
234+
// set led pin as output.
235+
pinMode (ledPin, OUTPUT);
236+
237+
// continue looping until hardware reset.
238+
while (true) {
239+
digitalWrite (ledPin, HIGH); // sets the LED on.
240+
delay (250); // pauses 1/4 of second.
241+
digitalWrite (ledPin, LOW); // sets the LED off.
242+
delay (250); // pauses 1/4 of second.
243+
}
244+
245+
// solution selected due to lack of exit() and assert().
246+
}
247+
248+
#endif // _STACKARRAY_H

0 commit comments

Comments
 (0)