pigpio 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3e3b36ea74c532d0278a04483af6e8c647a5df3a
4
- data.tar.gz: d836915f6f247d343fe50eda5eebef2995447373
3
+ metadata.gz: 01a1c4e0c688be24b237155ef8958a86515efae4
4
+ data.tar.gz: 31044e1d6f588ea3f45edb2fb3dc575508caa9da
5
5
  SHA512:
6
- metadata.gz: cfbd04cf3b854038492c5b3b21fae7accb109a2452dbd6941e36dd3d872a7f6f3e17120e9c5598d7e3469a1a8933657154b22d032ff15bd32e1d471aec21f0b3
7
- data.tar.gz: 2e47b1431603926156d2961058216968368e9235388b93220f8b330ea9524b62f761a00ef6ddc3404cfc1c7e52f58eaff9b43a8dcb359da203af248eab6daf68
6
+ metadata.gz: d5be365ff9e98fad14a67ba8922f3450cf23093a27d1443b9e09547e20a8cc7d4cc28c2259f03c1ea6762559fd2ae896388d821641e6cab7ed9085cdcd17fb53
7
+ data.tar.gz: 1eaff222417c95031107ee21b7723a6caf2655b7f6f9275ff6483c58b27046b6e516fdf9535146062b4d73907ae7588095a027c1b1f3bf38f6aa1c0e2523952a
data/README.md CHANGED
@@ -51,9 +51,11 @@ led.pud = PI_PUD_OFF
51
51
  led.write 0
52
52
  sleep 1
53
53
  end
54
+ pi.stop
54
55
  ```
55
56
 
56
57
  Then, run deamon and script.
58
+
57
59
  ```sh
58
60
  $ sudo pigpiod
59
61
  $ ruby example_led.rb
@@ -13,11 +13,15 @@ button=pi.gpio(17)
13
13
  button.mode=PI_INPUT
14
14
  button.pud=PI_PUD_UP
15
15
  button.glitch_filter(30)
