FSST - Hardwarenahe Programmierung
You are viewing an older revision! See the latest version
Date Codes 1
Date nur Tag¶
aus mbed-Simulator
und Intro
Copy-Paste die folgenden Codes
#include "mbed.h"
class Date // nur day implementiert; TODO: month and year
{
private:
uint8_t day;
public:
Date():day(1) // Initialisierungsliste mit konstantem Parameterwert
{}
// Initialisierungsliste über parametrisierten Konstruktor
Date(uint8_t _day):day(_day) { // entspircht: day = _day; .... siehe unten
}
Date(uint8_t _day, int _tmp) {
day = _day;
}
~Date() { printf("Good bye\n"); } // Destruktor
uint8_t GetDay(); // Prototyping
};
uint8_t Date::GetDay() {
return day;
}
int main()
{
Date date1; // Instanziierung mit Standard Konstruktor und
Date date2(18); // mit parametrisierten Konstruktor
Date date3(30, 0); // mit parametrisierten Konstruktor
printf("GetDay Test\n");
printf("Day 1: %d\n", date1.GetDay());
printf("Day 2: %d\n", date2.GetDay());
printf("Day 3: %d\n", date3.GetDay());
return 0;
}
Tag, Monat, Jahr¶
#include "mbed.h"
class Date
{
private:
uint8_t day, month, year;
public:
Date():day(1), month(1), year(00) // lnitialisierungsliste mit konstante Parameterwerten
{}
// Initialisierungsliste über parametrisierten Konstruktor
Date(uint8_t _day, uint8_t _month, uint8_t _year):day(_day), month(_month), year(_year) {
// entspircht: day = _day; ....
}
~Date() { printf("Good bye\n"); } // Destruktor
uint8_t GetDay(); // Prototyping
};
uint8_t Date::GetDay() {
return day;
}
int main()
{
Date date1; // Instanziierung mit Standard Konstruktor und
Date date2(18,4,15); // mit parametrisierten Konstruktor
printf("GetDay Test\n");
printf("Day 1: %d\n", date1.GetDay());
printf("Day 2: %d\n", date2.GetDay());
return 0;
}
#include "mbed.h"
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <string>
using namespace std;
//#pragma warning(disable : 4996)
#include "C12832.h"
C12832 lcd(p5, p7, p6, p8, p11);
const uint8_t STRMAX = 20;
const char EOT = '.';
const char CRLF = '\n';
// ---------------- Serial RS232 Event Class --------------------------
class SerialEvent {
Serial _pc;
void _risingISR();
char _str[STRMAX];
volatile bool _strOkFlag;
int _index;
public:
SerialEvent(PinName tx, PinName rx) : _pc(tx, rx) { // create the Serial on the pin specified to SwEvent
_pc.attach(this, &SerialEvent::pc_recv); // attach DataReceive-function of this SerialEvent instance
_strOkFlag = false;
_index=0;
}
void pc_recv();
void getString(char st[]);
int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated
};
// ---------------- Serial Event Class Methodes --------------------------
void SerialEvent::getString(char st[]) {
for( int i=0; i <= _index; i++)
st[i] = _str[i];
_index=0;
}
void SerialEvent::pc_recv() {
char c;
while(_pc.readable()){
c = _pc.getc();
if((c != CRLF) && (_index < STRMAX)) {
_str[_index++] = c;
}
}
if(( c == EOT)) { // end: . string not empty
if(_index >= 1) {
_strOkFlag = true;
_str[--_index] = 0;
}
}
}
int SerialEvent::checkFlag() {
if( _strOkFlag ) {
_strOkFlag = false;
return 1;
}
return 0;
}
/*
** reverse string in place
*/
void reverse(char *s) {
char *j;
int c;
j = s + strlen(s) - 1;
while(s < j) {
c = *s;
*s++ = *j;
*j-- = c;
}
}
/* itoa: convert n to characters in s */
void itoa(int n, char s[], int z)
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
class Date
{
protected:
uint8_t day, month, year;
public:
Date() :day(1), month(1), year(00) {
lcd.locate(0,0);
lcd.cls();
} // Initialisierungsliste mit konstante Parameterwerten
// Initialisierungsliste über parametrisierten Konstruktor
Date(uint8_t _day, uint8_t _month, uint8_t _year) :day(_day), month(_month), year(_year) {}
uint8_t GetDay(); // Methode
};
uint8_t Date::GetDay()
{
//lcd.cls();
lcd.locate(0,0);
lcd.printf("Day : %d", day);
lcd.locate(0,10);
lcd.printf("Day : %d", day);
lcd.locate(0,20);
lcd.printf("Day : %d", day);
return day;
}
class DateString : public Date
{
string str;
public:
DateString() :Date(20, 3, 18) {}
// DateString():Date(2,3,11) {} // Alternativ mit neuen Werten --> ausprobieren
DateString(uint8_t _day, uint8_t _month, uint8_t _year) : Date(_day, _month, _year) {}
using Date:: GetDay;
char* GetDay(char* str) {
itoa(day, str, 10); // https://fresh2refresh.com/c-programming/c-type-casting/c-itoa-function/
return str;
}
};
int main()
{
Date date1; // Instanziierung mit Standard Konstruktor und
DateString date2(18, 4, 15); // mit parametrisierten Konstruktor
char st[65];
printf("Day 1: %d\n", date1.GetDay()); // Verwenden der Methode GetDay aus Klasse Date
printf("Day 2: %d\n", date2.GetDay());
string str = date2.GetDay(st);
printf("DayString %s\n", str.c_str());
strcpy(st, date2.GetDay(st));
printf("DayChar* %s\n", st);
getchar();
return 0;
}