Skip to content

Commit c6658bd

Browse files
committed
initial commit
0 parents  commit c6658bd

13 files changed

+4363
-0
lines changed

Buffer.cpp

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/*
2+
The buffer is a class than handles all data regarding the inkjet. It takes burst and position information and stores it in a FiFo buffer. The buffer is what takes the most memory on the microcontroller
3+
4+
Todo:
5+
- Do not meticulously calculate buffer read and write left, but simply hold a variable that keeps this data. It will not magically add 15 lines without the functions knowing about it.
6+
*/
7+
8+
#include "Arduino.h"
9+
#include "Buffer.h" //<*whispers: "there is actually nothing in there"
10+
11+
#define BUFFER_SIZE 4003 //the number of 48 byte blocks the buffer consists of (44 for inkjet, 4 for coordinate) (+3 for off by ones)
12+
#define PRIMITIVE_OVERLAY_EVEN 7384 //B0001110011011000
13+
#define PRIMITIVE_OVERLAY_ODD 8999 //B0010001100100111
14+
15+
#define BUFFER_MODE_CLEARING 0 //where the buffer line is cleared after use
16+
#define BUFFER_MODE_STATIC 1 //where the buffer line is retained after use
17+
#define BUFFER_MODE_LOOPING 2 //where the buffer resets to position 0 after it reaches the end
18+
19+
#define BUFFER_PRINT_MODE_ALL 0 //both odd and even active
20+
#define BUFFER_PRINT_MODE_ODD 1 //Only odd active
21+
#define BUFFER_PRINT_MODE_EVEN 2 //Only even active
22+
23+
class Buffer {
24+
//class member variables
25+
uint16_t burstBuffer[BUFFER_SIZE][22]; //burst data
26+
int32_t positionBuffer[BUFFER_SIZE]; //position data
27+
int32_t readPosition[2], writePosition; //read and write positions for odd and even
28+
uint16_t primitiveOverlay[2]; //0 or 1 for even or odd
29+
//int32_t readLeft[2], writeLeft; //future additions
30+
uint8_t sideActive[2] = {0, 0}; //turns the overlay on or off for a side
31+
uint8_t bufferMode = BUFFER_MODE_CLEARING;
32+
uint8_t bufferPrintMode = BUFFER_PRINT_MODE_ALL;
33+
uint8_t bufferLoopCounter = 0; //contains how often the buffer has looped. Is used to activate other functions
34+
35+
public:
36+
Buffer() {
37+
//make odd and even overlays, used for the 2 positions in the buffer
38+
primitiveOverlay[0] = PRIMITIVE_OVERLAY_ODD;
39+
primitiveOverlay[1] = PRIMITIVE_OVERLAY_EVEN;
40+
41+
ClearAll(); //reset the buffer
42+
}
43+
void SetMode(uint8_t tempMode) { //sets the mode the buffer operates at
44+
if (tempMode == BUFFER_MODE_CLEARING || tempMode == BUFFER_MODE_STATIC || tempMode == BUFFER_MODE_LOOPING) { //verify if the requested value is valid
45+
bufferMode = tempMode;
46+
}
47+
}
48+
uint8_t GetMode() { //get the current buffer mode
49+
return bufferMode;
50+
}
51+
int32_t Next(uint8_t tempSide) { //if possible, adds one to the read position and returns lines left. Data needs to be fetched using other functions
52+
tempSide &= 1; //constrain side
53+
54+
if (ReadLeftSide(tempSide) > 0) { //if there is something left to read
55+
readPosition[tempSide] ++; //add one to the read position
56+
readPosition[tempSide] = readPosition[tempSide] % BUFFER_SIZE; //overflow protection
57+
//Serial.print("Buffer next side: "); Serial.print(tempSide); Serial.print(", left: "); Serial.println(ReadLeftSide(tempSide));
58+
59+
if (bufferMode == BUFFER_MODE_LOOPING ) { //if the mode is looping
60+
if (ReadLeft() == 0) { //if there is nothing left to read on either side
61+
Reset(); //reset the buffer
62+
//Serial.println("Loop: Resetting buffer");
63+
bufferLoopCounter++;
64+
}
65+
}
66+
return ReadLeftSide(tempSide);
67+
}
68+
return -1; //return back -1 if it is not possible
69+
}
70+
int32_t LookAheadPosition(uint8_t tempSide) { //takes the position in the next buffer position
71+
tempSide &= 1; //constrain side
72+
int32_t tempReadPos;
73+
if (ReadLeftSide(tempSide) > 0) { //if there is something left ot read
74+
tempReadPos = readPosition[tempSide]; //make a temporary read position and make it go to the next pos
75+
tempReadPos ++;
76+
tempReadPos = tempReadPos % BUFFER_SIZE; //overflow protection
77+
return positionBuffer[tempReadPos];
78+
}
79+
return -1; //return -1 for error
80+
}
81+
int32_t Add(int32_t temp_position, uint16_t tempInput[22]) { //adds a coordinate (int32_t and a burst to the buffer, returns space left if successful, -1 if failed
82+
int32_t temp_left = WriteLeft();
83+
if (temp_left > 0) { //if there is space left in the buffer
84+
positionBuffer[writePosition] = temp_position; //add position
85+
//Serial.print("Add to buffer: "); Serial.print(temp_position); Serial.print(": ");
86+
for (uint8_t a = 0; a < 22; a++) { //add burst
87+
burstBuffer[writePosition][a] = tempInput[a];
88+
//Serial.print(tempInput[a]); Serial.print(", ");
89+
}
90+
//Serial.println("");
91+
writePosition++; //add one to write position
92+
writePosition = writePosition % BUFFER_SIZE; //overflow protection
93+
temp_left--;
94+
//Serial.print("Buffer write left: "); Serial.println(temp_left);
95+
//Serial.print("Buffer read left: "); Serial.println(ReadLeft());
96+
return temp_left;
97+
}
98+
return -1; //return a -1 if this failed
99+
}
100+
int32_t ReadLeft() { //returns the number of filled buffer read slots. Automatically returns the largest value
101+
//calculate read lines left on pos 0 by subtracting read position and adding write position, and then take the modulo of the buffer size to protect from overflows
102+
int32_t tempCalc1, tempCalc2;
103+
tempCalc1 = BUFFER_SIZE;
104+
tempCalc1 += writePosition;
105+
tempCalc2 = tempCalc1;
106+
tempCalc1 -= readPosition[0];
107+
tempCalc2 -= readPosition[1];
108+
tempCalc1 = tempCalc1 % BUFFER_SIZE; //constrain to buffer size
109+
tempCalc2 = tempCalc2 % BUFFER_SIZE;
110+
tempCalc1 -= 1; //subtract one because read can never be equal to write
111+
tempCalc2 -= 1;
112+
if (tempCalc1 > tempCalc2) { //return smallest value
113+
return tempCalc1;
114+
}
115+
else {
116+
return tempCalc2;
117+
}
118+
}
119+
int32_t ReadLeftSide(uint8_t tempSide) {//returns the number of lines left to read for a given size
120+
tempSide &= 1; //constrain side
121+
int32_t tempCalc;
122+
tempCalc = BUFFER_SIZE;
123+
tempCalc += writePosition;
124+
tempCalc -= readPosition[tempSide];
125+
tempCalc = tempCalc % BUFFER_SIZE; //constrain to buffer size
126+
tempCalc -= 1; //subtract one because read can never be equal to write
127+
return tempCalc;
128+
}
129+
int32_t WriteLeft() { //returns the number of free buffer write slots. Automatically returns the smallest value
130+
//calculate write lines left on pos 0 by subtracting write position and adding read position, and then take the modulo of the buffer size to protect from overflows
131+
int32_t tempCalc1, tempCalc2;
132+
if (bufferMode == BUFFER_MODE_CLEARING) { //if the buffer is cleared after a line is printed, calculate rolling value
133+
tempCalc1 = BUFFER_SIZE;
134+
tempCalc1 -= writePosition;
135+
tempCalc2 = tempCalc1;
136+
tempCalc1 += readPosition[0];
137+
tempCalc2 += readPosition[1];
138+
tempCalc1 = tempCalc1 % BUFFER_SIZE; //constrain to buffer size
139+
tempCalc2 = tempCalc2 % BUFFER_SIZE;
140+
tempCalc1 -= 2; //subtract to create some protection
141+
tempCalc2 -= 2;
142+
if (tempCalc1 < tempCalc2) { //return smallest value
143+
return tempCalc1;
144+
}
145+
else {
146+
return tempCalc2;
147+
}
148+
}
149+
if (bufferMode == BUFFER_MODE_STATIC || bufferMode == BUFFER_MODE_LOOPING) { //if the line is retained after it is printed
150+
tempCalc1 = BUFFER_SIZE; //start with the buffer size
151+
tempCalc1 -= writePosition; //subtract where we are currently writing
152+
tempCalc1 -= 2; //subtract to create some protection
153+
return tempCalc1;
154+
}
155+
return 0; //if you got here, something went horribly wrong
156+
}
157+
void ClearAll() { //resets the read and write positions in the buffer
158+
for (uint16_t b = 0; b < BUFFER_SIZE; b++) {
159+
for (uint8_t a = 0; a < 22; a++) {
160+
burstBuffer[b][a] = 0;
161+
}
162+
positionBuffer[b] = 0;
163+
}
164+
readPosition[0] = 0;
165+
readPosition[1] = 0;
166+
writePosition = 1;
167+
}
168+
void Reset() { //only resets the read position to the first array position
169+
readPosition[0] = 0;
170+
readPosition[1] = 0;
171+
}
172+
uint16_t GetPulse(uint8_t temp_address) {
173+
temp_address = constrain(temp_address, 0, 21);
174+
uint16_t tempPulse = 0; //make the return pulse
175+
tempPulse = tempPulse & PRIMITIVE_OVERLAY_EVEN & burstBuffer[readPosition[0]][temp_address];
176+
tempPulse = tempPulse & PRIMITIVE_OVERLAY_ODD & burstBuffer[readPosition[1]][temp_address];
177+
return tempPulse;
178+
}
179+
void SetActive(uint8_t tempSide, uint8_t tempState) { //set which side (odd or even) is on or off
180+
tempSide = constrain(tempSide, 0, 1);
181+
tempState = constrain(tempState, 0, 1);
182+
sideActive[tempSide] = tempState;
183+
}
184+
void SetPrintMode(uint8_t tempMode){ //set what mode the head prints at. 0 is both on, 1 is odd only, 2 is even only
185+
//this function looks a lot like SetActive, but is meant for more permanent settings
186+
if (tempMode == BUFFER_PRINT_MODE_ALL || tempMode == BUFFER_PRINT_MODE_ODD || tempMode == BUFFER_PRINT_MODE_EVEN){
187+
bufferPrintMode = tempMode;
188+
}
189+
190+
}
191+
uint16_t *GetBurst(uint16_t tempBurst[22]) { //gets the complete current burst, based on the 2 positions from buffer and writes it to the input uint16_t[22] array
192+
uint16_t tempOdd, tempEven; //temporary burst values
193+
for (uint8_t a = 0; a < 22; a++) { //walk through the entire pulse
194+
tempBurst[a] = 0; //reset value
195+
if (sideActive[0] == 1 && bufferPrintMode != BUFFER_PRINT_MODE_EVEN) { //if odd side is active
196+
tempOdd = PRIMITIVE_OVERLAY_ODD & burstBuffer[readPosition[0]][a]; //odd side
197+
}
198+
else {
199+
tempOdd = 0;
200+
}
201+
if (sideActive[1] == 1 && bufferPrintMode != BUFFER_PRINT_MODE_ODD) { //if even side is active
202+
tempEven = PRIMITIVE_OVERLAY_EVEN & burstBuffer[readPosition[1]][a]; //even side
203+
}
204+
else {
205+
tempEven = 0;
206+
}
207+
tempBurst[a] = tempBurst[a] | tempEven; //add even
208+
tempBurst[a] = tempBurst[a] | tempOdd; //add odd
209+
}
210+
return tempBurst;
211+
}
212+
int32_t GetPosition(uint8_t tempSide) {
213+
tempSide &= 1; //constrain side
214+
return positionBuffer[readPosition[tempSide]];
215+
}
216+
uint8_t GetLoopCounter(){
217+
return bufferLoopCounter;
218+
}
219+
220+
private:
221+
};

Buffer.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)