blink_tm 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b4e90e73c0c9d495bf37a16e6020498d5880ef79dd2ad36aaa054fd344580e5c
4
+ data.tar.gz: 02a83b060adb7e15c3e06cc6fe266aa699740b494d0a4f87296aa329741aa12a
5
+ SHA512:
6
+ metadata.gz: 40d53fe4c787ad0995525d6531688ef5d83f9049107fca15936826ebde78515cf29c15bd567ad9c19a8bebc6acca9ce38419a874349063e76c93349739c62ea4
7
+ data.tar.gz: f6483431ae948c6eda5a59ca449e086b7a3fb4c8fee9766039bbb1d8c200ddd62994cb00387a78229e32726dbe90881ea57912e01bd31af8534b575541aba4dc
data/exe/blink-tm ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require 'blink_tm'
3
+
4
+ # Find Device running Blink Taskmanager
5
+ dev = nil
6
+ retry_count = 0
7
+
8
+ while(!dev)
9
+ dev = BlinkTM.find_device
10
+
11
+ if dev
12
+ puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: Device discovered successfully. Path: #{dev}#{BlinkTM::RESET}"
13
+ else
14
+ puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: No device found. Retrying #{retry_count += 1}#{BlinkTM::RESET}"
15
+ sleep 0.5
16
+ end
17
+ end
18
+
19
+ BlinkTM.start(dev)
@@ -0,0 +1,99 @@
1
+ #include <stdio.h>
2
+ #include <fcntl.h>
3
+ #include <termios.h>
4
+ #include "ruby.h"
5
+
6
+ VALUE setBaudRate(volatile VALUE obj, volatile VALUE dev, volatile VALUE speed) {
7
+ char *device = StringValuePtr(dev) ;
8
+ unsigned int spd = NUM2UINT(speed) ;
9
+
10
+ int serial_port = open(device, O_RDWR) ;
11
+ struct termios tty ;
12
+
13
+ char status = tcgetattr(serial_port, &tty) ;
14
+
15
+ /*
16
+ Serial Monitor sets these flags:
17
+
18
+ speed 57600 baud; line = 0;
19
+ min = 0; time = 0;
20
+ -brkint -icrnl -imaxbel
21
+ -opost
22
+ -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
23
+ */
24
+
25
+ if(status != 0) return Qnil ;
26
+
27
+ tty.c_cflag &= ~CSIZE ;
28
+ tty.c_cflag |= CS8 ;
29
+ tty.c_cflag |= CREAD | CLOCAL ;
30
+ tty.c_oflag &= ~OPOST ;
31
+
32
+ tty.c_lflag &= ~ECHO ;
33
+ tty.c_lflag &= ~ECHOCTL ;
34
+ tty.c_lflag &= ~ECHOK ;
35
+ tty.c_lflag &= ~ECHOE ;
36
+ tty.c_lflag &= ~ECHOKE ;
37
+ tty.c_lflag &= ~ISIG ;
38
+ tty.c_lflag &= ~ICRNL ;
39
+ tty.c_lflag &= ~IEXTEN ;
40
+ tty.c_lflag &= ~ICANON ;
41
+
42
+ tty.c_cc[VTIME] = 0 ;
43
+ tty.c_cc[VMIN] = 0 ;
44
+
45
+ cfsetispeed(&tty, spd) ;
46
+ cfsetospeed(&tty, spd) ;
47
+ status = tcsetattr(serial_port, TCSANOW, &tty) ;
48
+
49
+ if (status == 0) return Qtrue ;
50
+
51
+ return Qfalse ;
52
+ }
53
+
54
+ VALUE getBaudRate(volatile VALUE obj, volatile VALUE dev) {
55
+ char *device = StringValuePtr(dev) ;
56
+
57
+ int serial_port = open(device, O_RDWR) ;
58
+ struct termios tty ;
59
+
60
+ char status = tcgetattr(serial_port, &tty) ;
61
+
62
+ if(status == 0) {
63
+ unsigned int in = cfgetispeed(&tty) ;
64
+ unsigned int out = cfgetospeed(&tty) ;
65
+
66
+ return rb_ary_new_from_args(2,
67
+ UINT2NUM(in), UINT2NUM(out)
68
+ ) ;
69
+ }
70
+
71
+ return rb_ary_new() ;
72
+ }
73
+
74
+ void Init_baudrate() {
75
+ VALUE blinktm = rb_define_module("BlinkTM") ;
76
+ rb_define_const(blinktm, "B0", INT2FIX(B0)) ;
77
+ rb_define_const(blinktm, "B50", INT2FIX(B50)) ;
78
+ rb_define_const(blinktm, "B75", INT2FIX(B75)) ;
79
+ rb_define_const(blinktm, "B110", INT2FIX(B110)) ;
80
+ rb_define_const(blinktm, "B134", INT2FIX(B134)) ;
81
+ rb_define_const(blinktm, "B150", INT2FIX(B150)) ;
82
+ rb_define_const(blinktm, "B200", INT2FIX(B200)) ;
83
+ rb_define_const(blinktm, "B300", INT2FIX(B300)) ;
84
+ rb_define_const(blinktm, "B600", INT2FIX(B600)) ;
85
+ rb_define_const(blinktm, "B1200", INT2FIX(B1200)) ;
86
+ rb_define_const(blinktm, "B1800", INT2FIX(B1800)) ;
87
+ rb_define_const(blinktm, "B2400", INT2FIX(B2400)) ;
88
+ rb_define_const(blinktm, "B4800", INT2FIX(B4800)) ;
89
+ rb_define_const(blinktm, "B9600", INT2FIX(B9600)) ;
90
+ rb_define_const(blinktm, "B19200", INT2FIX(B19200)) ;
91
+ rb_define_const(blinktm, "B38400", INT2FIX(B38400)) ;
92
+ rb_define_const(blinktm, "B57600", INT2FIX(B57600)) ;
93
+ rb_define_const(blinktm, "B115200", INT2FIX(B115200)) ;
94
+
95
+ rb_define_module_function(blinktm, "set_baudrate", setBaudRate, 2) ;
96
+
97
+ rb_define_module_function(blinktm, "get_baudrate", getBaudRate, 1) ;
98
+ rb_define_module_function(blinktm, "baudrate", getBaudRate, 1) ;
99
+ }
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile 'blink_tm/baudrate'
data/lib/blink_tm.rb ADDED
@@ -0,0 +1,33 @@
1
+ # Frozen_String_Literal: true
2
+ require 'blink_tm/baudrate'
3
+
4
+ module BlinkTM
5
+ # Important Constants
6
+ BAUDRATE = BlinkTM::B57600
7
+ SCANID = 'BTM'
8
+ POLLING = 0.375
9
+
10
+ # Refresh should always get subtracted with 0.05
11
+ REFRESH = 0.5 - 0.05
12
+
13
+ # Errors
14
+ NoDeviceError = Class.new(StandardError)
15
+ DeviceNotReady = Class.new(StandardError)
16
+
17
+ # Units
18
+ TB = 10 ** 12
19
+ GB = 10 ** 9
20
+ MB = 10 ** 6
21
+ KB = 10 ** 3
22
+
23
+ # ANSI Sequences
24
+ RED = "\e[38;2;225;79;67m"
25
+ BLUE = "\e[38;2;45;125;255m"
26
+ GREEN = "\e[38;2;40;175;95m"
27
+ ORANGE = "\e[38;2;245;155;20m"
28
+ BOLD = "\e[1m"
29
+ RESET = "\e[0m"
30
+ end
31
+
32
+ require 'blink_tm/version'
33
+ require 'blink_tm/blink_tm'
@@ -0,0 +1,223 @@
1
+
2
+ #!/usr/bin/ruby -w
3
+ # Frozen_String_Literal: true
4
+
5
+ require 'fcntl'
6
+ require 'linux_stat'
7
+
8
+ module BlinkTM
9
+ # Detect device
10
+ def find_device
11
+ dev = nil
12
+
13
+ Dir.glob('/sys/bus/usb/devices/*').each { |x|
14
+ v = File.join(x, 'idVendor')
15
+ vendor = IO.read(v).strip if File.readable?(v)
16
+
17
+ p = File.join(x, 'idProduct')
18
+ product = IO.read(p).strip if File.readable?(p)
19
+
20
+ if vendor == '1a86' && product == '7523'
21
+ puts "#{BOLD}#{GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: A potential device discovered: #{vendor}:#{product}#{RESET}"
22
+
23
+ Dir.glob('/dev/ttyUSB[0-9]*').each { |x|
24
+ if File.writable?(x)
25
+ puts "#{BlinkTM::BOLD}#{BlinkTM::BLUE}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Changing baudrate to 57600...#{BlinkTM::RESET}"
26
+
27
+ if BlinkTM.set_baudrate(x, BlinkTM::BAUDRATE)
28
+ puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Successfully Changed baudrate to 57600...#{BlinkTM::RESET}"
29
+ else
30
+ puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Cannot change the baudrate#{BlinkTM::RESET}"
31
+ end
32
+ else
33
+ "#{BOLD}#{RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: No permission granted to change Baudrate#{RESET}"
34
+ end
35
+
36
+ if File.readable?(x)
37
+ begin
38
+ if File.open(x).readpartial(30).to_s.scrub.include?("BTM")
39
+ puts "#{BOLD}#{ORANGE}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Multiple Blink Task "\
40
+ "Manager Hardware Found! "\
41
+ "Selecting: #{vendor}:#{product}#{RESET}" if dev
42
+
43
+ dev = x
44
+ end
45
+ rescue EOFError
46
+ sleep 0.125
47
+ retry
48
+ end
49
+ end
50
+ }
51
+ end
52
+ }
53
+
54
+ dev
55
+ end
56
+
57
+ # Convert Numeric bytes to the format that blink-taskmanager can read
58
+ def convert_bytes(n)
59
+ if n >= TB
60
+ "%06.2f".%(n.fdiv(TB)).split(?.).join + ?4
61
+ elsif n >= GB
62
+ "%06.2f".%(n.fdiv(GB)).split(?.).join + ?3
63
+ elsif n >= MB
64
+ "%06.2f".%(n.fdiv(MB)).split(?.).join + ?2
65
+ elsif n >= KB
66
+ "%06.2f".%(n.fdiv(KB)).split(?.).join + ?1
67
+ else
68
+ "%06.2f".%(n).split(?.).join + ?0
69
+ end
70
+ end
71
+
72
+ # Convert percentages to the format that blink-taskmanager can read
73
+ def convert_percent(n)
74
+ "%06.2f".%(n).split('.').join
75
+ end
76
+
77
+ def start(device)
78
+ return false unless device
79
+
80
+ cpu_u = mem_u = swap_u = iostat = net_u = net_d = 0
81
+ io_r = io_w = 0
82
+
83
+ Thread.new {
84
+ cpu_u = LS::CPU.total_usage(POLLING).to_f while true
85
+ }
86
+
87
+ Thread.new {
88
+ while true
89
+ netstat = LS::Net::current_usage(POLLING)
90
+ net_u = netstat[:transmitted].to_i
91
+ net_d = netstat[:received].to_i
92
+ end
93
+ }
94
+
95
+ Thread.new {
96
+ while true
97
+ io_stat1 = iostat()
98
+ sleep POLLING
99
+ io_stat2 = iostat()
100
+
101
+ io_r = POLLING * io_stat2[0].-(io_stat1[0])
102
+ io_w = POLLING * io_stat2[1].-(io_stat1[1])
103
+ end
104
+ }
105
+
106
+ begin
107
+ raise NoDeviceError unless device
108
+
109
+ in_sync = false
110
+ fd = IO.sysopen(device, Fcntl::O_RDWR | Fcntl::O_EXCL)
111
+ file = IO.open(fd)
112
+
113
+ until in_sync
114
+ # Clear out any extra zombie bits
115
+ file.syswrite(?~)
116
+ # Start the device
117
+ file.syswrite(?#)
118
+ file.flush
119
+
120
+ sleep 0.125
121
+
122
+ begin
123
+ if file.readpartial(8000).include?(?~)
124
+ in_sync = true
125
+ break
126
+ end
127
+ rescue EOFError
128
+ sleep 0.05
129
+ retry
130
+ end
131
+
132
+ sleep 0.05
133
+ end
134
+
135
+ puts "#{BlinkTM::BOLD}#{BlinkTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Device ready!#{BlinkTM::RESET}"
136
+
137
+ while true
138
+ # cpu(01234) memUsed(999993) swapUsed(999992) io_active(0)
139
+ # netUpload(999991) netDownload(999990)
140
+ # disktotal(999990) diskused(999990)
141
+
142
+ memstat = LS::Memory.stat
143
+ mem_u = memstat[:used].to_i.*(1024).*(100).fdiv(memstat[:total].to_i * 1024).round
144
+
145
+ swapstat = LS::Swap.stat
146
+ swap_u = swapstat[:used].to_i.*(1024).*(100).fdiv(swapstat[:total].to_i * 1024).round
147
+
148
+ # Output has to be exactly this long. If not, blink-taskmanager shows invalid result.
149
+ # No string is split inside blink-task manager, it just depends on the string length.
150
+ #
151
+ # cpu(100) memUsed(100) swapUsed(100)
152
+ # netDownload(9991) netUpload(9991)
153
+ # ioWrite(9991) ioRead(9991)
154
+
155
+ # Debugging string
156
+ # str = "#{"%03d" % cpu_u} #{"%03d" % mem_u} #{"%03d" % swap_u} "\
157
+ # "#{convert_bytes(net_u)} #{convert_bytes(net_d)} "\
158
+ # "#{convert_bytes(io_r)} #{convert_bytes(io_w)}"
159
+
160
+ str = "#{"%03d" % cpu_u}#{"%03d" % mem_u}#{"%03d" % swap_u}"\
161
+ "#{convert_bytes(net_u)}#{convert_bytes(net_d)}"\
162
+ "#{convert_bytes(io_r)}#{convert_bytes(io_w)}"
163
+
164
+ # Rescuing from suspend
165
+ file.syswrite('#')
166
+
167
+ file.syswrite('!')
168
+ file.flush
169
+ sleep 0.025
170
+
171
+ file.syswrite(str)
172
+ file.flush
173
+ sleep 0.025
174
+
175
+ file.syswrite('~')
176
+ file.flush
177
+
178
+ sleep REFRESH
179
+ end
180
+ rescue Interrupt, SystemExit, SignalException
181
+ file &.close
182
+ exit 0
183
+ rescue Errno::ENOENT, BlinkTM::NoDeviceError
184
+ file &.close
185
+ device = find_device
186
+
187
+ unless device
188
+ puts "#{BlinkTM::BOLD}#{BlinkTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Error establishing connection. Don't worry if this is a valid device. Retrying...#{BlinkTM::RESET}"
189
+ sleep 0.1
190
+ end
191
+
192
+ retry
193
+ rescue BlinkTM::DeviceNotReady
194
+ file &.close
195
+ sleep 0.1
196
+ retry
197
+ rescue Exception
198
+ puts $!.full_message
199
+ file &.close
200
+ sleep 0.1
201
+ retry
202
+ end
203
+ end
204
+
205
+ def iostat
206
+ @@root_partition ||= IO.foreach('/proc/mounts').detect {
207
+ |x| x.split[1] == ?/
208
+ }.to_s.split[0].to_s.split(?/).to_a[-1]
209
+
210
+ @@sector_size = LS::FS.stat(?/)[:block_size]
211
+
212
+ io_stat = IO.foreach('/proc/diskstats'.freeze).find { |x|
213
+ x.split[2] == @@root_partition
214
+ } &.split.to_a
215
+
216
+ _io_r = io_stat[5].to_f.*(@@sector_size).round
217
+ _io_w = io_stat[9].to_f.*(@@sector_size).round
218
+
219
+ [_io_r, _io_w]
220
+ end
221
+
222
+ extend(self)
223
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BlinkTM
4
+ VERSION = "0.1.1"
5
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blink_tm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sourav Goswami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: linux_stat
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.2
27
+ description: A controller for Arduino OLED System Monitor, Blink Task Manager
28
+ email:
29
+ - souravgoswami@protonmail.com
30
+ executables:
31
+ - blink-tm
32
+ extensions:
33
+ - ext/baudrate/extconf.rb
34
+ extra_rdoc_files: []
35
+ files:
36
+ - exe/blink-tm
37
+ - ext/baudrate/baudrate.c
38
+ - ext/baudrate/extconf.rb
39
+ - lib/blink_tm.rb
40
+ - lib/blink_tm/blink_tm.rb
41
+ - lib/blink_tm/version.rb
42
+ homepage: https://github.com/souravgoswami/blink-tm
43
+ licenses:
44
+ - MIT
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.6.0
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubygems_version: 3.2.15
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A controller for Arduino OLED System Monitor, Blink Task Manager
65
+ test_files: []