Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

201 can errors cli viewer #316

Merged
merged 22 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
52f21d4
Moved files to new branch
sokosam Nov 6, 2024
a421af2
error description added with temp description
sokosam Nov 7, 2024
267814e
Fixed bug with bits
sokosam Nov 7, 2024
be9eb9b
minor error with submenu when opening description
sokosam Nov 8, 2024
df36d5d
new description features added
sokosam Nov 8, 2024
9ac02de
finished: ~10. Make cursor move to the ignored menu when the ignored…
sokosam Nov 11, 2024
4ef003d
refactored some of the code to reduce redundancies, added comments to…
sokosam Nov 12, 2024
d9b345a
small fix to description box showing the main table description when …
sokosam Nov 12, 2024
648dd81
added structs to store data and changed functionality to implement, m…
sokosam Nov 17, 2024
f9eed6a
Removed last instance of findRowString and added cli flag for canInte…
sokosam Nov 17, 2024
261c523
Key behavior attached to keymap
sokosam Nov 17, 2024
4a149b6
Updated the README.md
sokosam Nov 17, 2024
c6681c9
fixed findError when done after sort
sokosam Nov 17, 2024
7861322
Refactored code. Added metatable, modified table.go to keep track of …
sokosam Nov 19, 2024
cf9da83
remove redundant function
sokosam Nov 19, 2024
7dcc3a3
Small fix in readme and further implemented isolated logic
sokosam Nov 19, 2024
eba8ea8
other small fixes
sokosam Nov 19, 2024
dc38cfb
Removed redundant comments, changed some field/function names to be m…
sokosam Nov 19, 2024
8ad6e9f
remove submodule
sokosam Nov 19, 2024
af17608
Undo submodule change
BlakeFreer Nov 20, 2024
3cba992
Program now exits gracefully when an error occurs, setup_vcan now req…
sokosam Nov 20, 2024
a8e255c
remove submodule
sokosam Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions scripts/can_errgo/README.md
BlakeFreer marked this conversation as resolved.
Show resolved Hide resolved
BlakeFreer marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
# To test the virtual can and go script
# CAN Error TUI

This is a Tangible User Interface program designed to track CAN Errors including their count, recency, description, and name.

Users can:

- Acknowledge the error ( Hide and resets the error until it is sent again)
- Ignore the error ( Hides, resets, and stops reading all future occurrences of the error)
- View ignored errors and choose to unignore those errors

## Setup

Install required dependencies:

```bash
sudo apt-get update
sudo apt-get upgrade
sudo apt install net-tools iproute2 can-utils linux-modules-extra-$(uname -r)
```

Kill all existing instances of ip-links:

`ip link show`
`sudo ip link delete <ip-link>`
```bash
ip link show
sudo ip link delete <ip-link>
```

Setup the can network:

`./setup_vcan.sh <CAN_PORT>`
```bash
./setup_vcan.sh <CAN_PORT>
```

Start the CLI viewer:
`go run error_tui.go -i <CAN_PORT>`
## Usage

Start the CLI viewer for single use:

```bash
go run error_tui.go -i <CAN_PORT> -w <WARN_TIMER>
```

OR

Build the go file:

```bash
go build -o <BUILD_NAME>
./<BUILD_NAME> -i <CAN_PORT> -w <WARN_TIMER>
```

You can now run:
`cansend <CAN_PORT> <MSG_ID>#<MSG_DATA>`

```bash
cansend <CAN_PORT> <MSG>
```
84 changes: 41 additions & 43 deletions scripts/can_errgo/components/table.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package components

// Modified from "github.com/charmbracelet/bubbles/table"
// Repurposed for use in CAN error handling application

