blink_tm 0.1.3 → 1.0.0

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
  SHA256:
3
- metadata.gz: 3a36bc60afc808cd46592945eebb5522b017af21743da1cdc71eeb381a7367dd
4
- data.tar.gz: 8a5d4c012fdad495381e8fe4caafa39e9b3aafa8490f52dc83460cecc8826296
3
+ metadata.gz: '080b57c4c4dac231dbb6353428aaaf7be86bd47e4da2379308c23d9ca5315aaa'
4
+ data.tar.gz: e13ccff20189b8e1e944230b0c27ce87272677a9d50ee2c289098800f1860fe9
5
5
  SHA512:
6
- metadata.gz: ac2ac55e696cb64a0260057029633e1132255d1956f9a484c841515fb3b9dac92d21ec6b4f7287186396c0c4bd3b54f301b69be88126616ee2ef4c9ffdd1a84c
7
- data.tar.gz: 59454494c4df79ed7918e925c7e9cacf8db35b1e2b698e2192ba1c953829db023fb28d876075d736e9128f1940adc78d20892f3b3e75ec9201c961fad565f114
6
+ metadata.gz: 1ae79bb6028b57c8046e1cf68ccd62fcac30ae38a75579d6402855e9dce94d012877cd358bd0b4a99dc6ddd1376ba3548a3ac88aa59867ea54d9c7bdd7e5f16c
7
+ data.tar.gz: 4cc14d89f98e59675fa2071005cda0b44a4c41f421c5ea0ba850193363b33c922c623187c939f9ed48bd152277732ceb7022e3ccedc88cc13ba710c2d52157f4
data/exe/blink-tm CHANGED
@@ -9,9 +9,9 @@ while(!dev)
9
9
  dev = BlinkTM.find_device
10
10
 
11
11
  if dev
12
- puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: Device discovered successfully. Path: #{dev}#{BlinkTM::RESET}"
12
+ puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Device discovered successfully. Path: #{dev}#{BlinkTM::RESET}"
13
13
  else
14
- puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: No device found. Retrying #{retry_count += 1}#{BlinkTM::RESET}"
14
+ puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: No device found. Retrying #{retry_count += 1}#{BlinkTM::RESET}"
15
15
  sleep 0.5
16
16
  end
17
17
  end
@@ -7,7 +7,7 @@ VALUE setBaudRate(volatile VALUE obj, volatile VALUE dev, volatile VALUE speed)
7
7
  char *device = StringValuePtr(dev) ;
8
8
  unsigned int spd = NUM2UINT(speed) ;
9
9
 
10
- int serial_port = open(device, O_RDWR) ;
10
+ int serial_port = open(device, O_RDWR | O_NOCTTY) ;
11
11
  struct termios tty ;
12
12
 
13
13
  char status = tcgetattr(serial_port, &tty) ;
@@ -22,7 +22,10 @@ VALUE setBaudRate(volatile VALUE obj, volatile VALUE dev, volatile VALUE speed)
22
22
  -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
23
23
  */
24
24
 
25
- if(status != 0) return Qnil ;
25
+ if(status != 0) {
26
+ close(serial_port) ;
27
+ return Qnil ;
28
+ }
26
29
 
27
30
  tty.c_cflag &= ~CSIZE ;
28
31
  tty.c_cflag |= CS8 ;
@@ -46,18 +49,20 @@ VALUE setBaudRate(volatile VALUE obj, volatile VALUE dev, volatile VALUE speed)
46
49
  cfsetospeed(&tty, spd) ;
47
50
  status = tcsetattr(serial_port, TCSANOW, &tty) ;
48
51
 
49
- if (status == 0) return Qtrue ;
52
+ close(serial_port) ;
50
53
 
54
+ if (status == 0) return Qtrue ;
51
55
  return Qfalse ;
52
56
  }
53
57
 
54
58
  VALUE getBaudRate(volatile VALUE obj, volatile VALUE dev) {
55
59
  char *device = StringValuePtr(dev) ;
56
60
 
57
- int serial_port = open(device, O_RDWR) ;
61
+ int serial_port = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK) ;
58
62
  struct termios tty ;
59
63
 
60
64
  char status = tcgetattr(serial_port, &tty) ;
65
+ close(serial_port) ;
61
66
 