16
- button.callback(EITHER_EDGE){|tick,level|
16
+ cb=button.callback(EITHER_EDGE){|tick,level|
17
17
  led.write level
18
18
  counter+=1
19
19
  }
20
20
 
21
+ led.write 1
21
22
  while(counter<10)do
23
+ sleep(1)
22
24
  end
23
- led.write 0
25
+ cb.cancel
26
+ led.write 0
27
+ pi.stop
@@ -0,0 +1,18 @@
1
+ require "pigpio"
2
+ include Pigpio::Constant
3
+
4
+ pi=Pigpio.new
5
+ unless pi.connect
6
+ exit -1
7
+ end
8
+
9
+ led = pi.gpio(4)
10
+ led.mode = PI_OUTPUT
11
+ led.pud = PI_PUD_OFF
12
+ 3.times do |i|
13
+ led.write 1
14
+ sleep 1
15
+ led.write 0
16
+ sleep 1
17
+ end
18
+ pi.stop
@@ -18,4 +18,5 @@ while true do
18
18
  i=(i+1)%256 if button.read == 0
19
19
  pwm.dutycycle= i
20
20
  sleep 0.01
21
- end
21
+ end
22
+ pi.stop
@@ -11,6 +11,7 @@
11
11
  LED brightness will increase while you press the button.
12
12
 
13
13
  ```sh
14
+ $ sudo pigpiod
14
15
  $ ruby pwm.rb
15
16
  ```
16
17
 
@@ -18,10 +19,9 @@ $ ruby pwm.rb
18
19
 
19
20
  [Script is here.](./callback.rb)
20
21
 
21
- not work, now.
22
-
23
- LED will light, while you press the button.
22
+ LED will light, while you release the button.
24
23
 
25
24
  ```sh
25
+ $ sudo pigpiod
26
26
  $ ruby callback.rb
27
27
  ```
@@ -4,81 +4,87 @@
4
4
  #define TypedData_Get_Struct2(obj, type, data_type) ((type*)rb_check_typeddata((obj), (data_type)))
5
5
 
6
6
  static VALUE cCallbackID;
7
+ static VALUE cNativeQueue;
7
8
  static VALUE cCallbackError;
8
9
 
9
10
  typedef struct{
10
- unsigned level;
11
11
  uint32_t tick;
12
+ unsigned char level;
13
+ unsigned char dummy;
14
+ unsigned char gpio;
15
+ unsigned char pi;
12
16
  } callback_item_t;
13
17
 
14
18
  #define PG_EXT_CALLBACK_BUF_SIZE (128)
15
19
  typedef struct{
16
20
  volatile callback_item_t buf[ PG_EXT_CALLBACK_BUF_SIZE ];
17
- volatile callback_item_t* volatile read;
18
- volatile callback_item_t* volatile write;
19
- volatile int flag_overflow;
21
+ volatile unsigned short read;
22
+ volatile unsigned short write;
23
+ volatile unsigned short size;
24
+ volatile char flag_overflow;
20
25
  } callback_queue_t;
21
26
 
22
27
 
23
28
 
24
29
  typedef struct{
25
30
  callback_queue_t*queue;
31
+ VALUE callback;
32
+ struct timeval t;
26
33
  } callback_pqueue_t;
27
- void pigpio_rbst_callback_que_dfree(void* _self){
34
+ void pigpio_rbst_callback_pqueue_dmark(void* _self){
35
+ callback_pqueue_t *self=(callback_pqueue_t *)_self;
36
+ rb_gc_mark(self->callback);
37
+ }
38
+ void pigpio_rbst_callback_pqueue_dfree(void* _self){
28
39
  callback_pqueue_t *self=(callback_pqueue_t *)_self;
29
40
  if(self->queue!=NULL)free(self->queue);
30
41
  xfree(self);
31
42
  return;
32
43
  }
33
- size_t pigpio_rbst_callback_que_dsize(const void *_self){
44
+ size_t pigpio_rbst_callback_pqueue_dsize(const void *_self){
34
45
  return sizeof(callback_pqueue_t)+sizeof(callback_queue_t);
35
46
  }
36
47
  const rb_data_type_t callback_pqueue_type = { //https://gist.github.com/yugui/87ef6964d8a76794be6f
37
- "struct@callback_pqueue",{NULL,pigpio_rbst_callback_que_dfree,pigpio_rbst_callback_que_dsize,{0,0}},0,NULL,0
48
+ "struct@callback_pqueue",{
49
+ pigpio_rbst_callback_pqueue_dmark,
50
+ pigpio_rbst_callback_pqueue_dfree,
51
+ pigpio_rbst_callback_pqueue_dsize,{0,0}},0,NULL,0
38
52
  };
39
53
  /*
40
54
  Constructor of Pigpio::NativeQueue class
41
55
  */
42
- VALUE pigpio_rbst_callback_pqueue_make(VALUE self){
56
+ VALUE pigpio_rbst_callback_pqueue_make_inner(VALUE callback,time_t sec,long usec){
43
57
  VALUE obj;
44
58
  callback_pqueue_t *st;
45
59
  callback_queue_t *queue;
46
- obj = TypedData_Make_Struct(self, callback_pqueue_t, &callback_pqueue_type, st);
60
+ obj = TypedData_Make_Struct(cNativeQueue, callback_pqueue_t, &callback_pqueue_type, st);
61
+ st->t.tv_sec=sec;
62
+ st->t.tv_usec=usec;
63
+ st->callback=callback;
47
64
  st->queue=queue=malloc(sizeof(callback_queue_t));
48
- queue->read=queue->buf;
49
- queue->write=queue->buf;
65
+ queue->read=0;
66
+ queue->write=0;
50
67
  queue->flag_overflow=0;
51
- return obj;
52
- }
53
- /*
54
- Get a data from FIFO Queue.
55
- */
56
- VALUE pigpio_rbst_callback_pqueue_pop(VALUE self){
57
- callback_pqueue_t *st=TypedData_Get_Struct2(self,callback_pqueue_t,&callback_pqueue_type);
58
- callback_queue_t *queue=st->queue;
59
- volatile callback_item_t *cur=queue->read;
60
- VALUE ret;
61
- struct timeval t;
62
- t.tv_sec=0;
63
- t.tv_usec=100000;
64
- if(queue->flag_overflow!=0){
65
- rb_raise(cCallbackError,"Overflow NativeQueue.\n");
66
- }
67
- while(cur==queue->write){
68
- rb_thread_wait_for(t);
69
- }
70
- ret=rb_ary_new_from_args(2,ULONG2NUM(cur->tick),UINT2NUM(cur->level));
71
- queue->read=(cur==queue->buf+(PG_EXT_CALLBACK_BUF_SIZE-1)) ? queue->buf : (cur+1) ;
72
- return(ret);
68
+ return obj;
73
69
  }
74
70
  /*
75
71
  Set a data to FIFO Queue.
76
72
  */
77
- void pigpio_rbstn_callback_pqueue_push(callback_queue_t *queue,unsigned level, uint32_t tick){
78
- volatile callback_item_t *cur=queue->write;
73
+ void pigpio_rbbk_CBFuncEx(int pi, unsigned user_gpio, unsigned level, uint32_t tick, void *_queue){
74
+ callback_queue_t *queue=(callback_queue_t *)_queue;
75
+ unsigned short cur=queue->write;
76
+ queue->buf[cur].tick=tick;
77
+ queue->buf[cur].level=level;
78
+ queue->buf[cur].gpio=user_gpio;
79
+ queue->buf[cur].pi=pi;
80
+ cur=(cur>=(PG_EXT_CALLBACK_BUF_SIZE-1)) ? 0 : (cur+1) ;
81
+ /*
79
82
  cur->tick=tick;
80
83
  cur->level=level;
84
+ cur->gpio=user_gpio;
85
+ cur->pi=pi;
81
86
  cur=(cur==queue->buf+(PG_EXT_CALLBACK_BUF_SIZE-1)) ? queue->buf : (cur+1) ;
87
+ */
82
88
  if(cur==queue->read){
83
89
  queue->flag_overflow=1;
84
90
  }else{
@@ -86,6 +92,28 @@ void pigpio_rbstn_callback_pqueue_push(callback_queue_t *queue,unsigned level, u
86
92
  }
87
93
  return;
88
94
  }
95
+ static VALUE callback_receive(void*_self){
96
+ callback_pqueue_t *st=TypedData_Get_Struct2((VALUE)_self,callback_pqueue_t,&callback_pqueue_type);
97
+ VALUE callee_proc =st->callback;
98
+ struct timeval t =st->t;
99
+ callback_queue_t *queue=st->queue;
100
+ unsigned short cur=queue->read;
101
+
102
+ while(1){
103
+ if(queue->flag_overflow!=0){
104
+ rb_raise(cCallbackError,"Overflow NativeQueue.\n");
105
+ }
106
+ while(cur==queue->write){
107
+ rb_thread_wait_for(t);
108
+ }
109
+ rb_funcall((VALUE)callee_proc, rb_intern("call"), 4,
110
+ ULONG2NUM(queue->buf[cur].tick),CHR2FIX(queue->buf[cur].level),CHR2FIX(queue->buf[cur].gpio),CHR2FIX(queue->buf[cur].pi));
111
+ queue->read=cur=(cur==(PG_EXT_CALLBACK_BUF_SIZE-1)) ? 0 : (cur+1) ;
112
+ }
113
+ return Qnil;
114
+ }
115
+
116
+
89
117
 
90
118
  typedef int (*cancel_t)(unsigned);
91
119
  typedef struct{
@@ -124,7 +152,6 @@ VALUE pigpio_rbst_callback_id_make_inner(int id,cancel_t cancel,VALUE queue,VALU
124
152
  st->queue=queue;
125
153
  st->thread=thread;
126
154
  st->cancel=cancel;
127
-
128
155
  return obj;
129
156
  }
130
157
  /*
@@ -150,18 +177,16 @@ VALUE pigpio_rbst_callback_id_cancel(VALUE self){
150
177
  callback_id_t *st=TypedData_Get_Struct2(self,callback_id_t,&callback_id_data_type);
151
178
  id=st->id;
152
179
  if(id<0){return INT2NUM(pigif_callback_not_found);}
180
+ id=(*(st->cancel))(st->id);
153
181
  if(st->thread!=Qnil){
154
182
  rb_funcall((VALUE)st->thread, rb_intern("kill"), 0);
155
183
  }
156
184
  st->id=-1;
157
185
  st->thread=Qnil;
158
186
  st->queue=Qnil;
159
- return INT2NUM((*(st->cancel))(id));
160
- }
161
- void pigpio_rbbk_CBFuncEx(int pi, unsigned user_gpio, unsigned level, uint32_t tick, void *queue){
162
- pigpio_rbstn_callback_pqueue_push((callback_queue_t *)queue,level,tick);
163
- return;
187
+ return INT2NUM(id);
164
188
  }
189
+
165
190
  void pigpio_rbbk_evtCBFuncEx(int pi, unsigned event, uint32_t tick, void *callee_proc){
166
191
  (rb_funcall((VALUE)callee_proc, rb_intern("call"), 2,ULONG2NUM(tick),UINT2NUM(event)));
167
192
  return;
@@ -1987,15 +2012,25 @@ GPIO has the identified edge.
1987
2012
  . .
1988
2013
 
1989
2014
  :call-seq:
1990
- callback(Integer pi,Integer user_gpio, Integer edge){|tick,level,user_gpio| } -> Pigpio::Callback
2015
+ callback(Integer pi,Integer user_gpio, Integer edge){|tick,level,gpio,pi| ... } -> Pigpio::Callback
1991
2016
 
1992
2017
  If you call this method without a block, this method raises an Pigpio::CallbackError exception.
1993
2018
 
1994
2019
  See also: {pigpio site}[http://abyz.me.uk/rpi/pigpio/pdif2.html#callback_ex]
1995
2020
  */
1996
- VALUE pigpio_rbfn_callback(VALUE self,VALUE pi, VALUE user_gpio, VALUE edge, VALUE queue, VALUE thread){
2021
+ VALUE pigpio_rbfn_callback(int argc, VALUE *argv, VALUE self){
1997
2022
  int id;
1998
- callback_pqueue_t *st=TypedData_Get_Struct2(queue,callback_pqueue_t,&callback_pqueue_type);
2023
+ VALUE queue;
2024
+ VALUE thread;
2025
+ callback_pqueue_t *st;
2026
+ VALUE pi; VALUE user_gpio; VALUE edge; VALUE block;
2027
+ rb_scan_args(argc,argv,"3&",&pi,&user_gpio,&edge,&block);
2028
+ if(block==Qnil){
2029
+ return Qnil;
2030
+ }
2031
+ queue=pigpio_rbst_callback_pqueue_make_inner(block,0,100000);
2032
+ thread=rb_thread_create(callback_receive,(void*)queue);
2033
+ st=TypedData_Get_Struct2(queue,callback_pqueue_t,&callback_pqueue_type);
1999
2034
 
2000
2035
  //if(NIL_P(callee_proc)){rb_raise(cCallbackError,"No callback block.\n");}
2001
2036
  id=callback_ex(NUM2INT(pi), NUM2UINT(user_gpio), NUM2UINT(edge), pigpio_rbbk_CBFuncEx, (void*)st->queue);
@@ -3676,7 +3711,7 @@ This class has some constances for pigpio library.
3676
3711
  rb_define_singleton_method(cAPI, "wave_add_generic", pigpio_rbfn_wave_add_generic, 2);
3677
3712
  rb_define_singleton_method(cAPI, "wave_add_serial", pigpio_rbfn_wave_add_serial, 7);
3678
3713
  rb_define_singleton_method(cAPI, "wave_chain", pigpio_rbfn_wave_chain, 2);
3679
- rb_define_singleton_method(cAPI, "callback", pigpio_rbfn_callback, 5);
3714
+ rb_define_singleton_method(cAPI, "callback", pigpio_rbfn_callback, -1);
3680
3715
  rb_define_singleton_method(cAPI, "callback_cancel", pigpio_rbfn_callback_cancel, 1);
3681
3716
  rb_define_singleton_method(cAPI, "wait_for_edge", pigpio_rbfn_wait_for_edge, 4);
3682
3717
  rb_define_singleton_method(cAPI, "event_callback", pigpio_rbfn_event_callback, -1);
@@ -3751,9 +3786,8 @@ This class has some constances for pigpio library.
3751
3786
  /*
3752
3787
  The class of native queue.
3753
3788
  */
3754
- cNativeQueue = rb_define_class_under(cPigpio,"NativeQueue", rb_cData);
3755
- rb_define_singleton_method(cNativeQueue, "make", pigpio_rbst_callback_pqueue_make, 0);
3756
- rb_define_method(cNativeQueue, "pop", pigpio_rbst_callback_pqueue_pop, 0);
3789
+ cNativeQueue = rb_define_class_under(cAPI,"NativeQueue", rb_cData);
3790
+ rb_gc_register_address(&cNativeQueue);
3757
3791
 
3758
3792
  /*
3759
3793
  The class of callback.
@@ -12,14 +12,7 @@ class Pigpio
12
12
  end
13
13
  def callback(edge,&blk)
14
14
  return nil unless blk
15
- q=NativeQueue.make
16
- th=Thread.start do
17
- while true
18
- resource = q.pop
19
- blk.call(*resource)
20
- end
21
- end
22
- IF.callback(@pi,@gpio,edge,q,th)
15
+ IF.callback(@pi,@gpio,edge,&blk)
23
16
  end
24
17
  def wait_for_edge(edge,timeout)
25
18
  ret=IF.wait_for_edge(@pi,@gpio,edge,timeout)
@@ -1,3 +1,3 @@
1
1
  class Pigpio
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pigpio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - nak1114
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-17 00:00:00.000000000 Z
11
+ date: 2019-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - check.txt
91
91
  - example/simple/board.svg
92
92
  - example/simple/callback.rb
93
+ - example/simple/led.rb
93
94
  - example/simple/pwm.rb
94
95
  - example/simple/readme.md
95
96
  - ext/pigpio/extconf.rb