pirblaster  0.01
Use the pi as a remote control for your TV, DVD player etc.
Receive.c
Go to the documentation of this file.
1 /********************************************************************************/
2 /* Use a raspberry pi with a IR led as remote control for your Tv, Dvd etc. */
3 /* Copyright (C) 2016 Ed Kapitein */
4 /* */
5 /* This program is free software: you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation, either version 3 of the License, or */
8 /* (at your option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details. */
14 /* */
15 /* You should have received a copy of the GNU General Public License */
16 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /********************************************************************************/
18 #define _POSIX_C_SOURCE 199309L
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <fcntl.h>
22 #include <sys/mman.h>
23 #include <unistd.h>
24 #include <inttypes.h>
25 #include <time.h>
26 #include <sys/timeb.h>
27 /* #include "../Receive/Receive.h" */
28 
29 #define BCM2708_PERI_BASE 0x20000000
30 #define GPIO 0x00200000 /* GPIO controller */
31 #define TIMER_CONTROL 0x00003000
32 #define ONE_MHZ_COUNTER 0x00000004
33 
34 #define PAGE_SIZE (4*1024)
35 #define BLOCK_SIZE (4*1024)
36 
37 
38 #define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
39 #define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
40 #define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
41 
42 #define GPIO_SET *(gpio+7)
43 #define GPIO_CLR *(gpio+10)
44 
45 #define GET_GPIO(g) (*(gpio+13)&(1<<g))
46 
47 #define GPIO_PULL *(gpio+37)
48 #define GPIO_PULLCLK0 *(gpio+38)
49 
50 int main()
51  {
52  #define NrOfSamples 500
53  long int i;
54  uint32_t Buffer[NrOfSamples + 1];
55  uint32_t TimeStamp[NrOfSamples + 1];
56  void *TimerBase;
57  volatile uint32_t *gpio;
58  volatile uint32_t *Timer;
59  uint32_t StartTime;
60  int fd;
61 
62  /* open /dev/mem */
63  if ((fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0)
64  {
65  printf("can't open /dev/mem \n");
66  exit(-1);
67  }
68 
69  /* set a pointer to the 1MHz clock we use for time stamps */
70  if( ( TimerBase = mmap( NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, BCM2708_PERI_BASE + TIMER_CONTROL ) ) == NULL )
71  {
72  fprintf( stderr, "201608281433 mmap() failed.\n");
73  return -1;
74  }
75  Timer = (uint32_t *)((char *)TimerBase + ONE_MHZ_COUNTER );
76 
77  /* set a pointer to the gpio base */
78  if( ( gpio = mmap( NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, BCM2708_PERI_BASE + GPIO ) ) == NULL )
79  {
80  fprintf( stderr, "201608281434 mmap() failed.\n");
81  return -1;
82  }
83 
84  INP_GPIO(17);
85 
86  while( GET_GPIO(17) ); /* wait for the pulse */
87  StartTime = *Timer;
88 
89  for(i=0; i< NrOfSamples; i++) /* sample as fast as we can */
90  {
91  Buffer[i] = GET_GPIO(17);
92  while( Buffer[i] == GET_GPIO(17) )
93  {
94  TimeStamp[i] = *Timer - StartTime;
95  }
96  }
97 
98  printf("0,1\n");
99  for(i=0; i< NrOfSamples; i++ ) /* print out the samples */
100  {
101  printf( "%" PRIu32 ",", TimeStamp[i] );
102  if(Buffer[i])
103  {
104  printf( "1\n" ); /* the carrier was off, but has switched on after this timestamp */
105  }
106  else
107  {
108  printf( "0\n" ); /* the carrier was on, but has switched off after this timestamp */
109  }
110  }
111 
112  return(0);
113 
114  }
int main()
Definition: Receive.c:50
#define GPIO
Definition: Receive.c:30
#define NrOfSamples
#define INP_GPIO(g)
Definition: Receive.c:38
#define ONE_MHZ_COUNTER
Definition: Receive.c:32
#define GET_GPIO(g)
Definition: Receive.c:45
#define TIMER_CONTROL
Definition: Receive.c:31
#define BLOCK_SIZE
Definition: Receive.c:35
#define BCM2708_PERI_BASE
Definition: Receive.c:29