-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GPIO control buttons to turn on and off the lights (#172)
* Start gpio storage * Start Gpio config visual * Add updatePin function * Continue Gpio pin visual * Add custom icon button * Subscribe to topic state * Fix topic names and message types * Remove subscriber * Update pin names * Fix confusing type names
- Loading branch information
Showing
9 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { styled } from '@/renderer/globalStyles/styled'; | ||
import React, { ReactNode } from 'react'; | ||
|
||
interface IconButtonProps { | ||
icon: ReactNode; | ||
onClick: () => void; | ||
title?: string; | ||
style?: React.CSSProperties; | ||
} | ||
|
||
const IconContainer = styled.div` | ||
display: flex; | ||
flex-direction: row; | ||
align-items: center; | ||
justify-content: center; | ||
`; | ||
|
||
const Button = styled.button` | ||
background-color: ${({ theme }) => theme.colors.darkerBackground}; | ||
border-radius: 4px; | ||
padding: 6px; | ||
color: white; | ||
border: none; | ||
background: linear-gradient(#0000, rgb(0 0 0/30%)) top/100% 800%; | ||
transition: background-position 0.2s ease-in-out; | ||
&:hover { | ||
background-position: bottom; | ||
} | ||
`; | ||
|
||
const IconButton = ({ icon, onClick, title, style }: IconButtonProps) => { | ||
return ( | ||
<Button style={style} onClick={onClick}> | ||
<IconContainer> | ||
{icon} | ||
{title && <span style={{ marginLeft: '4px' }}>{title}</span>} | ||
</IconContainer> | ||
</Button> | ||
); | ||
}; | ||
|
||
export default IconButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/renderer/components/pages/Config/pages/GpioPinsConfig/GpioPin.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import IconButton from '@/renderer/components/common/IconButton'; | ||
import { styled } from '@/renderer/globalStyles/styled'; | ||
import { GpioPinState, gpioPinsSlice } from '@/renderer/store/modules/gpioPins'; | ||
import { rosClient } from '@/renderer/utils/ros/rosClient'; | ||
import { TopicOptions } from '@/renderer/utils/ros/roslib-ts-client/@types'; | ||
import React, { useMemo } from 'react'; | ||
import { FaPowerOff } from 'react-icons/fa'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
interface GpioPinProps { | ||
gpioPin: GpioPinState; | ||
} | ||
|
||
const Card = styled.div` | ||
background-color: ${({ theme }) => theme.colors.darkerBackground}; | ||
border-radius: 4px; | ||
padding: 16px; | ||
margin: 8px; | ||
min-width: 150px; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: flex-start; | ||
align-items: flex-start; | ||
box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.5); | ||
`; | ||
|
||
export const GpioPin = ({ gpioPin }: GpioPinProps) => { | ||
const dispatch = useDispatch(); | ||
|
||
const topic: TopicOptions = useMemo( | ||
() => ({ | ||
name: gpioPin.topicName, | ||
messageType: 'std_msgs/UInt16', | ||
}), | ||
[gpioPin.topicName] | ||
); | ||
|
||
const togglePin = () => { | ||
rosClient.publish(topic, { data: gpioPin.isOn ? 0 : 1 }); | ||
|
||
dispatch(gpioPinsSlice.actions.togglePin(gpioPin.id)); | ||
}; | ||
|
||
return ( | ||
<Card> | ||
<h3 style={{ paddingBottom: '4px' }}>{gpioPin.name}</h3> | ||
<IconButton | ||
icon={<FaPowerOff />} | ||
title={gpioPin.isOn ? 'Power Off' : 'Power On'} | ||
onClick={togglePin} | ||
style={ | ||
gpioPin.isOn | ||
? { backgroundColor: 'red' } | ||
: { backgroundColor: 'green' } | ||
} | ||
/> | ||
</Card> | ||
); | ||
}; |
26 changes: 26 additions & 0 deletions
26
src/renderer/components/pages/Config/pages/GpioPinsConfig/GpioPinsConfig.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { SectionTitle } from '../../styles'; | ||
import { useSelector } from 'react-redux'; | ||
import { selectAllGpioPins } from '@/renderer/store/modules/gpioPins'; | ||
import { GpioPin } from './GpioPin'; | ||
import { styled } from '@/renderer/globalStyles/styled'; | ||
|
||
const Container = styled.div` | ||
display: flex; | ||
flex-direction: row; | ||
align-items: flex-start; | ||
`; | ||
|
||
export const GpioPinsConfig = () => { | ||
const gpioPins = useSelector(selectAllGpioPins); | ||
return ( | ||
<> | ||
<SectionTitle>GPIO Control</SectionTitle> | ||
<Container> | ||
{gpioPins.map((gpioPin) => ( | ||
<GpioPin key={gpioPin.id} gpioPin={gpioPin} /> | ||
))} | ||
</Container> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { GlobalState } from '@/renderer/store/store'; | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
import { nanoid } from 'nanoid'; | ||
|
||
export interface GpioPinState { | ||
id: string; | ||
name: string; | ||
topicName: string; | ||
isOn: boolean; | ||
} | ||
|
||
export const initialState: GpioPinState[] = [ | ||
{ | ||
id: nanoid(), | ||
name: 'FRONT LED', | ||
topicName: '/DOP1', | ||
isOn: false, | ||
}, | ||
{ | ||
id: nanoid(), | ||
name: 'BACK LED', | ||
topicName: '/DOP2', | ||
isOn: false, | ||
}, | ||
{ | ||
id: nanoid(), | ||
name: 'ARM LED', | ||
topicName: '/DOP3', | ||
isOn: false, | ||
}, | ||
]; | ||
|
||
export const gpioPinsSlice = createSlice({ | ||
name: 'gpioPins', | ||
initialState, | ||
reducers: { | ||
togglePin: (state, { payload }: PayloadAction<string>) => { | ||
const element = state.find((element) => element.id === payload); | ||
if (element) { | ||
element.isOn = !element.isOn; | ||
} | ||
}, | ||
addPin: (state, { payload }: PayloadAction<GpioPinState>) => { | ||
state.push(payload); | ||
}, | ||
updatePin: (state, { payload }: PayloadAction<GpioPinState>) => { | ||
const element = state.find((element) => element.id === payload.id); | ||
if (element) { | ||
element.name = payload.name; | ||
element.topicName = payload.topicName; | ||
} | ||
}, | ||
updateIsOn: ( | ||
state, | ||
{ payload }: PayloadAction<{ id: string; isOn: boolean }> | ||
) => { | ||
const element = state.find((element) => element.id === payload.id); | ||
if (element) { | ||
element.isOn = payload.isOn; | ||
} | ||
}, | ||
removePin: (state, { payload }: PayloadAction<GpioPinState>) => { | ||
state = state.filter((pin) => pin.id !== payload.id); | ||
}, | ||
}, | ||
}); | ||
|
||
export const selectAllGpioPins = (state: GlobalState) => state.gpioPins; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters