Skip to content

Commit

Permalink
add gpio mapper for atlas 200DK
Browse files Browse the repository at this point in the history
Signed-off-by: zhangyanhua <[email protected]>
  • Loading branch information
orsline committed Mar 20, 2023
1 parent c7d53e0 commit bd34b5f
Show file tree
Hide file tree
Showing 15 changed files with 1,168 additions and 0 deletions.
11 changes: 11 additions & 0 deletions mappers/gpio-atlas200/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM ubuntu:20.04
WORKDIR /usr/local/bin
COPY ./res /usr/local/res
COPY ./bin /usr/local/bin
#RUN echo "HwHiAiUser:x:1000:1000::/home/HwHiAiUser:" >> /etc/passwd ; \
#echo "HwHiAiUser:x:1024:" >> /etc/group ; \
#echo "HwHiAiUser:!:17795:0:99999:7:::" >> /etc/shadow ; \
#chown -R 1000:1000 /usr/local/bin; \
#chown -R 1000:1000 /usr/local/res;
USER 0
ENTRYPOINT ["./gpio","--v","4"]
36 changes: 36 additions & 0 deletions mappers/gpio-atlas200/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
SHELL := /bin/bash

curr_dir := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
rest_args := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
$(eval $(rest_args):;@:)

help:
#
# Usage:
# make template : create a mapper based on a template.
# make mapper {mapper-name} <action> <parameter>: execute mapper building process.
# make all : execute building process to all mappers.
#
# Actions:
# - mod, m : download code dependencies.
# - lint, l : verify code via go fmt and `golangci-lint`.
# - build, b : compile code.
# - package, p : package docker image.
# - test, t : run unit tests.
# - clean, c : clean output binary.
#
# Parameters:
# ARM : true or undefined
# ARM64 : true or undefined
#
# Example:
# - make mapper gpio ARM64=true : execute `build` "gpio" mapper for ARM64.
# - make mapper gpio test : execute `test` "gpio" mapper.
@echo

make_rules := $(shell ls $(curr_dir)/hack/make-rules | sed 's/.sh//g')
$(make_rules):
@$(curr_dir)/hack/make-rules/$@.sh $(rest_args)

.DEFAULT_GOAL := help
.PHONY: $(make_rules) build test package
12 changes: 12 additions & 0 deletions mappers/gpio-atlas200/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"github.com/kubeedge/mappers-go/mapper-sdk-go/pkg/service"
"github.com/kubeedge/mappers-go/mappers/gpio-atlas200/driver"
)

// main gpio device program entry
func main() {
gpio := &driver.GPIO{}
service.Bootstrap("GPIO", gpio)
}
60 changes: 60 additions & 0 deletions mappers/gpio-atlas200/crd_example/gpio-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: gpio-deploy-edge-line
spec:
replicas: 1
selector:
matchLabels:
app: gpio-line1
template:
metadata:
labels:
app: gpio-line1
spec:
hostNetwork: true
nodeSelector:
kubernetes.io/hostname: edge-a200-00
containers:
- name: gpio-container
image: gpio-mapper-arm64:v1.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 1215
hostPort: 1215
hostIP: 0.0.0.0
securityContext:
privileged: true
volumeMounts:
- name: config-volume
mountPath: /opt/kubeedge/
- name: gpio0-direction
mountPath: /sys/class/gpio/gpio504/direction
- name: gpio1-direction
mountPath: /sys/class/gpio/gpio444/direction
- name: gpio0-value
mountPath: /sys/class/gpio/gpio504/value
- name: gpio1-value
mountPath: /sys/class/gpio/gpio444/value
- name: i2c-path
mountPath: /dev/i2c-1
volumes:
- name: config-volume
configMap:
name: device-profile-config-edge-a200-00
- name: gpio0-direction
hostPath:
path: /sys/class/gpio/gpio504/direction
- name: gpio1-direction
hostPath:
path: /sys/class/gpio/gpio444/direction
- name: gpio0-value
hostPath:
path: /sys/class/gpio/gpio504/value
- name: gpio1-value
hostPath:
path: /sys/class/gpio/gpio444/value
- name: i2c-path
hostPath:
path: /dev/i2c-1
restartPolicy: Always
78 changes: 78 additions & 0 deletions mappers/gpio-atlas200/crd_example/gpio-device-instance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
apiVersion: devices.kubeedge.io/v1alpha2
kind: Device
metadata:
name: led-instance-01
labels:
model: led-model
spec:
deviceModelRef:
name: led-model
protocol:
customizedProtocol:
protocolName: GPIO
nodeSelector:
nodeSelectorTerms:
- matchExpressions:
- key: ''
operator: In
values:
- edge-a200-00 #pls give your edge node name
propertyVisitors:
- propertyName: red-power-status
collectCycle: 10000000000 #Mapper 从设备收集数据的频率
reportCycle: 1000000000 #Mapper 报告数据的频率
customizedProtocol:
protocolName: GPIO
configData:
pin: 0 #pls give your red light's Pin
- propertyName: green-power-status
collectCycle: 10000000000 #Mapper 从设备收集数据的频率
reportCycle: 1000000000 #Mapper 报告数据的频率
customizedProtocol:
protocolName: GPIO
configData:
pin: 1 #pls give your green light's Pin
- propertyName: yellow-power-status
collectCycle: 10000000000 #Mapper 从设备收集数据的频率
reportCycle: 1000000000 #Mapper 报告数据的频率
customizedProtocol:
protocolName: GPIO
configData:
pin: 3 #pls give your yellow light's Pin, pin 2 is not vaild for atlas200dk

