Advertisement
duke

apo semestralka v1.2

May 24th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.51 KB | None | 0 0
  1. #include "kbd_hw.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/mman.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <dirent.h>
  10. #include <unistd.h>
  11. #include <string.h>
  12. #include <dirent.h>
  13. #include <errno.h>
  14. #include "chmod_lcd.h"
  15.  
  16.  
  17. // metody
  18. int main();
  19. void nastaveni();
  20. void napajeni();
  21. void map();
  22. void piskani(int i);
  23. void zapis(uint32_t offset, uint8_t data);
  24. void nastaveniLCD();
  25. void nsleep(long nsec);
  26. void controlLcd(int data);
  27. void initKey();
  28. void vypinani();
  29. void zapiString(char* data);
  30. void writeCharToLcd(char data);
  31. void radek();
  32. void vycistit();
  33. int tlacitko(uint8_t data);
  34. int readFromDevice(uint32_t offset);
  35. void dirList(int number, char *directory);
  36.  
  37.  
  38.  
  39.  
  40. //******************************
  41. // promene
  42. uint8_t data;
  43. void * pointer;
  44.  
  45. //hold array of files and dirs in give dir
  46. char* adresy[100];
  47.  
  48. //counter of elements found in given dir
  49. int counter = 0;
  50. //iterator of elements in given dir
  51. int iterator = 0;
  52. //initial address for listning of elements in given dir -- do not modify!
  53. char* intialAddress;
  54.  
  55. //adjust your initial address herevoid listDir()
  56. char addressFull[300] = "/home/student/Desktop/";
  57.  
  58. //temp address
  59. char addressOld[300];
  60.  
  61. //temp variables for building address while going trough dirs
  62. char temp[300];
  63. char temp2[300];
  64.  
  65. int i = 0;
  66. int checkWay;
  67.  
  68. char *filename;
  69.  
  70. DIR *dip, *dirTest;
  71. struct dirent *dit;
  72. int main(){
  73.  
  74. nastaveni();
  75. piskani(1);
  76.  
  77. vycistit();
  78. zapiString("ahoj");
  79. usleep(2000000);
  80. radek();
  81. vycistit();
  82. while(1){
  83.  
  84. int prvniSloupec = tlacitko(0x03);
  85. int druhySloupec = tlacitko(0x05);
  86. int tretiSloupec = tlacitko(0x06);
  87.  
  88. if(prvniSloupec == 2){
  89. zapiString("Zavrit");
  90. piskani(1);
  91. usleep(100000);
  92. piskani(0);
  93. usleep(600000);
  94. vycistit();
  95. }
  96.  
  97. if(druhySloupec == 1){
  98. counter++;
  99. dirList(counter, "./");
  100. //zapiString("Nahoru");
  101. piskani(1);
  102. usleep(100000);
  103. piskani(0);
  104. usleep(600000);
  105. vycistit();
  106. }
  107. if(druhySloupec == 2){
  108. zapiString("Stop/play");
  109. piskani(1);
  110. usleep(100000);
  111. piskani(0);
  112. usleep(600000);
  113. vycistit();
  114.  
  115. }
  116. if(druhySloupec == 3){
  117. counter--;
  118. dirList(counter, "./");
  119. //zapiString("Dolu");
  120. piskani(1);
  121. usleep(100000);
  122. piskani(0);
  123. usleep(600000);
  124. vycistit();
  125. }
  126. if(tretiSloupec== 2){
  127. zapiString("Vpred");
  128. piskani(1);
  129. usleep(100000);
  130. piskani(0);
  131. usleep(600000);
  132. vycistit();
  133. }
  134.  
  135. if(tretiSloupec== 4){
  136.          printf("konec\n");
  137.          vypinani();
  138.          usleep(200000);
  139. }
  140. }
  141.  
  142. usleep(600000);
  143.  
  144. vypinani();
  145.  
  146. }
  147.  
  148.  
  149. void nastaveni(){
  150.  
  151. map();
  152. napajeni();
  153. piskani(0);
  154. nastaveniLCD();
  155.  
  156. }
  157.  
  158. void map(){
  159.  
  160. char *mem_dev = "/dev/mem";
  161.     int memLength = 0x10000;
  162.  
  163.     uintptr_t cardAddress = 0xfe8f0000;
  164.  
  165. int fd = open(mem_dev, O_RDWR | O_SYNC);
  166.     if (fd < 0) {
  167.     printf("ERROR while opening address of device");
  168.     }
  169.  
  170.  
  171. //map device to main address space
  172.     pointer = mmap(NULL, memLength, PROT_READ | PROT_WRITE, MAP_SHARED, fd, cardAddress);
  173.  
  174.  
  175.  
  176. }
  177.  
  178. void napajeni(){
  179. zapis(0x8040, 0xFF);
  180. }
  181.  
  182. void vypinani() {
  183. vycistit();
  184. zapiString("Koncim");
  185.     usleep(5000000);
  186.     zapis(0x8040, 0x00);
  187. exit(0);
  188. }
  189.  
  190.  
  191. void zapis(uint32_t offset, uint8_t data) {
  192.     void * temppointer = pointer;
  193.     temppointer += offset;
  194.     *(volatile uint32_t *)temppointer = data;
  195. }
  196.  
  197. void piskani(int i) {
  198.  
  199.     //chce nabeznou hranu, proto z Fka padnout na E
  200.     if (i == 0) {
  201.     zapis(0x8060, 0x00);
  202.     } else {
  203.     zapis(0x8060, 0x80);
  204.     }
  205.  
  206.     zapis(0x8040, 0xF7);
  207.     usleep(10);
  208.     zapis(0x8040, 0xE7);
  209.     usleep(3000);
  210. }
  211.  
  212. void nastaveniLCD() {
  213.     nsleep(40000000);
  214.     controlLcd(0x3B);
  215.     nsleep(39000);
  216.     controlLcd(0x0F);
  217.     nsleep(39000);
  218.     controlLcd(0x01);
  219.     nsleep(1530000);
  220.     controlLcd(0x06);
  221.     nsleep(39000);
  222. }
  223.  
  224. void nsleep(long nsec) {
  225.     struct timespec req = {0}; // = malloc(sizeof(timespec));
  226.     req.tv_nsec = nsec;
  227.     nanosleep(&req, NULL);
  228. }
  229.  
  230. /*
  231. * Writes control instructions to LCD.
  232. */
  233. void controlLcd(int data) {
  234.     zapis(0x8060, data);
  235.     zapis(0x8040, 0xE4);
  236.     nsleep(1000);
  237.     zapis(0x8040, 0xF4);
  238. }
  239.  
  240.  
  241.  
  242.  
  243. void zapiString(char* string) {
  244.     vycistit();
  245.     int i = 0;
  246.     while (1) {
  247.     if (string[i] == 0) break;
  248.     writeCharToLcd(string[i]);
  249.     i++;
  250.     }
  251. }
  252.  
  253. /*
  254. * Writes char to LCD.
  255. */
  256. void writeCharToLcd(char data) {
  257.     zapis(0x8060, data); // 7 6 5 4 3 2 1 0
  258.     zapis(0x8040, 0xE6); // 1 1 1 0 0 1 1 0
  259.     nsleep(100);
  260.     zapis(0x8040, 0xF6); // 1 1 1 1 0 1 1 0
  261. }
  262.  
  263. void radek(){
  264. controlLcd(0xC0);
  265.     usleep(46);
  266. }
  267.  
  268. void vycistit(){
  269. nastaveniLCD();
  270. }
  271.  
  272. void initKey() {
  273.     zapis(0x8040, 0xE7);
  274.     usleep(100000);
  275.     zapis(0x8040, 0xF7);
  276.     usleep(100000);
  277.     zapis(0x8040, 0xE8);
  278.  
  279.     usleep(100000);
  280.  
  281. }
  282.  
  283. int tlacitko(uint8_t data) {
  284.  
  285.     int cudliky[4] = {0, 0, 0, 0};
  286.  
  287.     nsleep(200000);
  288.  
  289.     int i;
  290.     for (i = 0; i < 10; i++) {
  291.     zapis(0x8040, 0xe7); // zapise do klavesnice a sirenky, to dolu
  292.     zapis(0x8060, data); // jednotka na sloupce
  293.     zapis(0x8040, 0xf7); // potvrzeni vyberu periferie
  294.     zapis(0x8040, 0xfc);
  295.     zapis(0x8040, 0xf8);
  296.     zapis(0x8040, 0xe8);
  297.  
  298.     uint8_t j = readFromDevice(0x8080);
  299.  
  300.     // cudliky
  301.     if (j == 0xfe) {
  302.     cudliky[0] = cudliky[0] + 1;
  303.     }
  304.     if (j == 0xfd) {
  305.     cudliky[1] = cudliky[1] + 1;
  306.     }
  307.     if (j == 0xfb) {
  308.     cudliky[2] = cudliky[2] + 1;
  309.     }
  310.     if (j == 0xf7) {
  311.     cudliky[3] = cudliky[3] + 1;
  312.     }
  313.     if (j == 0xef) {
  314.     // tady muzu nakej ten postrani cudlik
  315.     }
  316.  
  317.  
  318.     usleep(50);
  319.     }
  320.  
  321.     // ktery prvek nejvickrat
  322.     int u;
  323.     int nejvyzsi = 0;
  324.     for (u = 0; u < 4; u++) {
  325.     if (cudliky[u] > nejvyzsi) {
  326.     nejvyzsi = cudliky[u];
  327.     }
  328.     }
  329.  
  330.     if (nejvyzsi == 0) {
  331.     return -1;
  332.     }
  333.  
  334.     int h;
  335.     for (h = 0; h < 4; h++) {
  336.     if (cudliky[h] == nejvyzsi) {
  337.     if (h == 0) {
  338.     return h + 1;
  339.     }
  340.     if (h == 1) {
  341.     return h + 1;
  342.     }
  343.     if (h == 2) {
  344.     return h + 1;
  345.     }
  346.     if (h == 3) {
  347.     return h + 1;
  348.     }
  349.     }
  350.     }
  351. }
  352.  
  353. int readFromDevice(uint32_t offset) {
  354.     void * temppointer = pointer;
  355.     temppointer += offset;
  356.     data = *((char *) temppointer);
  357.     return data;
  358. }
  359.  
  360. void dirList(int number, char *directory) {
  361.     DIR *dp;
  362.     struct dirent *ep;
  363.     int i = 0;
  364.  
  365.     dp = opendir(directory);
  366.     if (dp != NULL) {
  367.         while (ep = readdir(dp)){
  368.             if(i == number){
  369.         zapiString(ep->d_name);            
  370.             }
  371.             i++;
  372.         }
  373.         (void) closedir(dp);
  374.     } else
  375.         zapiString("Cant open dir");
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement