fixed indentation after linebreak, made some refactoring

This commit is contained in:
hhaupt 2024-06-05 13:13:26 +02:00
parent 4ffed430e2
commit e30395e8a6
4 changed files with 88 additions and 71 deletions

View File

@ -10,15 +10,20 @@
*/ */
#define SDA_PIN 1
#define SCL_PIN 2
#include "Dezibot.h" #include "Dezibot.h"
#include <Wire.h>
Dezibot::Dezibot():multiColorLight(){}; Dezibot::Dezibot():multiColorLight(){};
void Dezibot::begin(void) { void Dezibot::begin(void) {
//infraredLight.begin(); Wire.begin(SDA_PIN,SCL_PIN);
//lightDetection.begin(); infraredLight.begin();
//motion.begin(); lightDetection.begin();
//multiColorLight.begin(); motion.begin();
multiColorLight.begin();
display.begin(); display.begin();
}; };

View File

@ -1,3 +1,22 @@
/**
* @file CharTable.h
* @author Hans Haupt (hans.haupt@dezibot.de)
* @brief LookUpTable for 8x8 Pixel Characters for an SSD1306 Display
* @version 0.1
* @date 2024-05-24
*
* @copyright Copyright (c) 2024
*
*/
/**
* @brief First index specifies the index, where index equals the ascii encoding
* unprintable characters are encode as all zero
* the first byte in an entry is the cmd_byte for SSD1306 and therefore not printed
* Encoding is colum wise, so first byte is the first column of the char and so on
*
*/
const char font8x8_colwise[128][9] = {{ 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // U+0() const char font8x8_colwise[128][9] = {{ 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // U+0()
{ 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // U+1() { 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // U+1()
{ 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // U+2() { 0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // U+2()

View File

@ -1,47 +1,28 @@
/**
* @file Display.cpp
* @author Hans Haupt (hans.haupt@dezibot.de)
* @brief Adds the ability to print to the display of the robot.
* @version 0.1
* @date 2024-06-05
*
* @copyright Copyright (c) 2024
*/
#include "Display.h" #include "Display.h"
#include "CharTable.h" #include "CharTable.h"
#include "Wire.h" #include "Wire.h"
void Display::sendDisplayCMD(uint8_t cmd){
Wire.beginTransmission(DisplayAdress);
Wire.write(cmd_byte);
Wire.write(cmd);
Wire.endTransmission();
};
void Display::begin(void){ void Display::begin(void){
//TODO: change pin to #defines, first check if Wire.begin was already called
Wire.begin(1,2);
//set Mux Ratio //set Mux Ratio
sendDisplayCMD(muxRatio); sendDisplayCMD(muxRatio);
sendDisplayCMD(0x3f); sendDisplayCMD(0x3f);
sendDisplayCMD(setOffset); sendDisplayCMD(setOffset);
sendDisplayCMD(0x00); sendDisplayCMD(0x00);
sendDisplayCMD(setStartLine); sendDisplayCMD(setStartLine);
/*x-flip*/
//sendDisplayCMD(setSegmentMap);
//sendDisplayCMD(setSegmentReMap);
/*mapping of the rows*/
//sendDisplayCMD(setComHardwareConfig);
//sendDisplayCMD(0x10);
/*y-flip*/
//sendDisplayCMD( setComDirectionNormal);
//sendDisplayCMD(setComDirectionFlipped);
//sendDisplayCMD(setContrast);
//sendDisplayCMD(0x7f);
sendDisplayCMD(stopCompleteOn); sendDisplayCMD(stopCompleteOn);
/*which pixels are bright: normal = 1s are bright, inverese= 0s are bright*/ /*which pixels are bright: normal = 1s are bright, inverese= 0s are bright*/
sendDisplayCMD( setNormalMode); sendDisplayCMD( setNormalMode);
//sendDisplayCMD( setInverseMode);
sendDisplayCMD( setOscFreq); sendDisplayCMD( setOscFreq);
sendDisplayCMD(0x80); sendDisplayCMD(0x80);
@ -52,6 +33,14 @@ void Display::begin(void){
this->clear(); this->clear();
return; return;
}; };
void Display::sendDisplayCMD(uint8_t cmd){
Wire.beginTransmission(DisplayAdress);
Wire.write(cmd_byte);
Wire.write(cmd);
Wire.endTransmission();
};
void Display::clear(void){ void Display::clear(void){
sendDisplayCMD(addressingMode); sendDisplayCMD(addressingMode);
sendDisplayCMD(0x00); //horizontal sendDisplayCMD(0x00); //horizontal
@ -74,11 +63,6 @@ void Display::clear(void){
return; return;
}; };
/**
* @brief updates amount of chars on current rows and also the current row
*
* @param charAmount how many chars were added
*/
void Display::updateLine(uint charAmount) void Display::updateLine(uint charAmount)
{ {
if(charAmount+this->charsOnCurrLine>16) if(charAmount+this->charsOnCurrLine>16)
@ -90,44 +74,35 @@ void Display::updateLine(uint charAmount)
{ {
this->charsOnCurrLine = charAmount+this->charsOnCurrLine; this->charsOnCurrLine = charAmount+this->charsOnCurrLine;
} }
} };
void Display::print(char *value){ void Display::print(char *value){
char *nextchar; char *nextchar;
/* write data to the buffer */ /* write data to the buffer */
while(value && *value != '\0') //check if pointer is still valid and string is not terminated while(value && *value != '\0') //check if pointer is still valid and string is not terminated
{ {
//check if input is a predefined cmd //check if next character is a linebreak
if(*value=='\n') if(*value=='\n')
{ {
//value++; //fill the current line with blanks
//if(*nextchar=='n') while(this->charsOnCurrLine<16)
{
if(this->charsOnCurrLine==16)
{
this->currLine=currLine+1;
this->charsOnCurrLine=0;
}
while(this->charsOnCurrLine<17)
{ {
updateLine(1);
Wire.beginTransmission(DisplayAdress); Wire.beginTransmission(DisplayAdress);
for(int i = 0;i<9;i++){ for(int i = 0;i<9;i++){
Wire.write(font8x8_colwise[0][i]); Wire.write(font8x8_colwise[0][i]);
} }
Wire.endTransmission(); Wire.endTransmission();
updateLine(1);
if(charsOnCurrLine==16)
{
break;
}
}
} }
//make the linebreak
this->currLine=currLine+1;
this->charsOnCurrLine=0;
} }
else else
{ {
updateLine(1); updateLine(1);
Wire.beginTransmission(DisplayAdress); Wire.beginTransmission(DisplayAdress);
//print the character
for(int i = 0;i<9;i++){ for(int i = 0;i<9;i++){
Wire.write(font8x8_colwise[*value][i]); Wire.write(font8x8_colwise[*value][i]);
} }
@ -135,8 +110,11 @@ void Display::print(char *value){
} }
value++; value++;
} }
return; };
void Display::println(char *value){
this ->print(value);
this->print("\n");
}; };
void Display::flipOrientation(void){ void Display::flipOrientation(void){
@ -148,11 +126,13 @@ void Display::flipOrientation(void){
sendDisplayCMD(setSegmentReMap); sendDisplayCMD(setSegmentReMap);
} }
this->orientationFlipped = !this->orientationFlipped; this->orientationFlipped = !this->orientationFlipped;
}
void Display::println(char *value){
this ->print(value);
this->print("\n");
}; };
void Display::print(int value){
void Display::invertColor(void){
if(this->colorInverted){
sendDisplayCMD(setNormalMode);
} else {
sendDisplayCMD(setInverseMode);
}
this->colorInverted = !this->colorInverted;
}; };

View File

@ -19,10 +19,16 @@ class Display{
protected: protected:
//how many chars are on current line //how many chars are on current line
uint8_t charsOnCurrLine = 0; uint8_t charsOnCurrLine = 0;
//on which line are we currently printing //on which line are we currently printing
uint8_t currLine = 0; uint8_t currLine = 0;
//flag that marks if the y-orientation is currently flipped //flag that marks if the y-orientation is currently flipped
bool orientationFlipped = false; bool orientationFlipped = false;
//flag thats marks if the color is currently inverted
bool colorInverted = false;
/** /**
* @brief sends the passed cmd to the display, cmd_byte is added as prefix by the function * @brief sends the passed cmd to the display, cmd_byte is added as prefix by the function
* *
@ -65,11 +71,18 @@ class Display{
* @param value the string that should be printed * @param value the string that should be printed
*/ */
void println(char *value); void println(char *value);
void print(int value);
/** /**
* @brief flips the horizontal orientation of all content on the display * @brief flips the horizontal orientation of all content on the display
*/ */
void flipOrientation(void); void flipOrientation(void);
/**
* @brief inverts the pixelcolors, so pixels on will be set to off and currently off pixels will be turned off.
* affects already printed content as well as future prints.
*
*/
void invertColor(void);
}; };