62
67
  if(status == 0) {
63
68
  unsigned int in = cfgetispeed(&tty) ;
data/ext/crc32/crc32.c ADDED
@@ -0,0 +1,33 @@
1
+ /*
2
+ Source:
3
+ https://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art008
4
+
5
+ Note that this obeys the standard and matches with Ruby's Zlib.crc32(...)
6
+ */
7
+
8
+ #define CRC32_DIVISOR 0xEDB88320
9
+ #include "ruby.h"
10
+
11
+ static VALUE getCRC(volatile VALUE obj, volatile VALUE str) {
12
+ char *input = StringValuePtr(str) ;
13
+ unsigned char len = strlen(input) ;
14
+ unsigned long crc = 0xFFFFFFFF ;
15
+
16
+ for (unsigned char i = 0 ; i < len ; ++i) {
17
+ crc ^= input[i] ;
18
+
19
+ for (unsigned char k = 8 ; k ; --k) {
20
+ crc = crc & 1 ? (crc >> 1) ^ CRC32_DIVISOR : crc >> 1 ;
21
+ }
22
+ }
23
+
24
+ char buffer[11] ;
25
+ sprintf(buffer, "%lu", crc ^ 0xFFFFFFFF) ;
26
+
27
+ return rb_str_new_cstr(buffer) ;
28
+ }
29
+
30
+ void Init_crc32() {
31
+ VALUE blinktm = rb_define_module("BlinkTM") ;
32
+ rb_define_module_function(blinktm, "crc32", getCRC, 1) ;
33
+ }
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile 'blink_tm/crc32'
data/lib/blink_tm.rb CHANGED
@@ -1,20 +1,24 @@
1
1
  # Frozen_String_Literal: true
2
2
  require 'blink_tm/baudrate'
3
+ require 'linux_stat'
4
+
5
+ $-v = true
3
6
 
4
7
  module BlinkTM
5
8
  # Important Constants
6
- BAUDRATE = BlinkTM::B57600
9
+ BAUDRATE = BlinkTM::B38400
7
10
  SCANID = 'BTM'
8
11
 
9
12
  # POLLING time, how often should CPU, Net, IO usages should be updated.
10
13
  # Should always be a float.
11
- POLLING = 0.375
14
+ POLLING = 0.250
12
15
 
13
16
  # Refresh time, how often the main loop should run
14
17
  REFRESH = 0.5
15
18
 
16
19
  # Errors
17
20
  NoDeviceError = Class.new(StandardError)
21
+ SyncError = Class.new(StandardError)
18
22
  DeviceNotReady = Class.new(StandardError)
19
23
 
20
24
  # Units
@@ -30,9 +34,18 @@ module BlinkTM
30
34
  ORANGE = "\e[38;2;245;155;20m"
31
35
  BOLD = "\e[1m"
32
36
  RESET = "\e[0m"
37
+
38
+ # Other constants
39
+ ROOT_DEV = ::LinuxStat::Mounts.root
40
+ ROOT = File.split(ROOT_DEV)[-1]
41
+
42
+ SECTORS = ::LS::Filesystem.sectors(ROOT_DEV)
43
+
44
+ abort "#{BOLD}#{RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Can't get root partition#{RESET}" unless ROOT
45
+ abort "#{BOLD}#{RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Can't get sector size#{RESET}" unless SECTORS
33
46
  end
34
47
 
35
48
  require 'blink_tm/version'
36
49
  require 'fcntl'
37
- require 'linux_stat'
38
50
  require 'blink_tm/blink_tm'
51
+ require 'blink_tm/crc32'
@@ -1,4 +1,4 @@
1
- #!/usr/bin/ruby -w
1
+ #!/usr/bin/env ruby
2
2
  # Frozen_String_Literal: true
3
3
 
4
4
  module BlinkTM
@@ -77,7 +77,10 @@ module BlinkTM
77
77
  io_r = io_w = 0
78
78
 
79
79
  Thread.new {
80
- cpu_u = LS::CPU.total_usage(POLLING).to_f while true
80
+ while true
81
+ _cpu_u = LS::CPU.total_usage(POLLING).to_f
82
+ cpu_u = _cpu_u.nan? ? 0 : _cpu_u.to_i
83
+ end
81
84
  }
82
85
 
83
86
  Thread.new {
@@ -90,27 +93,34 @@ module BlinkTM
90
93
 
91
94
  Thread.new {
92
95
  while true
93
- io_stat1 = iostat()
96
+ io_stat1 = LS::FS.total_io(ROOT)
94
97
  sleep POLLING
95
- io_stat2 = iostat()
98
+ io_stat2 = LS::FS.total_io(ROOT)
96
99
 
97
- io_r = io_stat2[0].-(io_stat1[0]).fdiv(POLLING)
98
- io_w = io_stat2[1].-(io_stat1[1]).fdiv(POLLING)
100
+ io_r = io_stat2[0].-(io_stat1[0]).*(SECTORS).fdiv(POLLING)
101
+ io_w = io_stat2[1].-(io_stat1[1]).*(SECTORS).fdiv(POLLING)
99
102
  end
100
103
  }
101
104
 
105
+ prev_crc32 = ''
106
+
102
107
  begin
103
108
  raise NoDeviceError unless device
104
109
 
105
110
  in_sync = false
106
- fd = IO.sysopen(device, Fcntl::O_RDWR | Fcntl::O_EXCL)
111
+
112
+ fd = IO.sysopen(
113
+ device,
114
+ Fcntl::O_RDWR | Fcntl::O_NOCTTY | Fcntl::O_NONBLOCK | Fcntl::O_TRUNC
115
+ )
116
+
107
117
  file = IO.open(fd)
108
118
 
109
119
  until in_sync
110
120
  # Clear out any extra zombie bits
111
- file.syswrite(?~)
121
+ file.syswrite(?~.freeze)
112
122
  # Start the device
113
- file.syswrite(?#)
123
+ file.syswrite(?#.freeze)
114
124
  file.flush
115
125
 
116
126
  sleep 0.125
@@ -124,12 +134,12 @@ module BlinkTM
124
134
  sleep 0.05
125
135
  retry
126
136
  end
127
-
128
- sleep 0.05
129
137
  end
130
138
 
139
+ sync_error_count = 0
140
+
131
141
  puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Device ready!#{BlinkTM::RESET}"
132
- refresh = 0
142
+ file.read
133
143
 
134
144
  while true
135
145
  # cpu(01234) memUsed(999993) swapUsed(999992) io_active(0)
@@ -154,16 +164,23 @@ module BlinkTM
154
164
  # "#{convert_bytes(net_u)} #{convert_bytes(net_d)} "\
155
165
  # "#{convert_bytes(io_r)} #{convert_bytes(io_w)}"
156
166
 
157
- refresh ^= 1
158
-
159
167
  str = "!##{"%03d" % cpu_u}#{"%03d" % mem_u}#{"%03d" % swap_u}"\
160
168
  "#{convert_bytes(net_u)}#{convert_bytes(net_d)}"\
161
- "#{convert_bytes(io_r)}#{convert_bytes(io_w)}#{refresh}~"
169
+ "#{convert_bytes(io_r)}#{convert_bytes(io_w)}1~"
162
170
 
163
171
  # Rescuing from suspend
164
172
  file.syswrite(str)
165
173
  file.flush
174
+ crc32 = file.read.scrub![/\d+/]
166
175
 
176
+ unless crc32 == prev_crc32 || prev_crc32.empty?
177
+ raise SyncError if sync_error_count > 1
178
+ sync_error_count += 1
179
+ else
180
+ sync_error_count = 0 unless sync_error_count == 0
181
+ end
182
+
183
+ prev_crc32 = BlinkTM.crc32(str[2..-2])
167
184
  sleep REFRESH
168
185
  end
169
186
  rescue Interrupt, SystemExit, SignalException
@@ -178,6 +195,10 @@ module BlinkTM
178
195
  sleep 0.1
179
196
  end
180
197
 
198
+ retry
199
+ rescue BlinkTM::SyncError
200
+ file &.close
201
+ sleep 0.005
181
202
  retry
182
203
  rescue BlinkTM::DeviceNotReady
183
204
  file &.close
@@ -191,31 +212,5 @@ module BlinkTM
191
212
  end
192
213
  end
193
214
 
194
- def iostat
195
- @@root_partition ||= IO.foreach('/proc/mounts').detect {
196
- |x| x.split[1] == ?/
197
- }.to_s.split[0].to_s.split(?/).to_a[-1]
198
-
199
- @@sector_size ||= LS::FS.stat(?/)[:block_size]
200
-
201
- io = []
202
-
203
- IO.foreach('/proc/diskstats') { |x|
204
- splitted = x.split
205
-
206
- if splitted[2] == @@root_partition
207
- # IO read / write
208
- io.replace([
209
- splitted[5].to_i * @@sector_size,
210
- splitted[9].to_i * @@sector_size
211
- ])
212
-
213
- break
214
- end
215
- }
216
-
217
- io
218
- end
219
-
220
215
  extend(self)
221
216
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlinkTM
4
- VERSION = "0.1.3"
4
+ VERSION = "1.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blink_tm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-28 00:00:00.000000000 Z
11
+ date: 2021-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: linux_stat
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.2.2
19
+ version: 2.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 2.2.2
26
+ version: 2.3.0
27
27
  description: A controller for Arduino OLED System Monitor, Blink Task Manager
28
28
  email:
29
29
  - souravgoswami@protonmail.com
@@ -31,11 +31,14 @@ executables:
31
31
  - blink-tm
32
32
  extensions:
33
33
  - ext/baudrate/extconf.rb
34
+ - ext/crc32/extconf.rb
34
35
  extra_rdoc_files: []
35
36
  files:
36
37
  - exe/blink-tm
37
38
  - ext/baudrate/baudrate.c
38
39
  - ext/baudrate/extconf.rb
40
+ - ext/crc32/crc32.c
41
+ - ext/crc32/extconf.rb
39
42
  - lib/blink_tm.rb
40
43
  - lib/blink_tm/blink_tm.rb
41
44
  - lib/blink_tm/version.rb
@@ -58,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
61
  - !ruby/object:Gem::Version
59
62
  version: '0'
60
63
  requirements: []
61
- rubygems_version: 3.2.15
64
+ rubygems_version: 3.2.21
62
65
  signing_key:
63
66
  specification_version: 4
64
67
  summary: A controller for Arduino OLED System Monitor, Blink Task Manager