Various projects using Raspberry Pi

raspilcd.adb 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. with Interfaces;
  2. use Interfaces;
  3. with Interfaces.C;
  4. use Interfaces.C;
  5. with Interfaces.C.extensions;
  6. use Interfaces.C.extensions;
  7. with bcm2835_h;
  8. use bcm2835_h;
  9. package body RaspiLcd is
  10. procedure io_init is
  11. begin
  12. bcm2835_gpio_fsel(pin => LCD_CS, mode => unsigned_char(BCM2835_GPIO_FSEL_OUTP));
  13. bcm2835_gpio_fsel(pin => LCD_RST, mode => unsigned_char(BCM2835_GPIO_FSEL_OUTP));
  14. bcm2835_gpio_fsel(pin => LCD_A0, mode => unsigned_char(BCM2835_GPIO_FSEL_OUTP));
  15. bcm2835_gpio_fsel(pin => LCD_CLK, mode => unsigned_char(BCM2835_GPIO_FSEL_OUTP));
  16. bcm2835_gpio_fsel(pin => LCD_SI, mode => unsigned_char(BCM2835_GPIO_FSEL_OUTP));
  17. end io_init;
  18. procedure lcd_init is
  19. begin
  20. -- reset
  21. bcm2835_gpio_write(pin => LCD_CS, on => unsigned_char(HIGH));
  22. bcm2835_delayMicroseconds(unsigned_long_long(1));
  23. bcm2835_gpio_write(pin => LCD_RST, on => unsigned_char(LOW));
  24. bcm2835_delayMicroseconds(unsigned_long_long(1));
  25. bcm2835_gpio_write(pin => LCD_RST, on => unsigned_char(HIGH));
  26. bcm2835_delayMicroseconds(unsigned_long_long(1));
  27. -- init routine
  28. for index in lcd_init_data'range loop
  29. lcd_transfer_data(value => lcd_init_data(index), si => false);
  30. end loop;
  31. lcd_clear;
  32. end lcd_init;
  33. procedure lcd_ascii57_string (xpos : natural; ypos : natural; data : string) is
  34. begin
  35. for index in 0 .. data'length-1 loop
  36. lcd_ascii57(xpos => xpos + index * 6, ypos => ypos, data => character'val(character'pos(data(index+1))));
  37. end loop;
  38. end lcd_ascii57_string;
  39. procedure lcd_ascii57 (xpos : natural; ypos : natural; data : character) is
  40. begin
  41. lcd_set_page(page => ypos, column => xpos);
  42. -- write one 5x7 char
  43. for index in 0..4 loop
  44. lcd_transfer_data(value => font_5x7(character'pos(data) - 32)(index), si => true);
  45. end loop;
  46. -- one free column between chars
  47. lcd_transfer_data(value => 16#00#, si => true);
  48. end lcd_ascii57;
  49. procedure lcd_picture (xpos : natural; ypos: natural) is
  50. begin
  51. for outdex in 0..7 loop
  52. lcd_set_page(page => ypos + outdex, column => xpos);
  53. for index in (128 * outdex) .. (128 * (outdex + 1) - 1) loop
  54. lcd_transfer_data(value => picture(index), si => true);
  55. end loop;
  56. end loop;
  57. end lcd_picture;
  58. procedure lcd_clear is
  59. begin
  60. bcm2835_gpio_write(pin => LCD_CS, on => unsigned_char(LOW));
  61. for outdex in 0..7 loop
  62. lcd_set_page(page => outdex, column => 0);
  63. for index in 0..128 loop
  64. lcd_transfer_data(value => 16#00#, si => true);
  65. end loop;
  66. end loop;
  67. bcm2835_gpio_write(pin => LCD_CS, on => unsigned_char(HIGH));
  68. end lcd_clear;
  69. procedure lcd_transfer_data (value : byte; si : boolean) is
  70. begin
  71. bcm2835_gpio_write(pin => LCD_CS, on => unsigned_char(LOW));
  72. bcm2835_gpio_write(pin => LCD_CLK, on => unsigned_char(HIGH));
  73. if si then
  74. bcm2835_gpio_write(pin => LCD_A0, on => unsigned_char(HIGH));
  75. else
  76. bcm2835_gpio_write(pin => LCD_A0, on => unsigned_char(LOW));
  77. end if;
  78. lcd_byte(value);
  79. bcm2835_gpio_write(pin => LCD_CS, on => unsigned_char(HIGH));
  80. end lcd_transfer_data;
  81. procedure lcd_set_page (page : natural; column : natural) is
  82. lsb : byte := byte(column + 1) and 16#0f#;
  83. msb : byte := byte(column + 1) and 16#f0#;
  84. page_int : byte := byte(page) or 16#b0#;
  85. begin
  86. msb := Shift_Right(msb, 4);
  87. msb := msb or 16#10#;
  88. lcd_transfer_data(value => page_int, si => false);
  89. lcd_transfer_data(value => msb, si => false);
  90. lcd_transfer_data(value => lsb, si => false);
  91. null;
  92. end lcd_set_page;
  93. procedure lcd_byte (data : byte) is
  94. data_int : byte := data;
  95. begin
  96. for index in 0..7 loop
  97. bcm2835_delayMicroseconds(unsigned_long_long(1));
  98. bcm2835_gpio_write(pin => LCD_CLK, on => unsigned_char(LOW));
  99. if (data_int and 16#80#) = 16#80# then
  100. bcm2835_gpio_write(pin => LCD_SI, on => unsigned_char(HIGH));
  101. else
  102. bcm2835_gpio_write(pin => LCD_SI, on => unsigned_char(LOW));
  103. end if;
  104. data_int := Shift_Left(data_int, 1);
  105. bcm2835_delayMicroseconds(unsigned_long_long(1));
  106. bcm2835_gpio_write(pin => LCD_CLK, on => unsigned_char(HIGH));
  107. end loop;
  108. end lcd_byte;
  109. end RaspiLcd;