Display.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Display.h
  3. *
  4. * Created on: 03.12.2020
  5. * Author: willy
  6. */
  7. #ifndef DISPLAY_H_
  8. #define DISPLAY_H_
  9. // Definition of Screen resolution in pixel
  10. #define COLS 12
  11. #define ROWS 8
  12. /*
  13. * virtual screen,
  14. * The screen is regarded as a Matrix with 8 rows and 12 collums.
  15. * Each field inside the matrix represent one pixel on the screen
  16. * consists of a one dimensional array with integer values.
  17. * Each row on the screen is represented by one field of the array.
  18. * The Integer filling the field convertet to binary state wich pixels are active
  19. * Exp.:
  20. * __ __
  21. * | 1,0,0,0,0,0,0,0,0,0,0,0 |
  22. * | 1,0,0,0,0,0,0,0,0,0,0,0 |
  23. * | 1,0,0,0,0,0,0,0,0,0,0,0 |
  24. * [2048,2048,2048,64,0,1,1,1] ==> | 0,0,0,0,0,1,0,0,0,0,0,0 |
  25. * | 0,0,0,0,0,0,0,0,0,0,0,0 |
  26. * | 0,0,0,0,0,0,0,0,0,0,0,1 |
  27. * | 0,0,0,0,0,0,0,0,0,0,0,1 |
  28. * |__0,0,0,0,0,0,0,0,0,0,0,1__|
  29. *
  30. * 1-> pixel light up
  31. * 0-> pixel is dark
  32. */
  33. int screen[ROWS];
  34. /*
  35. * helper function to print integer values as binary.
  36. * only used for debugging to display the screen on the console
  37. * found at: https://www.geeksforgeeks.org/binary-representation-of-a-given-number/
  38. */
  39. void bin(unsigned n)
  40. {
  41. if (n > 1)
  42. bin(n >> 1);
  43. printf("%d", n & 1);
  44. }
  45. /*
  46. * actual drawing function. It will output the screen variabe on the designated registers.
  47. * every Row of the screen is written to the register one after another in the following format:
  48. *
  49. * 0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0
  50. * |______||_____________________|
  51. * Row-ID Row-Data
  52. *
  53. * Row-ID: state which row the data is from. Values from 1 to 8. 0 is an invalid value
  54. * Row-Data: a binary value for each pixel in the row
  55. */
  56. void draw() {
  57. int row,i,j;
  58. for (i=0; i<ROWS; i++) {
  59. row = screen[i] | ((i+1)<<12); //send format: (4bits row indicator, 12bits data)
  60. IOWR_ALTERA_AVALON_PIO_DATA(PIO_MATRIX_BASE, row);
  61. }
  62. }
  63. /*
  64. * function to draw the virtual screen on a console. Only used for debugging.
  65. */
  66. void draw_console() {
  67. int i;
  68. for (i=0; i<ROWS; i++) {
  69. bin(screen[i]);
  70. printf("\n");
  71. }
  72. }
  73. /*
  74. * function to set the given pixel 1.
  75. * x and y are the coordinates, x is collumn number, y row number.
  76. */
  77. void set_pixel(int x, int y) {
  78. //constrain x and y
  79. x = MAX(MIN(x, 0), COLS - 1);
  80. y = MAX(MIN(y, 0), ROWS - 1);
  81. screen[y] = screen[y] & ~(1<<x) | (1<<x);
  82. }
  83. /*
  84. * reset the screen, all pixels are set to 0.
  85. */
  86. void reset_screen() {
  87. int i;
  88. for (i=0; i<ROWS; i++) {
  89. screen[i] = 0;
  90. }
  91. }
  92. #endif /* DISPLAY_H_ */