1
0
mirror of https://github.com/2martens/uni.git synced 2026-05-06 11:26:25 +02:00

[ES] Finished 6.1

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
2015-07-07 16:28:01 +02:00
parent 19f7cfc78f
commit b1733ead11

View File

@ -153,20 +153,13 @@ void setup()
*/ */
void loop() void loop()
{ {
// write my data if (Serial.available() > 0) {
printString(9, 33, "Jim", 3); String command = Serial.readString();
printString(19, 21, "Martens", 7); resetBuffer();
printString(29, 21, "6420323", 7); handleCommand(command);
flushBuffer(); flushBuffer();
resetBuffer(); delay(500);
delay(5000); }
// write his data
printString(9, 21, "Joachim", 7);
printString(19, 6, "Schmidberger", 12);
printString(29, 21, "6536496", 7);
flushBuffer();
resetBuffer();
delay(5000);
} }
/** /**
@ -289,16 +282,21 @@ int printString(int x, int y, const char* text, int textLength)
bool writeError = false; bool writeError = false;
for (int i = 0; i < textLength; i++) { for (int i = 0; i < textLength; i++) {
char character = text[i]; char character = text[i];
actualY++; int result = 0;
// write dash // write dash
if (actualY > 72) { if ((actualY + 6) > 72) {
printChar(actualX, actualY, '-'); printChar(actualX, actualY, '-');
actualX += 10; actualX += 10;
actualY = y; actualY = y;
result = printChar(actualX, actualY, character);
} }
else {
int result = printChar(actualX, actualY, character); result = printChar(actualX, actualY, character);
if (!result) { }
actualY += 6;
if (result != 0) {
// the character would be out of the display x bounds // the character would be out of the display x bounds
writeError = true; writeError = true;
break; break;
@ -306,8 +304,9 @@ int printString(int x, int y, const char* text, int textLength)
} }
if (writeError) { if (writeError) {
return 0;
resetBuffer(); resetBuffer();
printString(3, 20, "Too long text", 13); printString(3, 20, "Too long", 8);
} }
return 0; return 0;
@ -329,7 +328,7 @@ void printImage(int x, int y, int rows, int cols, const int* pixels)
int maxIndex = rows * cols; int maxIndex = rows * cols;
for (int i = 0; i < maxIndex; i++) { for (int i = 0; i < maxIndex; i++) {
if (currentY == cols) { if (currentY == (y + cols)) {
currentX++; currentX++;
currentY = y; currentY = y;
} }
@ -343,25 +342,16 @@ void printImage(int x, int y, int rows, int cols, const int* pixels)
/** /**
* Reads the contents of a text file into a string. * Reads the contents of a text file into a string.
* *
* Returns an empty string on failure.
*
* @param File file * @param File file
* @param char* text
*/ */
const char* readString(File file) void readString(File file, char* text, unsigned long fileSize)
{ {
if (file) { if (file) {
unsigned long fileSize = file.size(); int readBytes = file.readBytesUntil('\n', text, fileSize);
char result[fileSize]; text[readBytes] = '\0';
int readBytes = file.readBytesUntil('\n', result, fileSize);
result[readBytes] = '\0';
readStringLength = readBytes + 1; readStringLength = readBytes;
return result;
}
else {
// something happened
return "";
} }
} }
@ -371,18 +361,18 @@ const char* readString(File file)
* @param const int* input * @param const int* input
* @param int length * @param int length
* @param char delimeter * @param char delimeter
* @param int* result
*/ */
const int* explodeString(const char* input, int length, char delimeter) void explodeString(const char* input, int length, char delimeter, int* result)
{ {
int result[length] = {}; char tmp[length];
char tmp[length] = {};
int nthChar = 0; int nthChar = 0;
int nthInt = 0; int nthInt = 0;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
char currentChar = input[i]; char currentChar = input[i];
if (currentChar != delimeter) { if (currentChar != delimeter) {
tmp[nthChar] = input[i]; tmp[nthChar] = currentChar;
nthChar++; nthChar++;
} }
else { else {
@ -390,12 +380,17 @@ const int* explodeString(const char* input, int length, char delimeter)
tmp[nthChar] = '\0'; tmp[nthChar] = '\0';
result[nthInt] = (int) strtol(tmp, &end, 10); result[nthInt] = (int) strtol(tmp, &end, 10);
nthChar = 0; nthChar = 0;
memset(&tmp[0], 0, sizeof(tmp)); memset(&tmp[0], 0, length);
nthInt++; nthInt++;
} }
} }
return result; char *end;
tmp[nthChar] = '\0';
result[nthInt] = (int) strtol(tmp, &end, 10);
nthChar = 0;
memset(&tmp[0], 0, length);
nthInt++;
} }
/** /**
@ -413,7 +408,7 @@ void handleCommand(String command)
int indexP2 = command.indexOf(')'); int indexP2 = command.indexOf(')');
String command2 = command; String command2 = command;
command2.remove(indexP1); command2.remove(indexP1);
if (command2 != existCommand && command2 != showCommand) { if (command2 != existCommand && command2 != showCommand) {
Serial.println("Use doesFileExist(<file>) or showFile(<file>)"); Serial.println("Use doesFileExist(<file>) or showFile(<file>)");
return; return;
@ -421,9 +416,12 @@ void handleCommand(String command)
command.remove(indexP2); command.remove(indexP2);
String parameter = command.substring(indexP1 + 1); String parameter = command.substring(indexP1 + 1);
char paramChar[parameter.length() + 1];
parameter.toCharArray(paramChar, parameter.length() + 1);
paramChar[parameter.length()] = '\0';
if (command2 == existCommand) { if (command2 == existCommand) {
bool fileExists = SD.exists(parameter); bool fileExists = SD.exists(paramChar);
Serial.print("Does file exist? "); Serial.print("Does file exist? ");
Serial.println(fileExists ? "Yes" : "No"); Serial.println(fileExists ? "Yes" : "No");
} }
@ -444,27 +442,42 @@ void showFile(String fileName)
int extPos = fileName.indexOf('.'); int extPos = fileName.indexOf('.');
String ext = fileName.substring(extPos + 1); String ext = fileName.substring(extPos + 1);
isTextFile = (ext == "txt"); isTextFile = (ext == "txt");
char fileNameChar[fileName.length() + 1];
fileName.toCharArray(fileNameChar, fileName.length() + 1);
fileNameChar[fileName.length()] = '\0';
if (SD.exists(fileName)) { if (SD.exists(fileNameChar)) {
File file = SD.open(fileName); File file = SD.open(fileNameChar);
unsigned long fileSize = file.size();
if (isTextFile) { if (isTextFile) {
const char* text = readString(file); char text[fileSize];
readString(file, text, fileSize);
Serial.println(text);
printString(0, 0, text, readStringLength); printString(0, 0, text, readStringLength);
} }
else { else {
const char* dimensions = readString(file); char dimensions[fileSize];
readString(file, dimensions, fileSize);
int dimensionsLength = readStringLength; int dimensionsLength = readStringLength;
const char* pixels = readString(file); char pixels[fileSize];
readString(file, pixels, fileSize);
int pixelsLength = readStringLength; int pixelsLength = readStringLength;
// parse dimensions and pixels // parse dimensions and pixels
const int* dimensionsInt = explodeString(dimensions, dimensionsLength, ','); int dimensionsInt[2];
const int* pixelsInt = explodeString(pixels, pixelsLength, ','); explodeString(dimensions, dimensionsLength, ',', dimensionsInt);
int pixelsInt[pixelsLength];
explodeString(pixels, pixelsLength, ',', pixelsInt);
// calculate x and y start positions // calculate x and y start positions
int rows = dimensionsInt[0]; int rows = dimensionsInt[0];
int cols = dimensionsInt[1]; int cols = dimensionsInt[1];
int x = (48 - rows) / 2; int x = (48 - rows) / 2;
int y = (84 - cols) / 2; int y = (84 - cols) / 2;
Serial.println(x);
Serial.println(y);
Serial.println(rows);
Serial.println(cols);
printImage(x, y, rows, cols, pixelsInt); printImage(x, y, rows, cols, pixelsInt);
} }