-
Notifications
You must be signed in to change notification settings - Fork 15
/
MPWBCMStore.m
90 lines (74 loc) · 1.72 KB
/
MPWBCMStore.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#import "MPWBCMStore.h"
#import "MPWGPIOPin.h"
#include <bcm2835.h>
#define MINPIN 0
#define MAXPIN 32
@implementation MPWBCMStore
{
int modes[MAXPIN*2];
}
-(instancetype)init
{
self=[super init];
if (!bcm2835_init()) {
return nil;
}
return self;
}
-(void)setMode:(int)mode ofPin:(int)pin
{
if ( pin>=MINPIN && pin <MAXPIN) {
if( modes[pin] != mode ) {
bcm2835_gpio_fsel(pin, mode);
modes[pin]=mode;
}
}
}
-(void)writePin:(int)pin value:(int)value
{
if ( pin>=0 && pin < MAXPIN ) {
[self setMode:BCM2835_GPIO_FSEL_OUTP ofPin:pin];
bcm2835_gpio_write(pin, value);
} else {
[NSException raise:@"pin out of range" format:@"pin %d out of range",pin];
}
}
-(int)readPin:(int)pin
{
if ( pin>=0 && pin < MAXPIN ) {
[self setMode:BCM2835_GPIO_FSEL_INPT ofPin:pin];
return bcm2835_gpio_lev(pin) ? YES : NO;
} else {
[NSException raise:@"pin out of range" format:@"pin %d out of range",pin];
}
return 0;
}
-(int)pinForAddress:(id <MPWReferencing>)address
{
NSArray<NSString*> *pathComponents=[address pathComponents];
if ( pathComponents.count == 1 ) {
int pinNumber = [pathComponents[0] intValue];
return pinNumber;
} else {
[NSException raise:@"incorrect pin address" format:@"%@",pathComponents];
}
}
-(id)at:(id <MPWReferencing>)address
{
return @([self readPin:[self pinForAddress:address]]);
}
-(void)at:(id <MPWReferencing>)address put:value
{
int pinValue = [value intValue] ? HIGH : LOW;
[self writePin:[self pinForAddress:address] value:pinValue];
}
-(MPWBinding*)bindingForReference:aReference inContext:aContext
{
return [MPWGPIOPin bindingWithReference:aReference inStore:self];
}
-(void)dealloc
{
bcm2835_close();
[super dealloc];
}
@end