Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / wiringPi / pseudoPins.c @ 32:cadb9025f1e0

History | View | Annotate | Download (2.7 KB)

1
/*
2
 * pseudoPins.c:
3
 *        Extend wiringPi with a number of pseudo pins which can be
4
 *        digitally or analog written/read.
5
 *
6
 *        Note:
7
 *                Just one set of pseudo pins can exist per Raspberry Pi.
8
 *                These pins are shared between all programs running on
9
 *                that Raspberry Pi. The values are also persistant as
10
 *                they live in shared RAM. This gives you a means for
11
 *                temporary variable storing/sharing between programs,
12
 *                or for other cunning things I've not thought of yet..
13
 *
14
 *        Copyright (c) 2012-2016 Gordon Henderson
15
 ***********************************************************************
16
 * This file is part of wiringPi:
17
 *        https://projects.drogon.net/raspberry-pi/wiringpi/
18
 *
19
 *    wiringPi is free software: you can redistribute it and/or modify
20
 *    it under the terms of the GNU Lesser General Public License as
21
 *    published by the Free Software Foundation, either version 3 of the
22
 *    License, or (at your option) any later version.
23
 *
24
 *    wiringPi is distributed in the hope that it will be useful,
25
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 *    GNU Lesser General Public License for more details.
28
 *
29
 *    You should have received a copy of the GNU Lesser General Public
30
 *    License along with wiringPi.
31
 *    If not, see <http://www.gnu.org/licenses/>.
32
 ***********************************************************************
33
 */
34

    
35
#define        SHARED_NAME        "wiringPiPseudoPins"
36
#define        PSEUDO_PINS        64
37

    
38
#include <unistd.h>
39
#include <sys/types.h>
40
#include <sys/mman.h>
41
#include <sys/stat.h>
42
#include <fcntl.h>
43

    
44
#include <wiringPi.h>
45

    
46
#include "pseudoPins.h"
47

    
48
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
49
{
50
  int *ptr   = (int *)node->data0 ;
51
  int  myPin = pin - node->pinBase ;
52

    
53
  return *(ptr + myPin) ;
54
}
55

    
56

    
57
static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
58
{
59
  int *ptr   = (int *)node->data0 ;
60
  int  myPin = pin - node->pinBase ;
61

    
62
  *(ptr + myPin) = value ;
63
}
64

    
65

    
66
/*
67
 * pseudoPinsSetup:
68
 *        Create a new wiringPi device node for the pseudoPins driver
69
 *********************************************************************************
70
 */
71

    
72
int pseudoPinsSetup (const int pinBase)
73
{
74
  struct wiringPiNodeStruct *node ;
75
  void *ptr ;
76

    
77
  node = wiringPiNewNode (pinBase, PSEUDO_PINS) ;
78

    
79
  node->fd = shm_open (SHARED_NAME, O_CREAT | O_RDWR, 0666) ;
80

    
81
  if (node->fd < 0)
82
    return FALSE ;
83

    
84
  if (ftruncate (node->fd, PSEUDO_PINS * sizeof (int)) < 0)
85
    return FALSE ;
86

    
87
  ptr = mmap (NULL, PSEUDO_PINS * sizeof (int), PROT_READ | PROT_WRITE, MAP_SHARED, node->fd, 0) ;
88

    
89
  node->data0 = (unsigned int)ptr ;
90

    
91
  node->analogRead  = myAnalogRead ;
92
  node->analogWrite = myAnalogWrite ;
93

    
94
  return TRUE ;
95
}