status:
twins:
- propertyName: red-power-status
reported:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
desired:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
- propertyName: green-power-status
reported:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
desired:
metadata:
timestamp: '1550049403598'
type: string
value: "ON"
- propertyName: yellow-power-status
reported:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"
desired:
metadata:
timestamp: '1550049403598'
type: string
value: "OFF"

26 changes: 26 additions & 0 deletions mappers/gpio-atlas200/crd_example/gpio-device-model.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: devices.kubeedge.io/v1alpha2
kind: DeviceModel
metadata:
name: led-model
namespace: default
spec:
properties:
- name: red-power-status
description: red
type:
string:
accessMode: ReadWrite
defaultValue: "OFF"
- name: green-power-status
description: green
type:
string:
accessMode: ReadWrite
defaultValue: "OFF"
- name: yellow-power-status
description: yellow
type:
string:
accessMode: ReadWrite
defaultValue: "ON"

131 changes: 131 additions & 0 deletions mappers/gpio-atlas200/driver/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package driver

import (
"encoding/json"
"fmt"
"strings"
"sync"
)

// GPIOProtocolConfig is the protocol config structure.
type GPIOProtocolConfig struct {
ProtocolName string `json:"protocolName"`
ProtocolConfigData `json:"configData"`
}
// ProtocolConfigData is the protocol config data structure.
type ProtocolConfigData struct {
}

// GPIOProtocolCommonConfig is the protocol common config structure.
type GPIOProtocolCommonConfig struct {
CommonCustomizedValues `json:"customizedValues"`
}

// CommonCustomizedValues is the customized values structure.
type CommonCustomizedValues struct {
}
// GPIOVisitorConfig is the visitor config structure.
type GPIOVisitorConfig struct {
ProtocolName string `json:"protocolName"`
VisitorConfigData `json:"configData"`
}

// VisitorConfigData is the visitor config data structure.
type VisitorConfigData struct {
Pin int `json:"pin"`
}

// GPIO Realize the structure of random number
type GPIO struct {
mutex sync.Mutex
protocolConfig GPIOProtocolConfig
protocolCommonConfig GPIOProtocolCommonConfig
visitorConfig GPIOVisitorConfig
}

// InitDevice Sth that need to do in the first
// If you need mount a persistent connection, you should provide parameters in configmap's protocolCommon.
// and handle these parameters in the following function
func (d *GPIO) InitDevice(protocolCommon []byte) (err error) {
if protocolCommon != nil {
if err = json.Unmarshal(protocolCommon, &d.protocolCommonConfig); err != nil {
fmt.Printf("Unmarshal ProtocolCommonConfig error: %v\n", err)
return err
}
}
fmt.Println("GPIO devices do not need to be initialized")
return nil
}

// SetConfig Parse the configmap's raw json message
func (d *GPIO) SetConfig(protocolCommon, visitor, protocol []byte) (pin int, err error) {
d.mutex.Lock()
defer d.mutex.Unlock()
if protocolCommon != nil {
if err = json.Unmarshal(protocolCommon, &d.protocolCommonConfig); err != nil {
fmt.Printf("Unmarshal ProtocolCommonConfig error: %v\n", err)
return 0, err
}
}
if visitor != nil {
if err = json.Unmarshal(visitor, &d.visitorConfig); err != nil {
fmt.Printf("Unmarshal visitorConfig error: %v\n", err)
return 0, err
}
}
if protocol != nil {
if err = json.Unmarshal(protocol, &d.protocolConfig); err != nil {
fmt.Printf("Unmarshal ProtocolConfig error: %v\n", err)
return 0, err
}
}
return d.visitorConfig.Pin, nil
}

// ReadDeviceData is an interface that reads data from a specific device, data is a type of string
func (d *GPIO) ReadDeviceData(protocolCommon, visitor, protocol []byte) (data interface{}, err error) {
// Parse raw json message to get a virtualDevice instance
pin, err := d.SetConfig(protocolCommon, visitor, protocol)
if err != nil {
return nil, err
}
pinClient := Pin(pin)
if pinClient.Read() == '0' {
return "OFF", nil
}
return "ON", nil
}

// WriteDeviceData is an interface that write data to a specific device, data's DataType is Consistent with configmap
func (d *GPIO) WriteDeviceData(data interface{}, protocolCommon, visitor, protocol []byte) (err error) {
// Parse raw json message to get a virtualDevice instance
pin, err := d.SetConfig(protocolCommon, visitor, protocol)
if err != nil {
return err
}
status := data.(string)
pinClient := Pin(pin)
if strings.ToUpper(status) == "OFF" {
pinClient.SetOutPut()
pinClient.SetLow()
} else if strings.ToUpper(status) == "ON" {
pinClient.SetOutPut()
pinClient.SetHight()
} else {
fmt.Println("the command should be \"ON\" or \"OFF\"")
}
return nil
}

// StopDevice is an interface to disconnect a specific device
// This function is called when mapper stops serving
func (d *GPIO) StopDevice() (err error) {
// in this func, u can get ur device-instance in the client map, and give a safety exit
fmt.Println("----------Stop gpio Device Successful----------")
return nil
}

// GetDeviceStatus is an interface to get the device status true is OK , false is DISCONNECTED
func (d *GPIO) GetDeviceStatus(protocolCommon, visitor, protocol []byte) (status bool) {
return true
}
Loading

0 comments on commit bd34b5f

Please sign in to comment.