Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "kbd_hw.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <string.h>
- #include <dirent.h>
- #include <errno.h>
- #include "chmod_lcd.h"
- // metody
- int main();
- void nastaveni();
- void napajeni();
- void map();
- void piskani(int i);
- void zapis(uint32_t offset, uint8_t data);
- void nastaveniLCD();
- void nsleep(long nsec);
- void controlLcd(int data);
- void initKey();
- void vypinani();
- void zapiString(char* data);
- void writeCharToLcd(char data);
- void radek();
- void vycistit();
- int tlacitko(uint8_t data);
- int readFromDevice(uint32_t offset);
- void dirList(int number, char *directory);
- //******************************
- // promene
- uint8_t data;
- void * pointer;
- //hold array of files and dirs in give dir
- char* adresy[100];
- //counter of elements found in given dir
- int counter = 0;
- //iterator of elements in given dir
- int iterator = 0;
- //initial address for listning of elements in given dir -- do not modify!
- char* intialAddress;
- //adjust your initial address herevoid listDir()
- char addressFull[300] = "/home/student/Desktop/";
- //temp address
- char addressOld[300];
- //temp variables for building address while going trough dirs
- char temp[300];
- char temp2[300];
- int i = 0;
- int checkWay;
- char *filename;
- DIR *dip, *dirTest;
- struct dirent *dit;
- int main(){
- nastaveni();
- piskani(1);
- vycistit();
- zapiString("ahoj");
- usleep(2000000);
- radek();
- vycistit();
- while(1){
- int prvniSloupec = tlacitko(0x03);
- int druhySloupec = tlacitko(0x05);
- int tretiSloupec = tlacitko(0x06);
- if(prvniSloupec == 2){
- zapiString("Zavrit");
- piskani(1);
- usleep(100000);
- piskani(0);
- usleep(600000);
- vycistit();
- }
- if(druhySloupec == 1){
- counter++;
- dirList(counter, "./");
- //zapiString("Nahoru");
- piskani(1);
- usleep(100000);
- piskani(0);
- usleep(600000);
- vycistit();
- }
- if(druhySloupec == 2){
- zapiString("Stop/play");
- piskani(1);
- usleep(100000);
- piskani(0);
- usleep(600000);
- vycistit();
- }
- if(druhySloupec == 3){
- counter--;
- dirList(counter, "./");
- //zapiString("Dolu");
- piskani(1);
- usleep(100000);
- piskani(0);
- usleep(600000);
- vycistit();
- }
- if(tretiSloupec== 2){
- zapiString("Vpred");
- piskani(1);
- usleep(100000);
- piskani(0);
- usleep(600000);
- vycistit();
- }
- if(tretiSloupec== 4){
- printf("konec\n");
- vypinani();
- usleep(200000);
- }
- }
- usleep(600000);
- vypinani();
- }
- void nastaveni(){
- map();
- napajeni();
- piskani(0);
- nastaveniLCD();
- }
- void map(){
- char *mem_dev = "/dev/mem";
- int memLength = 0x10000;
- uintptr_t cardAddress = 0xfe8f0000;
- int fd = open(mem_dev, O_RDWR | O_SYNC);
- if (fd < 0) {
- printf("ERROR while opening address of device");
- }
- //map device to main address space
- pointer = mmap(NULL, memLength, PROT_READ | PROT_WRITE, MAP_SHARED, fd, cardAddress);
- }
- void napajeni(){
- zapis(0x8040, 0xFF);
- }
- void vypinani() {
- vycistit();
- zapiString("Koncim");
- usleep(5000000);
- zapis(0x8040, 0x00);
- exit(0);
- }
- void zapis(uint32_t offset, uint8_t data) {
- void * temppointer = pointer;
- temppointer += offset;
- *(volatile uint32_t *)temppointer = data;
- }
- void piskani(int i) {
- //chce nabeznou hranu, proto z Fka padnout na E
- if (i == 0) {
- zapis(0x8060, 0x00);
- } else {
- zapis(0x8060, 0x80);
- }
- zapis(0x8040, 0xF7);
- usleep(10);
- zapis(0x8040, 0xE7);
- usleep(3000);
- }
- void nastaveniLCD() {
- nsleep(40000000);
- controlLcd(0x3B);
- nsleep(39000);
- controlLcd(0x0F);
- nsleep(39000);
- controlLcd(0x01);
- nsleep(1530000);
- controlLcd(0x06);
- nsleep(39000);
- }
- void nsleep(long nsec) {
- struct timespec req = {0}; // = malloc(sizeof(timespec));
- req.tv_nsec = nsec;
- nanosleep(&req, NULL);
- }
- /*
- * Writes control instructions to LCD.
- */
- void controlLcd(int data) {
- zapis(0x8060, data);
- zapis(0x8040, 0xE4);
- nsleep(1000);
- zapis(0x8040, 0xF4);
- }
- void zapiString(char* string) {
- vycistit();
- int i = 0;
- while (1) {
- if (string[i] == 0) break;
- writeCharToLcd(string[i]);
- i++;
- }
- }
- /*
- * Writes char to LCD.
- */
- void writeCharToLcd(char data) {
- zapis(0x8060, data); // 7 6 5 4 3 2 1 0
- zapis(0x8040, 0xE6); // 1 1 1 0 0 1 1 0
- nsleep(100);
- zapis(0x8040, 0xF6); // 1 1 1 1 0 1 1 0
- }
- void radek(){
- controlLcd(0xC0);
- usleep(46);
- }
- void vycistit(){
- nastaveniLCD();
- }
- void initKey() {
- zapis(0x8040, 0xE7);
- usleep(100000);
- zapis(0x8040, 0xF7);
- usleep(100000);
- zapis(0x8040, 0xE8);
- usleep(100000);
- }
- int tlacitko(uint8_t data) {
- int cudliky[4] = {0, 0, 0, 0};
- nsleep(200000);
- int i;
- for (i = 0; i < 10; i++) {
- zapis(0x8040, 0xe7); // zapise do klavesnice a sirenky, to dolu
- zapis(0x8060, data); // jednotka na sloupce
- zapis(0x8040, 0xf7); // potvrzeni vyberu periferie
- zapis(0x8040, 0xfc);
- zapis(0x8040, 0xf8);
- zapis(0x8040, 0xe8);
- uint8_t j = readFromDevice(0x8080);
- // cudliky
- if (j == 0xfe) {
- cudliky[0] = cudliky[0] + 1;
- }
- if (j == 0xfd) {
- cudliky[1] = cudliky[1] + 1;
- }
- if (j == 0xfb) {
- cudliky[2] = cudliky[2] + 1;
- }
- if (j == 0xf7) {
- cudliky[3] = cudliky[3] + 1;
- }
- if (j == 0xef) {
- // tady muzu nakej ten postrani cudlik
- }
- usleep(50);
- }
- // ktery prvek nejvickrat
- int u;
- int nejvyzsi = 0;
- for (u = 0; u < 4; u++) {
- if (cudliky[u] > nejvyzsi) {
- nejvyzsi = cudliky[u];
- }
- }
- if (nejvyzsi == 0) {
- return -1;
- }
- int h;
- for (h = 0; h < 4; h++) {
- if (cudliky[h] == nejvyzsi) {
- if (h == 0) {
- return h + 1;
- }
- if (h == 1) {
- return h + 1;
- }
- if (h == 2) {
- return h + 1;
- }
- if (h == 3) {
- return h + 1;
- }
- }
- }
- }
- int readFromDevice(uint32_t offset) {
- void * temppointer = pointer;
- temppointer += offset;
- data = *((char *) temppointer);
- return data;
- }
- void dirList(int number, char *directory) {
- DIR *dp;
- struct dirent *ep;
- int i = 0;
- dp = opendir(directory);
- if (dp != NULL) {
- while (ep = readdir(dp)){
- if(i == number){
- zapiString(ep->d_name);
- }
- i++;
- }
- (void) closedir(dp);
- } else
- zapiString("Cant open dir");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement