buildcpp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. OBSOLETE,
  3. replaced by codebuild
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #define CSVLINELEN (1024*16)
  9. char csvline[CSVLINELEN];
  10. char elements[10][CSVLINELEN];
  11. #define DEVICE elements[0]
  12. #define PINSET elements[1]
  13. #define CONSTRUCTOR elements[2]
  14. /*
  15. copy file from source_file_name to dest_file_name
  16. */
  17. int file_copy(const char *source_file_name, const char *dest_file_name)
  18. {
  19. int ch;
  20. FILE *source_fp;
  21. FILE *dest_fp;
  22. source_fp = fopen(source_file_name, "r");
  23. dest_fp = fopen(dest_file_name, "w");
  24. if ( source_fp == NULL || dest_fp == NULL )
  25. return 0;
  26. while( ( ch = fgetc(source_fp) ) != EOF )
  27. fputc(ch, dest_fp);
  28. fclose(source_fp);
  29. fclose(dest_fp);
  30. return 1;
  31. }
  32. /*
  33. Insert file "insertname" between lines "start_line" and "end_line" of file "filename"
  34. */
  35. int insert_into_file(const char *filename, const char *insertname, const char *start_line, const char *end_line)
  36. {
  37. int ch;
  38. static char line[1024*4];
  39. const char *tmpname = "tmp.h";
  40. FILE *source_fp;
  41. FILE *dest_fp;
  42. FILE *insert_fp;
  43. if ( file_copy(filename, tmpname) == 0 )
  44. return 0;
  45. source_fp = fopen(tmpname, "r");
  46. dest_fp = fopen(filename, "w");
  47. insert_fp = fopen(insertname, "r");
  48. if ( source_fp == NULL || dest_fp == NULL || insert_fp == NULL )
  49. return 0;
  50. for(;;)
  51. {
  52. if ( fgets(line, 1024*4, source_fp) == NULL )
  53. break;
  54. if ( strncmp(line, start_line, strlen(start_line)) == 0 )
  55. {
  56. fputs(line, dest_fp);
  57. while( ( ch = fgetc(insert_fp) ) != EOF )
  58. fputc(ch, dest_fp);
  59. fputs("\n", dest_fp);
  60. for(;;)
  61. {
  62. if ( fgets(line, 1024*4, source_fp) == NULL )
  63. break;
  64. if ( strncmp(line, end_line, strlen(end_line)) == 0 )
  65. {
  66. fputs(line, dest_fp);
  67. break;
  68. }
  69. }
  70. }
  71. else
  72. {
  73. fputs(line, dest_fp);
  74. }
  75. }
  76. fclose(insert_fp);
  77. fclose(source_fp);
  78. fclose(dest_fp);
  79. unlink(tmpname);
  80. return 1;
  81. }
  82. /*
  83. class U8X8_SSD1306_128X64_4W_SW_SPI : public U8X8
  84. {
  85. public:
  86. U8X8_SSD1306_128X64_4W_SW_SPI(uint8_t clock, uint8_t data, uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE) : U8X8()
  87. {
  88. u8x8_Setup_4Wire_SW_SPI(&u8x8, u8x8_d_ssd1306_128x64_noname, clock, data, cs, dc, reset);
  89. }
  90. };
  91. */
  92. char *get_args(const char *ps)
  93. {
  94. static char s[1024];
  95. if ( strcmp(ps, "4WSWSPI") == 0 )
  96. sprintf(s, "clock, data, cs, dc, reset");
  97. else if ( strcmp(ps, "4WHWSPI") == 0 )
  98. sprintf(s, "cs, dc, reset");
  99. else if ( strcmp(ps, "3WSWSPI") == 0 )
  100. sprintf(s, "clock, data, cs, reset");
  101. else if ( strcmp(ps, "SSDSWI2C") == 0 )
  102. sprintf(s, "clock, data, reset");
  103. else if ( strcmp(ps, "6800") == 0 )
  104. sprintf(s, "d0, d1, d2, d3, d4, d5, d6, d7, enable, cs, dc, reset");
  105. else if ( strcmp(ps, "8080") == 0 )
  106. sprintf(s, "d0, d1, d2, d3, d4, d5, d6, d7, enable, cs, dc, reset");
  107. else
  108. sprintf(s, "... unknown pinset ...");
  109. return s;
  110. }
  111. void write_u8x8_md(FILE *fp)
  112. {
  113. fprintf(fp, "| U8X8_%s(", CONSTRUCTOR);
  114. fprintf(fp, "%s) |\n", get_args(PINSET));
  115. }
  116. void write_class(const char *prefix, FILE *fp)
  117. {
  118. fprintf(fp, "class %s_%s : public %s {\n", prefix, CONSTRUCTOR, prefix);
  119. fprintf(fp, " public: %s_%s(", prefix, CONSTRUCTOR);
  120. if ( strcmp(PINSET, "4WSWSPI") == 0 )
  121. fprintf(fp, "uint8_t clock, uint8_t data, uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE");
  122. else if ( strcmp(PINSET, "4WHWSPI") == 0 )
  123. fprintf(fp, "uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE");
  124. else if ( strcmp(PINSET, "3WSWSPI") == 0 )
  125. fprintf(fp, "uint8_t clock, uint8_t data, uint8_t cs, uint8_t reset = U8X8_PIN_NONE");
  126. else if ( strcmp(PINSET, "SSDSWI2C") == 0 )
  127. fprintf(fp, "uint8_t clock, uint8_t data, uint8_t reset = U8X8_PIN_NONE");
  128. else if ( strcmp(PINSET, "6800") == 0 )
  129. fprintf(fp, "uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t enable, uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE");
  130. else if ( strcmp(PINSET, "8080") == 0 )
  131. fprintf(fp, "uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7, uint8_t enable, uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE");
  132. else
  133. fprintf(fp, "... unknown pinset ...");
  134. fprintf(fp, ") : %s() {\n", prefix);
  135. fprintf(fp, " ");
  136. if ( strcmp(PINSET, "4WSWSPI") == 0 )
  137. fprintf(fp, "u8x8_Setup_4Wire_SW_SPI");
  138. else if ( strcmp(PINSET, "4WHWSPI") == 0 )
  139. fprintf(fp, "u8x8_Setup_4Wire_HW_SPI");
  140. else if ( strcmp(PINSET, "3WSWSPI") == 0 )
  141. fprintf(fp, "u8x8_Setup_3Wire_SW_SPI");
  142. else if ( strcmp(PINSET, "SSDSWI2C") == 0 )
  143. fprintf(fp, "u8x8_Setup_SSD13xx_SW_I2C");
  144. else if ( strcmp(PINSET, "6800") == 0 )
  145. fprintf(fp, "u8x8_Setup_8Bit_6800");
  146. else if ( strcmp(PINSET, "8080") == 0 )
  147. fprintf(fp, "u8x8_Setup_8Bit_8080");
  148. else
  149. fprintf(fp, "... unknown pinset ...");
  150. fprintf(fp, "(getU8x8(), %s", DEVICE);
  151. fprintf(fp, ", %s", get_args(PINSET));
  152. /*
  153. if ( strcmp(PINSET, "4WSWSPI") == 0 )
  154. fprintf(fp, ", clock, data, cs, dc, reset");
  155. else if ( strcmp(PINSET, "4WHWSPI") == 0 )
  156. fprintf(fp, ", cs, dc, reset");
  157. else if ( strcmp(PINSET, "3WSWSPI") == 0 )
  158. fprintf(fp, ", clock, data, cs, reset");
  159. else if ( strcmp(PINSET, "SSDSWI2C") == 0 )
  160. fprintf(fp, ", clock, data, reset");
  161. else if ( strcmp(PINSET, "6800") == 0 )
  162. fprintf(fp, ", d0, d1, d2, d3, d4, d5, d6, d7, enable, cs, dc, reset");
  163. else if ( strcmp(PINSET, "8080") == 0 )
  164. fprintf(fp, ", d0, d1, d2, d3, d4, d5, d6, d7, enable, cs, dc, reset");
  165. else
  166. fprintf(fp, "... unknown pinset ...");
  167. */
  168. fprintf(fp, ");\n");
  169. fprintf(fp, " }\n");
  170. fprintf(fp, "};\n");
  171. }
  172. void readcsv_createu8x8line(char *s)
  173. {
  174. char *t;
  175. int i = 0;
  176. t = strtok(s, ",\n\r");
  177. while( t != NULL )
  178. {
  179. while( *t == ' ' || *t == '\t' )
  180. t++;
  181. //printf("%s\n", t);
  182. strcpy(elements[i], t);
  183. i++;
  184. if ( i >= 10 )
  185. break;
  186. t = strtok(NULL, ",\n\r");
  187. }
  188. }
  189. void readcsv_createu8x8md(const char *name, const char *mdname)
  190. {
  191. FILE *fp;
  192. FILE *md_fp;
  193. char *s;
  194. fp = fopen(name, "r");
  195. md_fp = fopen(mdname, "w");
  196. if ( md_fp != NULL )
  197. {
  198. for(;;)
  199. {
  200. s = fgets(csvline, CSVLINELEN, fp);
  201. if ( s == NULL )
  202. break;
  203. if ( s[0] == '\0' )
  204. continue;
  205. if ( s[0] == '\r' )
  206. continue;
  207. if ( s[0] == '\n' )
  208. continue;
  209. if ( s[0] == '#' )
  210. continue;
  211. readcsv_createu8x8line(s);
  212. write_u8x8_md(md_fp);
  213. }
  214. fclose(fp);
  215. fclose(md_fp);
  216. }
  217. }
  218. void readcsv_create(const char *prefix, const char *name, const char *ctorname)
  219. {
  220. FILE *fp;
  221. FILE *ctor_fp;
  222. char *s;
  223. fp = fopen(name, "r");
  224. ctor_fp = fopen(ctorname, "w");
  225. if ( fp != NULL )
  226. {
  227. for(;;)
  228. {
  229. s = fgets(csvline, CSVLINELEN, fp);
  230. if ( s == NULL )
  231. break;
  232. if ( s[0] == '\0' )
  233. continue;
  234. if ( s[0] == '\r' )
  235. continue;
  236. if ( s[0] == '\n' )
  237. continue;
  238. if ( s[0] == '#' )
  239. continue;
  240. readcsv_createu8x8line(s);
  241. write_class(prefix, ctor_fp);
  242. }
  243. fclose(fp);
  244. fclose(ctor_fp);
  245. }
  246. }
  247. int main(void)
  248. {
  249. readcsv_create("U8X8", "display.csv", "ctor.h");
  250. //insert_into_file("../../cppsrc/U8x8lib.h", "ctor.h", "// constructor list start", "// constructor list end");
  251. readcsv_create("U8G2", "display.csv", "ctor.h");
  252. readcsv_createu8x8md("display.csv", "md");
  253. return 0;
  254. }