import (
"strings"

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/viewport"
Expand All @@ -13,18 +12,25 @@ import (
"github.com/mattn/go-runewidth"
)

// MetaRow is the implementation of a Error Row with its corresponding error index within the 64-bit CAN frame
type MetaRow struct{
Row Row
Index int
}

// Model defines a state for the table widget.
type Model struct {
KeyMap KeyMap
Help help.Model

cols []Column
rows []Row
metaRow []MetaRow
BlakeFreer marked this conversation as resolved.
Show resolved Hide resolved
cursor int
focus bool
styles Styles
ShowCursor bool


viewport viewport.Model
start int
end int
Expand Down Expand Up @@ -160,10 +166,10 @@ func WithColumns(cols []Column) Option {
}
}

// WithRows sets the table rows (data).
func WithRows(rows []Row) Option {
//WithRows sets the table rows (data).
func WithRows(rows []MetaRow) Option {
return func(m *Model) {
m.rows = rows
m.metaRow = rows
}
}

Expand Down Expand Up @@ -268,7 +274,7 @@ func (m Model) HelpView() string {
// UpdateViewport updates the list content based on the previously defined
// columns and rows.
func (m *Model) UpdateViewport() {
renderedRows := make([]string, 0, len(m.rows))
renderedRows := make([]string, 0, len(m.metaRow))

// Render only rows from: m.cursor-m.viewport.Height to: m.cursor+m.viewport.Height
// Constant runtime, independent of number of rows in a table.
Expand All @@ -278,7 +284,7 @@ func (m *Model) UpdateViewport() {
} else {
m.start = 0
}
m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.rows))
m.end = clamp(m.cursor+m.viewport.Height, m.cursor, len(m.metaRow))
for i := m.start; i < m.end; i++ {
renderedRows = append(renderedRows, m.renderRow(i))
}
Expand All @@ -291,26 +297,34 @@ func (m *Model) UpdateViewport() {
// SelectedRow returns the selected row.
// You can cast it to your own implementation.
func (m Model) SelectedRow() Row {
if m.cursor < 0 || m.cursor >= len(m.rows) {
if m.cursor < 0 || m.cursor >= len(m.metaRow) {
return nil
}

return m.rows[m.cursor]
return m.metaRow[m.cursor].Row
}

// SelectedIndex returns the Error Index of the selected row.
func (m Model) SelectedIndex() int {
if m.cursor < 0 || m.cursor >= len(m.metaRow) {
return -1
}
return m.metaRow[m.cursor].Index
}

// Rows returns the current rows.
func (m Model) Rows() []Row {
return m.rows
// Rows returns the current metaRow.
func (m Model) Rows() []MetaRow {
return m.metaRow
}

// Columns returns the current columns.
func (m Model) Columns() []Column {
return m.cols
}

// SetRows sets a new rows state.
func (m *Model) SetRows(r []Row) {
m.rows = r
// SetRows sets a new metaRow state.
func (m *Model) SetRows(r []MetaRow) {
m.metaRow = r
m.UpdateViewport()
}

Expand Down Expand Up @@ -350,23 +364,23 @@ func (m Model) Cursor() int {
// ForceCursor forces the cursor to a specific row, regardless if the row is out of bounds.
func (m *Model) ForceCursor(n int) {
// Clamp the cursor position within bounds
m.cursor = clamp(n, 0, len(m.rows)-1)
m.cursor = clamp(n, 0, len(m.metaRow)-1)
m.UpdateViewport()
}

// SetCursor sets the cursor position in the table.
func (m *Model) SetCursor(n int) {
m.cursor = clamp(n, 0, len(m.rows)-1)
m.cursor = clamp(n, 0, len(m.metaRow)-1)
m.UpdateViewport()
}
// SetCursor sets the cursor position in the table.
func (m *Model) SetCursorAndViewport(n int) {
m.cursor = clamp(n, 0, len(m.rows)-1)
m.cursor = clamp(n, 0, len(m.metaRow)-1)

switch {
case m.start == 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor))
case m.end == len(m.rows) && m.cursor > m.viewport.Height:
case m.end == len(m.metaRow) && m.cursor > m.viewport.Height:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.viewport.Height))
}

Expand All @@ -378,10 +392,10 @@ func (m *Model) SetShowCursor(show bool) {
m.UpdateViewport()
}

// MoveUp moves the selection up by any number of rows.
// MoveUp moves the selection up by any number of metaRow.
// It can not go above the first row.
func (m *Model) MoveUp(n int) {
m.cursor = clamp(m.cursor-n, 0, len(m.rows)-1)
m.cursor = clamp(m.cursor-n, 0, len(m.metaRow)-1)
switch {
case m.start == 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset, 0, m.cursor))
Expand All @@ -393,14 +407,14 @@ func (m *Model) MoveUp(n int) {
m.UpdateViewport()
}

// MoveDown moves the selection down by any number of rows.
// MoveDown moves the selection down by any number of metaRow.
// It can not go below the last row.
func (m *Model) MoveDown(n int) {
m.cursor = clamp(m.cursor+n, 0, len(m.rows)-1)
m.cursor = clamp(m.cursor+n, 0, len(m.metaRow)-1)
m.UpdateViewport()

switch {
case m.end == len(m.rows) && m.viewport.YOffset > 0:
case m.end == len(m.metaRow) && m.viewport.YOffset > 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.viewport.Height))
case m.cursor > (m.end-m.start)/2 && m.viewport.YOffset > 0:
m.viewport.SetYOffset(clamp(m.viewport.YOffset-n, 1, m.cursor))
Expand All @@ -417,23 +431,7 @@ func (m *Model) GotoTop() {

// GotoBottom moves the selection to the last row.
func (m *Model) GotoBottom() {
m.MoveDown(len(m.rows))
}

// FromValues create the table rows from a simple string. It uses `\n` by
// default for getting all the rows and the given separator for the fields on
// each row.
func (m *Model) FromValues(value, separator string) {
rows := []Row{}
for _, line := range strings.Split(value, "\n") {
r := Row{}
for _, field := range strings.Split(line, separator) {
r = append(r, field)
}
rows = append(rows, r)
}

m.SetRows(rows)
m.MoveDown(len(m.metaRow))
}

func (m Model) headersView() string {
Expand All @@ -451,7 +449,7 @@ func (m Model) headersView() string {

func (m *Model) renderRow(r int) string {
s := make([]string, 0, len(m.cols))
for i, value := range m.rows[r] {
for i, value := range m.metaRow[r].Row {
if m.cols[i].Width <= 0 {
continue
}
Expand Down
Loading
Loading