ice_tm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4b75ddb028fb7f91ece76d947e7baaf49ca49834e94fd8eb511dc8bc358d320f
4
+ data.tar.gz: 102c2e59d34635e2dcdd5518e5acda17cc33c16e26eec46dff5d7e09c924c411
5
+ SHA512:
6
+ metadata.gz: 7ebe5c6589697b31a5bf8b7d158bc99220eb043807720223556e4236544f8d5ce52cdcd61756a619dab0f3daa9f921c8ff0cf4c984b3cc416d21edf3d6d610f7
7
+ data.tar.gz: 41e1f87dd7ed6259ef5fd58768bbfad923c42060d036c3f35f2bd1bc651ca906d3cf7a4c383b3f701883e3799fb5cf02c78ddca6de3698cb85134392c89221f3
data/exe/ice-tm ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ice_tm'
3
+
4
+ # Find Device running Ice Taskmanager
5
+ dev = nil
6
+ retry_count = 0
7
+
8
+ while(!dev)
9
+ dev = IceTM.find_device
10
+
11
+ if dev
12
+ puts "#{IceTM::BOLD}#{IceTM::GREEN}:: Device discovered successfully. Path: #{dev}#{IceTM::RESET}"
13
+ else
14
+ puts "#{IceTM::BOLD}#{IceTM::RED}:: No device found. Retrying #{retry_count += 1}#{IceTM::RESET}"
15
+ sleep 0.5
16
+ end
17
+ end
18
+
19
+ IceTM.start(dev)
@@ -0,0 +1,94 @@
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
+ // IDE sets these flags
16
+ // speed 57600 baud; line = 0;
17
+ // min = 0; time = 0;
18
+ // -brkint -icrnl -imaxbel
19
+ // -opost
20
+ // -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
21
+
22
+ if(status != 0) return Qnil ;
23
+ tty.c_cflag &= ~CSIZE ;
24
+ tty.c_cflag |= CS8 ;
25
+ tty.c_cflag |= CREAD | CLOCAL ;
26
+ tty.c_oflag &= ~OPOST ;
27
+
28
+ tty.c_lflag &= ~ECHO ;
29
+ tty.c_lflag &= ~ECHOCTL ;
30
+ tty.c_lflag &= ~ECHOK ;
31
+ tty.c_lflag &= ~ECHOE ;
32
+ tty.c_lflag &= ~ECHOKE ;
33
+ tty.c_lflag &= ~ISIG ;
34
+ tty.c_lflag &= ~ICRNL ;
35
+ tty.c_lflag &= ~IEXTEN ;
36
+ tty.c_lflag &= ~ICANON ;
37
+
38
+ tty.c_cc[VTIME] = 0 ;
39
+ tty.c_cc[VMIN] = 0 ;
40
+
41
+ cfsetispeed(&tty, spd) ;
42
+ cfsetospeed(&tty, spd) ;
43
+ status = tcsetattr(serial_port, TCSANOW, &tty) ;
44
+
45
+ if (status == 0) return Qtrue ;
46
+ else return Qfalse ;
47
+ }
48
+
49
+ VALUE getBaudRate(volatile VALUE obj, volatile VALUE dev) {
50
+ char *device = StringValuePtr(dev) ;
51
+
52
+ int serial_port = open(device, O_RDWR) ;
53
+ struct termios tty ;
54
+
55
+ char status = tcgetattr(serial_port, &tty) ;
56
+
57
+ if(status == 0) {
58
+ unsigned int in = cfgetispeed(&tty) ;
59
+ unsigned int out = cfgetospeed(&tty) ;
60
+
61
+ return rb_ary_new_from_args(2,
62
+ UINT2NUM(in), UINT2NUM(out)
63
+ ) ;
64
+ }
65
+
66
+ return rb_ary_new() ;
67
+ }
68
+
69
+ void Init_baudrate() {
70
+ VALUE icetm = rb_define_module("IceTM") ;
71
+ rb_define_const(icetm, "B0", INT2FIX(B0)) ;
72
+ rb_define_const(icetm, "B50", INT2FIX(B50)) ;
73
+ rb_define_const(icetm, "B75", INT2FIX(B75)) ;
74
+ rb_define_const(icetm, "B110", INT2FIX(B110)) ;
75
+ rb_define_const(icetm, "B134", INT2FIX(B134)) ;
76
+ rb_define_const(icetm, "B150", INT2FIX(B150)) ;
77
+ rb_define_const(icetm, "B200", INT2FIX(B200)) ;
78
+ rb_define_const(icetm, "B300", INT2FIX(B300)) ;
79
+ rb_define_const(icetm, "B600", INT2FIX(B600)) ;
80
+ rb_define_const(icetm, "B1200", INT2FIX(B1200)) ;
81
+ rb_define_const(icetm, "B1800", INT2FIX(B1800)) ;
82
+ rb_define_const(icetm, "B2400", INT2FIX(B2400)) ;
83
+ rb_define_const(icetm, "B4800", INT2FIX(B4800)) ;
84
+ rb_define_const(icetm, "B9600", INT2FIX(B9600)) ;
85
+ rb_define_const(icetm, "B19200", INT2FIX(B19200)) ;
86
+ rb_define_const(icetm, "B38400", INT2FIX(B38400)) ;
87
+ rb_define_const(icetm, "B57600", INT2FIX(B57600)) ;
88
+ rb_define_const(icetm, "B115200", INT2FIX(B115200)) ;
89
+
90
+ rb_define_module_function(icetm, "set_baudrate", setBaudRate, 2) ;
91
+
92
+ rb_define_module_function(icetm, "get_baudrate", getBaudRate, 1) ;
93
+ rb_define_module_function(icetm, "baudrate", getBaudRate, 1) ;
94
+ }
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile 'ice_tm/baudrate'
data/lib/ice_tm.rb ADDED
@@ -0,0 +1,25 @@
1
+ # Frozen_String_Literal: true
2
+ require 'ice_tm/baudrate'
3
+
4
+ module IceTM
5
+ # Errors
6
+ NoDeviceError = Class.new(StandardError)
7
+
8
+ # Other Important Constants
9
+ BAUDRATE = IceTM::B57600
10
+
11
+ TB = 10 ** 12
12
+ GB = 10 ** 9
13
+ MB = 10 ** 6
14
+ KB = 10 ** 3
15
+
16
+ RED = "\e[38;2;225;79;67m"
17
+ BLUE = "\e[38;2;45;125;255m"
18
+ GREEN = "\e[38;2;40;175;95m"
19
+ ORANGE = "\e[38;2;245;155;20m"
20
+ BOLD = "\e[1m"
21
+ RESET = "\e[0m"
22
+ end
23
+
24
+ require 'ice_tm/version'
25
+ require 'ice_tm/ice_tm'
@@ -0,0 +1,208 @@
1
+
2
+ #!/usr/bin/ruby -w
3
+ # Frozen_String_Literal: true
4
+
5
+ require 'fcntl'
6
+ require 'linux_stat'
7
+
8
+ module IceTM
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 "#{IceTM::BOLD}#{IceTM::BLUE}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Changing `baudrate` to 57600...#{IceTM::RESET}"
26
+ IceTM.set_baudrate(x, IceTM::BAUDRATE)
27
+ else
28
+ "#{BOLD}#{RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: No permission granted to change Baudrate#{RESET}"
29
+ end
30
+
31
+ if File.readable?(x)
32
+ 1000.times {
33
+ if File.open(x).gets.to_s.scrub.strip.include?("IceTM")
34
+ puts "#{BOLD}#{ORANGE}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Multiple Ice Task "\
35
+ "Manager Hardware Found! "\
36
+ "Selecting: #{vendor}:#{product}#{RESET}" if dev
37
+
38
+ dev = x
39
+ break
40
+ end
41
+ }
42
+ end
43
+ }
44
+ end
45
+ }
46
+
47
+ dev
48
+ end
49
+
50
+ # Convert Numeric bytes to the format that ice-taskmanager can read
51
+ def convert_bytes(n)
52
+ if n >= TB
53
+ "%06.2f".%(n.fdiv(TB)).split('.').join + ?4
54
+ elsif n >= GB
55
+ "%06.2f".%(n.fdiv(GB)).split('.').join + ?3
56
+ elsif n >= MB
57
+ "%06.2f".%(n.fdiv(MB)).split('.').join + ?2
58
+ elsif n >= KB
59
+ "%06.2f".%(n.fdiv(KB)).split('.').join + ?1
60
+ else
61
+ "%06.2f".%(n).split('.').join + ?0
62
+ end
63
+ end
64
+
65
+ # Convert percentages to the format that ice-taskmanager can read
66
+ def convert_percent(n)
67
+ "%06.2f".%(n).split('.').join
68
+ end
69
+
70
+ def start(device)
71
+ return false if(!device)
72
+
73
+ cpu_u = mem_u = swap_u = iostat = net_upload = net_download = 0
74
+
75
+ Thread.new {
76
+ while true
77
+ cpu_u = LS::CPU.total_usage(0.125).to_f
78
+ end
79
+ }
80
+
81
+ Thread.new {
82
+ while true
83
+ netstat = LS::Net::current_usage(0.1)
84
+ net_upload = netstat[:transmitted].to_i
85
+ net_download = netstat[:received].to_i
86
+ end
87
+ }
88
+
89
+ begin
90
+ in_sync = false
91
+ fd = IO.sysopen(device, Fcntl::O_RDWR | Fcntl::O_EXCL)
92
+ file = IO.open(fd)
93
+
94
+ until in_sync
95
+ file.syswrite(?! * 1000)
96
+ STDOUT.flush
97
+
98
+ begin
99
+ if file.readpartial(8000).include?(?~)
100
+ in_sync = true
101
+ break
102
+ end
103
+ rescue EOFError
104
+ retry
105
+ end
106
+ end
107
+
108
+ puts "#{IceTM::BOLD}#{IceTM::GREEN}:: #{Time.now.strftime('%H:%M:%S.%2N')}: Device ready!#{IceTM::RESET}"
109
+
110
+ while true
111
+ # cpu(01234) memUsed(999993) swapUsed(999992) io_active(0)
112
+ # netUpload(999991) netDownload(999990)
113
+ # disktotal(999990) diskused(999990)
114
+
115
+ memstat = LS::Memory.stat
116
+ mem_t = convert_bytes(memstat[:total].to_i.*(1000))
117
+ mem_u = convert_bytes(memstat[:used].to_i.*(1000))
118
+ mem_percent = convert_percent(memstat[:used].to_i.*(100).fdiv(memstat[:total].to_i))
119
+
120
+ swapstat = LS::Swap.stat
121
+ swap_u = convert_bytes(swapstat[:used].to_i.*(1000))
122
+ swap_percent = convert_percent(swapstat[:used].to_i.*(100).fdiv(swapstat[:total].to_i))
123
+
124
+ diskstat = LS::Filesystem.stat
125
+ disk_total = convert_bytes(diskstat[:total])
126
+ disk_used = convert_bytes(diskstat[:used])
127
+ _diskavail = LS::Filesystem.available
128
+ disk_avail = convert_bytes(_diskavail)
129
+
130
+ disk_used_percent = convert_percent(diskstat[:used].*(100).fdiv(diskstat[:total]))
131
+ disk_avail_percent = convert_percent(_diskavail.*(100).fdiv(diskstat[:total]))
132
+
133
+ total_process = LS::Process.count
134
+ ps_r = LS::Process.running.count
135
+ ps_sl = LS::Process.sleeping.count
136
+ ps_i = LS::Process.idle.count
137
+ ps_t = LS::Process.stopped.count
138
+ ps_z = LS::Process.zombie.count
139
+
140
+ # Output has to be exactly this long. If not, ice-taskmanager shows invalid result.
141
+ # No string is split inside ice-task manager, it just depends on the string length.
142
+ #
143
+ # cpu(01234) memUsed(999993) swapUsed(999992) io_active(0)
144
+ # netUpload(999991) netDownload(999990)
145
+ # disktotal(999990) diskused(999990) diskAvail(999990) totalProcess(12345678)
146
+ # memPercent(01234) swapPercent(01234) diskPercent(01234) diskAvailPercent(01234)
147
+ # psR(65536) psS(65536) psI(65536) psT(65536) psZ(65536)
148
+
149
+ # Array for better debugging
150
+ # a = [
151
+ # convert_percent(cpu_u),
152
+ # mem_u, swap_u, iostat(),
153
+ # convert_bytes(net_upload), convert_bytes(net_download),
154
+ # disk_total, disk_used, disk_avail,
155
+ # "%08d" % total_process,
156
+ # mem_percent, swap_percent, disk_used_percent, disk_avail_percent,
157
+ # "%05d" % ps_r, "%05d" % ps_sl, "%05d" % ps_i, "%05d" % ps_t, "%05d" % ps_z
158
+ # ]
159
+
160
+ str = "#{convert_percent(cpu_u)}#{mem_u}#{swap_u}#{iostat()}"\
161
+ "#{convert_bytes(net_upload)}#{convert_bytes(net_download)}"\
162
+ "#{disk_total}#{disk_used}#{disk_avail}#{"%08d" % total_process}"\
163
+ "#{mem_percent}#{swap_percent}#{disk_used_percent}#{disk_avail_percent}"\
164
+ "#{"%05d" % ps_r}#{"%05d" % ps_sl}#{"%05d" % ps_i}#{"%05d" % ps_t}#{"%05d" % ps_z}"
165
+
166
+ file.syswrite('!')
167
+ STDOUT.flush
168
+ sleep 0.0125
169
+ file.syswrite(str)
170
+ STDOUT.flush
171
+ sleep 0.0125
172
+ file.syswrite('~')
173
+ STDOUT.flush
174
+
175
+ sleep 0.1
176
+ end
177
+ rescue Interrupt, SystemExit, SignalException
178
+ file &.close
179
+ exit 0
180
+ rescue Errno::ENOENT, IceTM::NoDeviceError
181
+ device = find_device
182
+
183
+ unless device
184
+ puts "#{IceTM::BOLD}#{IceTM::RED}:: #{Time.now.strftime('%H:%M:%S.%2N')}: No device found. Retrying#{IceTM::RESET}"
185
+ sleep 0.1
186
+ end
187
+
188
+ retry
189
+ rescue Exception
190
+ puts $!.full_message
191
+ file &.close
192
+ sleep 0.1
193
+ retry
194
+ end
195
+ end
196
+
197
+ def iostat
198
+ @@root_partition ||= IO.foreach('/proc/mounts').detect {
199
+ |x| x.split[1] == ?/
200
+ }.to_s.split[0].to_s.split(?/).to_a[-1]
201
+
202
+ iostat = IO.foreach('/proc/diskstats'.freeze).find { |x|
203
+ x.split[2] == @@root_partition
204
+ } &.split.to_a[11]
205
+ end
206
+
207
+ extend(self)
208
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IceTM
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ice_tm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sourav Goswami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-23 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, ICE Task Manager
28
+ email:
29
+ - souravgoswami@protonmail.com
30
+ executables:
31
+ - ice-tm
32
+ extensions:
33
+ - ext/baudrate/extconf.rb
34
+ extra_rdoc_files: []
35
+ files:
36
+ - exe/ice-tm
37
+ - ext/baudrate/baudrate.c
38
+ - ext/baudrate/extconf.rb
39
+ - lib/ice_tm.rb
40
+ - lib/ice_tm/ice_tm.rb
41
+ - lib/ice_tm/version.rb
42
+ homepage: https://github.com/souravgoswami/ice-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, ICE Task Manager
65
+ test_files: []