mirror of
https://github.com/2martens/uni.git
synced 2026-05-06 11:26:25 +02:00
[ES] Aufgabe 5.1 angefangen
Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
126
es/blatt5/uebung5-1/uebung5-1.ino
Normal file
126
es/blatt5/uebung5-1/uebung5-1.ino
Normal file
@ -0,0 +1,126 @@
|
||||
#include <SPI.h>
|
||||
|
||||
// pins
|
||||
int slaveSelectPin = 10;
|
||||
int rstPin = 6;
|
||||
int dcPin = 5;
|
||||
|
||||
// constants
|
||||
int divider = 84;
|
||||
int maxBufferIndex = 288;
|
||||
|
||||
// buffer
|
||||
char screenBuffer[288];
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
|
||||
// intialize SPI
|
||||
SPI.begin(slaveSelectPin);
|
||||
SPI.setClockDivider(slaveSelectPin, divider);
|
||||
|
||||
// initialize pins
|
||||
pinMode(dcPin, OUTPUT);
|
||||
pinMode(rstPin, OUTPUT);
|
||||
|
||||
// initial reset of display
|
||||
resetDisplay();
|
||||
|
||||
// initialization of display
|
||||
digitalWrite(dcPin, LOW);
|
||||
SPI.transfer(slaveSelectPin, 0x21);
|
||||
SPI.transfer(slaveSelectPin, 0x14);
|
||||
SPI.transfer(slaveSelectPin, 0xb0);
|
||||
SPI.transfer(slaveSelectPin, 0x20);
|
||||
SPI.transfer(slaveSelectPin, 0x0c);
|
||||
digitalWrite(dcPin, HIGH);
|
||||
|
||||
// initialize screen buffer
|
||||
for (int i = 0; i < maxBufferIndex; i++) {
|
||||
screenBuffer[i] = 0x0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop function for main code
|
||||
*/
|
||||
void loop()
|
||||
{
|
||||
for (int y = 0; y < 84; y++) {
|
||||
for (int x = 0; x < 48; x++) {
|
||||
setPixel(x, y, 1);
|
||||
}
|
||||
flushBuffer();
|
||||
delay(20);
|
||||
}
|
||||
//flushBuffer();
|
||||
//delay(20);
|
||||
Serial.println("fertig");
|
||||
|
||||
}
|
||||
|
||||
void resetDisplay()
|
||||
{
|
||||
digitalWrite(rstPin, LOW);
|
||||
delay(500);
|
||||
digitalWrite(rstPin, HIGH);
|
||||
}
|
||||
|
||||
void flushBuffer()
|
||||
{
|
||||
for (int i = 0; i < maxBufferIndex; i++) {
|
||||
SPI.transfer(slaveSelectPin, screenBuffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the pixel at the given location.
|
||||
* @param x
|
||||
* @param y
|
||||
* @param value
|
||||
*/
|
||||
void setPixel(int x, int y, int value)
|
||||
{
|
||||
int bank = x / 8;
|
||||
int relativeRow = x - bank * 8;
|
||||
int bankStartIndex = bank * 48;
|
||||
int index = bankStartIndex + y;
|
||||
char bitValue = 0x0;
|
||||
|
||||
if (value == 1) {
|
||||
bitValue = 0xff;
|
||||
}
|
||||
|
||||
char existingValue = screenBuffer[index];
|
||||
char pixelMask = 1 << relativeRow;
|
||||
pixelMask = 0x0 | pixelMask;
|
||||
char invMask = ~ pixelMask;
|
||||
char newValue = pixelMask & bitValue;
|
||||
|
||||
char finalExistingValue = existingValue & invMask;
|
||||
char finalValue = finalExistingValue | newValue;
|
||||
|
||||
Serial.print("bank:");
|
||||
Serial.println(bank);
|
||||
Serial.print("relRow:");
|
||||
Serial.println(relativeRow);
|
||||
Serial.print("index:");
|
||||
Serial.println(index);
|
||||
Serial.print("value:");
|
||||
Serial.println(value);
|
||||
Serial.print("existingValue:");
|
||||
Serial.println(existingValue, BIN);
|
||||
Serial.print("finalExistingValue:");
|
||||
Serial.println(finalExistingValue, BIN);
|
||||
Serial.print("pixelMask:");
|
||||
Serial.println(pixelMask, BIN);
|
||||
Serial.print("invMask:");
|
||||
Serial.println(invMask, BIN);
|
||||
Serial.print("newValue:");
|
||||
Serial.println(newValue, BIN);
|
||||
Serial.print("finalValue:");
|
||||
Serial.println(finalValue, BIN);
|
||||
Serial.println("-----");
|
||||
screenBuffer[index] = finalValue;
|
||||
}
|
||||
Reference in New Issue
Block a user