-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
executable file
·99 lines (86 loc) · 2.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
Go Language Raspberry Pi Interface
(c) Copyright David Thorpe 2016-2018
All Rights Reserved
Documentation http://djthorpe.github.io/gopi/
For Licensing and Usage information, please see LICENSE.md
*/
// MMAL example to connect camera to screen and render the camera
// image
package main
import (
"errors"
"fmt"
"os"
// Frameworks
gopi "github.com/djthorpe/gopi"
hw "github.com/djthorpe/gopi-hw"
// Modules
_ "github.com/djthorpe/gopi-hw/sys/hw"
_ "github.com/djthorpe/gopi-hw/sys/mmal"
_ "github.com/djthorpe/gopi/sys/logger"
)
////////////////////////////////////////////////////////////////////////////////
func RendererInputPort(mmal hw.MMAL) (hw.MMALPort, error) {
if renderer, err := mmal.VideoRendererComponent(); err != nil {
return nil, err
} else if port := renderer.Inputs()[0]; port == nil {
return nil, gopi.ErrBadParameter
} else {
format := port.VideoFormat()
format.SetWidthHeight(640, 480)
format.SetCrop(hw.MMALRect{0, 0, 640, 480})
if err := port.CommitFormatChange(); err != nil {
return nil, err
}
if display_region, err := port.DisplayRegion(); err != nil {
return nil, err
} else {
display_region.SetFullScreen(true)
display_region.SetTransform(hw.MMAL_DISPLAY_TRANSFORM_ROT180_MIRROR)
display_region.SetMode(hw.MMAL_DISPLAY_MODE_FILL)
if err := port.SetDisplayRegion(display_region); err != nil {
return nil, err
} else {
return port, nil
}
}
}
}
func CameraOutputPort(mmal hw.MMAL) (hw.MMALPort, error) {
if camera, err := mmal.CameraComponent(); err != nil {
return nil, err
} else if port := camera.Outputs()[0]; port == nil {
return nil, gopi.ErrBadParameter
} else {
// TODO: Set camera parameters and return
return port, nil
}
}
func Main(app *gopi.AppInstance, done chan<- struct{}) error {
if mmal := app.ModuleInstance("hw/mmal").(hw.MMAL); mmal == nil {
return errors.New("Missing MMAL module")
} else if camera_port, err := CameraOutputPort(mmal); err != nil {
return err
} else if renderer_port, err := RendererInputPort(mmal); err != nil {
return err
} else if c, err := mmal.Connect(renderer_port, camera_port, hw.MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT|hw.MMAL_CONNECTION_FLAG_TUNNELLING); err != nil {
return err
} else if err := c.SetEnabled(true); err != nil {
return err
} else {
// Display camera preview until interrupted
fmt.Println("Press CTRL+C to exit")
app.WaitForSignal()
}
// Finish gracefully
done <- gopi.DONE
return nil
}
////////////////////////////////////////////////////////////////////////////////
func main() {
// Create the configuration, load the MMAL instance
config := gopi.NewAppConfig("hw/mmal")
// Run the command line tool
os.Exit(gopi.CommandLineTool2(config, Main))
}