Last active
August 29, 2015 14:10
-
-
Save TorstenC/aec0724be4afcd1d7545 to your computer and use it in GitHub Desktop.
C-Code-Generator für die 24h Wordclock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected override Boolean GenCode() { | |
string Version = "v0.4"; | |
// // generated code see https://gist.github.com/TorstenC/c45751a699af50fe9e64 | |
FileItem tables_h = this.Files.AppendFile("tables.h"); | |
// Konstanten (werdem am Ende gesetzt) ======================================= | |
CodeBlockItem HeaderT = tables_h.AddCodeBlock("HeaderT"); | |
CodeLine.Add(HeaderT, "// Arrays for 24h Wordclock, see http://www.mikrocontroller.net/articles/WordClock24h"); | |
CodeLine.Add(HeaderT, "// Von-Neumann-Variant, Data from " + this.ClockData.Name + " CodeGen " + Version); | |
CodeLine.Add(HeaderT, "// Code-Generator see https://gist.github.com/TorstenC/aec0724be4afcd1d7545"); | |
CodeBlockItem ConstantsT = tables_h.AddCodeBlock("Constants"); | |
CodeLine.Add(ConstantsT, ""); | |
// Structs =================================================================== | |
CodeBlockItem Structs = tables_h.AddCodeBlock("Structs"); | |
CodeLine.Add(Structs, ""); | |
CodeLine.Add(Structs, "struct Modes {"); | |
CodeLine.Add(Structs, 1, "int minute_txt;", "we don´t need minute_txt_last"); | |
CodeLine.Add(Structs, "int hour_txt;"); | |
CodeLine.Add(Structs, "const char* description;"); | |
CodeLine.Add(Structs, -1, "};"); | |
CodeLine.Add(Structs, "struct MinuteDisplay {"); | |
CodeLine.Add(Structs, 1, "uint8_t hourOffset;","offset for hour display"); | |
CodeLine.Add(Structs, "uint8_t wordIdx[MaxMinuteWordsPlusOne];"); | |
CodeLine.Add(Structs, -1, "};"); | |
// tbl_modes =================================================================== | |
CodeBlockItem tbl_modes = tables_h.AddCodeBlock("tbl_modes"); | |
CodeLine.Add(tbl_modes, "static const struct Modes tbl_modes[ModesCount] = {"); | |
tbl_modes.indent += 1; | |
this.ModesCount = 0; | |
foreach (ModeItem Mode in this.ClockData.Modes) { | |
CodeLine.Add(tbl_modes, "{ "); | |
tbl_modes.AppendCode(String.Format("{0:00}, ", Mode.minute_ix)); | |
tbl_modes.AppendCode(String.Format("{0:00}, ", Mode.hour_ix)); | |
tbl_modes.AppendCode( "\"" + Mode.Text + "\" },"); | |
this.ModesCount += 1; | |
} | |
tbl_modes.RemoveCode(1); | |
CodeLine.Add(tbl_modes, -1, "};"); | |
// tbl_hours =================================================================== | |
this.HourWordsMax = 0; // Länge des längsten Arrays | |
this.HourModesCount = 0; // Anzahl der Hour-Modes | |
CodeBlockItem tbl_hours = tables_h.AddCodeBlock("tbl_hours"); | |
CodeLine.Add(tbl_hours, ""); | |
CodeLine.Add(tbl_hours, "// tbl_hours, Beispiel: Alles in einer Zeile mit \"AppendCode()\", soll das so? Dann mache ich noch einen Zeilenumbruch."); | |
CodeLine.Add(tbl_hours, "static const struct uint8_t tbl_hours[HourModesCount][HourCount][MaxHourWordsPlusOne]= {"); | |
CodeBlockItem tbl_hours1 = tables_h.AddCodeBlock("tbl_hours1"); // separater Block, damit die Kommentare nicht so weit rechts stehen | |
tbl_hours1.indent += 1; | |
foreach (KeyValuePair<int, Tuple<String, String>> HourMode in this.ClockData.Hours.Modes) { | |
CodeLine.Add(tbl_hours1, "{", "Mode " + HourMode.Key + " " + HourMode.Value.Item1); | |
foreach (HourItem Hour in this.ClockData.Hours) { | |
tbl_hours1.AppendCode("{"); | |
if (Hour.WordsInModes.ContainsKey(HourMode.Key) && Hour.WordsInModes[HourMode.Key].Count > 0) { | |
int LenCount = 0; | |
foreach (int i in Hour.WordsInModes[HourMode.Key]) { | |
tbl_hours1.AppendCode(String.Format("{0:00},", i)); | |
LenCount += 1; | |
} | |
tbl_hours1.RemoveCode(1); // Das letzte Komma war zuviel | |
this.HourWordsMax = Math.Max(this.HourWordsMax, LenCount); | |
} | |
tbl_hours1.AppendCode("},"); | |
} | |
tbl_hours1.RemoveCode(1); // Das letzte Komma war zuviel | |
tbl_hours1.AppendCode("},"); | |
this.HourModesCount += 1; | |
} | |
tbl_hours1.RemoveCode(1); // Das letzte Komma war zuviel | |
CodeLine.Add(tbl_hours1, -1, "};"); | |
// tbl_minutes =================================================================== | |
this.MinuteWordsMax = 0; // Länge des längsten Arrays | |
this.MinuteModesCount = 0; // Anzahl der Minute-Modes | |
CodeBlockItem tbl_minutes = tables_h.AddCodeBlock("tbl_minutes"); | |
CodeLine.Add(tbl_minutes, ""); | |
CodeLine.Add(tbl_minutes, "// tbl_minutes, Beispiel: Jede Minute in einer Extra-Zeile mit \"CodeLine.Add()\", Vorteil: Der CodeGenerator muss keinen Zeilemumbruch berechnen."); | |
CodeLine.Add(tbl_minutes, "static const MinuteDisplay tbl_minutes[MinuteModesCount][MinuteCount]= {"); | |
CodeBlockItem tbl_minutes1 = tables_h.AddCodeBlock("tbl_minutes1"); // separater Block, damit die Kommentare nicht so weit rechts stehen | |
tbl_minutes1.indent += 1; | |
foreach (KeyValuePair<int, Tuple<String, String>> MinuteMode in this.ClockData.Minutes.Modes) { | |
CodeLine.Add(tbl_minutes1, "{", "Mode " + MinuteMode.Key + " " + MinuteMode.Value.Item1); | |
tbl_minutes1.indent += 1; | |
foreach (MinuteItem Minute in this.ClockData.Minutes) { | |
CodeLine.Add(tbl_minutes1, "{", String.Format("{0:00}. Minute", Minute.Minute)); | |
if (Minute.WordsInModes.ContainsKey(MinuteMode.Key) && Minute.WordsInModes[MinuteMode.Key].Words.Count > 0) { | |
tbl_minutes1.AppendCode(Minute.WordsInModes[MinuteMode.Key].HourInc ? "1,{" : "0,{"); | |
int LenCount = 0; | |
foreach (int i in Minute.WordsInModes[MinuteMode.Key].Words) { | |
tbl_minutes1.AppendCode(String.Format("{0:00},", i)); | |
LenCount += 1; | |
} | |
tbl_minutes1.RemoveCode(1); // Das letzte Komma war zuviel | |
tbl_minutes1.AppendCode("}"); | |
this.MinuteWordsMax = Math.Max(this.MinuteWordsMax, LenCount); | |
} | |
tbl_minutes1.AppendCode("},"); | |
} | |
tbl_minutes1.RemoveCode(1); // Das letzte Komma war zuviel | |
CodeLine.Add(tbl_minutes1, -1, "},"); | |
this.MinuteModesCount += 1; | |
} | |
CodeLine.Add(tbl_minutes1, -1, "};"); | |
CodeLine.Add(ConstantsT, "#define ModesCount " + (this.ModesCount + 1), "count of different display modes"); | |
CodeLine.Add(ConstantsT, "#define MaxHourWordsPlusOne " + (this.HourWordsMax + 1), "how many words for hour display (one plus for end token)"); | |
CodeLine.Add(ConstantsT, "#define HourCount 25", "24 plus one to distinguish between before and after full hour"); | |
CodeLine.Add(ConstantsT, "#define MaxMinuteWordsPlusOne " + (this.MinuteWordsMax + 1), "how many words for minute display (one plus for end token)"); | |
CodeLine.Add(ConstantsT, "#define MinuteCount 60"); | |
CodeLine.Add(ConstantsT, "#define HourModesCount " + (this.HourModesCount + 1), "count of different display modes for hours"); | |
CodeLine.Add(ConstantsT, "#define MinuteModesCount " + (this.MinuteModesCount + 1), "count of different display modes for minutes"); | |
// diyplay.h =================================================================== | |
FileItem display_h = this.Files.AppendFile("display.h"); | |
CodeBlockItem HeaderD = display_h.AddCodeBlock("HeaderD"); | |
CodeLine.Add(HeaderD, "// Mock-up arrays for 24h Wordclock, see http://www.mikrocontroller.net/articles/WordClock24h"); | |
CodeLine.Add(HeaderD, "// Von-Neumann-Variant, Data from " + this.ClockData.Name + ", CodeGen " + Version); | |
CodeLine.Add(HeaderD, "// Code-Generator see https://gist.github.com/TorstenC/aec0724be4afcd1d7545"); | |
CodeBlockItem Constants2 = display_h.AddCodeBlock("Constants"); | |
CodeLine.Add(Constants2, ""); | |
CodeLine.Add(Constants2, "#define DisplayX 18"); | |
CodeLine.Add(Constants2, "#define DisplayY 16"); | |
CodeLine.Add(Constants2, ""); | |
CodeLine.Add(Constants2, "struct WordIllu {"); | |
CodeLine.Add(Constants2, 1, "word row;"); | |
CodeLine.Add(Constants2, "word col;"); | |
CodeLine.Add(Constants2, "word len;"); | |
CodeLine.Add(Constants2, -1, "};"); | |
CodeBlockItem display = display_h.AddCodeBlock("display"); | |
CodeLine.Add(display, "static const wchar* display[1][DisplayY]= {{", "dummy-dimension for variant with capital ß"); | |
display.indent += 1; | |
for (int row = 0; row < this.ClockData.Glyphs.rows; row += 1) { | |
CodeLine.Add(display, "\""); | |
for (int col = 0; col < this.ClockData.Glyphs.columns; col += 1) { | |
display.AppendCode(this.ClockData.Glyphs.Glyphs[col, row] + ""); | |
} | |
display.AppendCode("\","); | |
} | |
display.RemoveCode(1); // Das letzte Komma war zuviel | |
CodeLine.Add(display, -1, "}};"); | |
CodeLine.Add(display, ""); | |
CodeLine.Add(display, "enum WordPos {"); | |
CodeLine.Add(display, 1, "WP_DUMMY,", "to have enum int value same as herberts index"); | |
foreach (WordItem Word in this.ClockData.Words) { | |
CodeLine.Add(display, "WP_" + Word.ID + ",", Word.Text); | |
} | |
CodeLine.Add(display, "WpCount","number of words + 1"); | |
CodeLine.Add(display, -1, "};"); | |
CodeLine.Add(display, "static const WordIllu illumination[1][WpCount]= {{", "dummy-dimension for variant with capital ß"); | |
display.indent += 1; | |
foreach (WordItem Word in this.ClockData.Words) { | |
CodeLine.Add(display, "{" + Word.Row + ",", Word.ID + " = " + Word.Text); | |
display.AppendCode(Word.Column + ","); | |
display.AppendCode(Word.Length + "},"); | |
} | |
CodeLine.Add(display, -1, "}};"); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment