Commits

Anonymous committed 438af4a

initial commit

Comments (0)

Files changed (7)

+include $(GOROOT)/src/Make.inc
+ 
+TARG=demo
+
+GOFILES=\
+	demo.go 
+
+include $(GOROOT)/src/Make.cmd
+package main
+
+import (
+	"log"
+	"os"
+	"path"
+	"strconv"
+//	"fmt"
+	"sort"
+	"strings"
+	"gtk"
+//	"gdkpixbuf"
+
+)
+
+var (
+	title string = "IRAM - Plateau de Bure model"
+	programName string = "Pdb-model"
+	authors []string = []string{
+		"Mathieu Lonjaret <lonjaret@iram.fr>",
+		"Roberto Garcia <garcia@iram.fr>"}
+	comments string = "Program interacting with the plateau de bure model, showing images at various resolutions depending on the model's input. The model was designed by Mathieu Lonjaret, Karin Zacher, Roberto Garcia, Bastien Lefranc and the iram mechanical construction team."
+	license string = "This program is available under the terms and conditions of the BSD style license."
+	imagesPath string = "../images"
+	suffixes = []string{".jpg", ".jpeg", ".png"}
+	maxNbImages int = 8
+	signaltoconf = map[int]int{
+		41: 1, 
+		37: 2, 
+		24: 3,
+		21: 4,
+		42: 5,
+		40: 6,
+		26: 7,
+		32: 8,
+	}
+)
+
+// Mapping of the signal to the antennae configuration.
+//We define the positions as the following:
+//1---------2----------3----4
+//				|
+//				5
+//				|
+//				6
+//and the positions are encoded as the positions of bits in a byte.
+//For example, if the 3 antennae are in position 1, 3 and 5, the bits
+//are 00010101, and hence the signal sent is 21.
+//The configurations are ordered in reverse compactness order, i.e
+//configuration 1 is the most spread out (best resolution, while
+//configuration 6 is the most compact.  Thus, the mapping is (cf
+//signaltoconf map):
+//41: 1, (00101001)
+//37: 2, (00100101)
+//24: 3, (00011001)
+//21: 4, (00010101)
+//42: 5, (00101010)
+//40: 6, (00100110)
+//26: 7, (00011010)
+//32: 8, (00010110)
+
+type lineReader struct {
+	*os.File
+}
+
+//TODO: prevent overflow over 3 chars
+func (lr *lineReader) readLine() (line string) {
+	var b []uint8 = make([]uint8, 1)
+	bline := make([]byte, 3)
+	i:= 0
+	for ;; {
+		_, err := lr.File.Read(b)
+		if err != nil {
+			log.Exit(err)
+		}
+		bline[i] = b[0] // + 48
+		if b[0] == '\n' {
+			line = string(bline[0:i])
+			break
+		}
+		i++
+	}
+	return line
+}
+
+//TODO: lineReader is very lame, there must be a much simpler way with a Scan func
+func readSignal(c chan int) {
+	lr := &lineReader{os.Stdin}
+	var lastSignal int = 0
+	for ;; {
+		signal, err := strconv.Atoi(lr.readLine())
+		if err != nil {
+			log.Exit(err)
+		}
+		if signal != lastSignal {
+			c <- signal
+			lastSignal = signal
+		}
+	}
+}
+
+type images struct {
+	dir string
+	dirs []string
+	paths []string
+	bufs []*gtk.GtkImage
+	visible *gtk.GtkImage
+}
+
+func (imgs *images) setImagesDirs(dirPath string) os.Error {
+	currentDir, err := os.Open(dirPath, os.O_RDONLY, 0644)
+	if err != nil {
+		return err
+	}
+	names, err := currentDir.Readdirnames(-1)
+	if err != nil {
+		return err
+	}
+	currentDir.Close()
+
+//TODO: don't hardcode that, or make it able to grow
+	dirs := make([]string, 10)
+	counter := 0
+	sort.SortStrings(names)
+	for _, v := range names {
+		fullpath := path.Join(dirPath, v)
+		fi, err := os.Lstat(fullpath)
+		if err != nil {
+			return err
+		}
+		if fi.IsDirectory() {
+			dirs[counter] = fullpath
+				counter++
+		}
+	}
+	dirs = dirs[0:counter]
+	imgs.dirs = dirs
+	return nil
+}
+
+func (imgs *images) setImagesPaths(dirPath string) os.Error {
+	imgs.dir = dirPath
+	currentDir, err := os.Open(dirPath, os.O_RDONLY, 0644)
+	if err != nil {
+		return err
+	}
+	names, err := currentDir.Readdirnames(-1)
+	if err != nil {
+		return err
+	}
+	currentDir.Close()
+
+	images := make([]string, maxNbImages)
+	counter := 0
+	sort.SortStrings(names)
+	for _, v := range names {
+		for _, suffix := range suffixes {
+			if strings.Index(v,	suffix) != -1 {
+				images[counter] = path.Join(dirPath, v)
+				counter++
+				break
+			}
+		}
+	}
+	images = images[0:counter]
+	imgs.paths = images
+	return nil
+}
+
+func (imgs *images) loadImages() {
+	images := make([]*gtk.GtkImage, len(imgs.paths))
+	for i, v := range imgs.paths {
+		images[i] = gtk.ImageFromFile(v)
+	}
+	imgs.bufs = images
+}
+
+func (imgs *images) chooseImagesDir(dirPath string) os.Error {
+	err := imgs.setImagesPaths(dirPath)
+	if err != nil {
+		return err
+	}
+	imgs.loadImages()
+	return nil
+}
+
+func gtkStuff(c chan int) {
+	//init gtk stuff
+	gtk.Init(nil)
+	window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
+	window.SetTitle(title)
+	window.Connect("destroy", func(w *gtk.GtkWidget, user_data string) {
+		println("got destroy!", user_data)
+		gtk.MainQuit()
+	},
+		"foo")
+
+	// preload images
+	dir, _ := path.Split(os.Args[0])
+	var imgs images
+	err := imgs.setImagesDirs(path.Join(dir, imagesPath))
+	if err != nil {
+		log.Exit(err)
+	}
+//TODO: check if imgs.dirs[0] exists
+	err = imgs.setImagesPaths(imgs.dirs[0])
+	if err != nil {
+		log.Exit(err)
+	}
+	imgs.loadImages()
+
+	//--------------------------------------------------------
+	// GtkVBox
+	//--------------------------------------------------------
+	vbox := gtk.VBox(false, 1)
+
+	//--------------------------------------------------------
+	// GtkMenuBar
+	//--------------------------------------------------------
+	menubar := gtk.MenuBar()
+	vbox.PackStart(menubar, false, false, 0)
+
+	//--------------------------------------------------------
+	// GtkMenuItem
+	//--------------------------------------------------------
+	filemenu := gtk.MenuItemWithMnemonic("_File")
+	menubar.Append(filemenu)
+	filesubmenu := gtk.Menu()
+	filemenu.SetSubmenu(filesubmenu)
+	exitmenuitem := gtk.MenuItemWithMnemonic("E_xit")
+	exitmenuitem.Connect("activate",func() { os.Exit(0) }, nil)
+	filesubmenu.Append(exitmenuitem)
+
+	filemenu = gtk.MenuItemWithMnemonic("_Help")
+	menubar.Append(filemenu)
+	filesubmenu = gtk.Menu()
+	filemenu.SetSubmenu(filesubmenu)
+	exitmenuitem = gtk.MenuItemWithMnemonic("_About")
+	exitmenuitem.Connect("activate", func() {
+		dialog := gtk.AboutDialog()
+		dialog.SetName(title)
+		dialog.SetProgramName(programName)
+		dialog.SetAuthors(authors)
+		dialog.SetComments(comments)
+		dialog.SetLicense(license)
+		dialog.SetWrapLicense(true)
+		dialog.Run()
+		dialog.Destroy()
+	},
+		nil)
+	filesubmenu.Append(exitmenuitem)
+
+	//--------------------------------------------------------
+	// GtkVPaned
+	//--------------------------------------------------------
+	vpaned := gtk.VPaned()
+	vbox.Add(vpaned)
+
+	//--------------------------------------------------------
+	// GtkFrame
+	//--------------------------------------------------------
+	frame1 := gtk.Frame(title)
+	framebox1 := gtk.VBox(false, 1)
+	frame1.Add(framebox1)
+
+	frame2 := gtk.Frame("no image")
+	framebox2 := gtk.VBox(false, 1)
+	frame2.Add(framebox2)
+
+	vpaned.Add1(frame1)
+	vpaned.Add2(frame2)
+
+	//--------------------------------------------------------
+	// GtkComboBox
+	//--------------------------------------------------------
+	controls := gtk.HBox(false, 1)
+	var combotext *gtk.GtkLabel
+	var combobox *gtk.GtkComboBox
+	togglebutton := gtk.ToggleButtonWithLabel("On/Off")
+	togglebutton.Connect("toggled", func() {
+		if togglebutton.GetActive() {
+			combotext.Hide()
+			combobox.Hide()
+			togglebutton.SetLabel("On")
+		} else {
+			combotext.Show()
+			combobox.Show()
+			togglebutton.SetLabel("Off")
+		}
+	},
+		nil)
+	controls.Add(togglebutton)
+	combotext = gtk.Label("Choose an astronomical object: ")
+	controls.Add(combotext)
+	combobox = gtk.ComboBoxNewText()
+	for i:=0; i<len(imgs.dirs); i++ {
+		combobox.AppendText(path.Base(imgs.dirs[i]))
+	}
+	combobox.SetActive(0)
+	combobox.Connect("changed", func() {
+		if !togglebutton.GetActive() {
+			selected := combobox.GetActiveText()
+			for _, v := range imgs.bufs {
+				v.Hide()
+				framebox2.Remove(v)
+			}
+			err = imgs.chooseImagesDir(path.Join(imagesPath, selected))
+			if err != nil {
+				log.Exit(err)
+			}
+			frame2.SetLabel(selected)
+			for _, v := range imgs.bufs {
+				framebox2.Add(v)
+			}
+			imgs.visible = imgs.bufs[0]
+			imgs.visible.Show()
+		}
+	}, nil)
+	controls.Add(combobox)
+	framebox1.PackStart(controls, false, false, 0)
+
+	//--------------------------------------------------------
+	// show it!
+	//--------------------------------------------------------
+	window.Add(vbox)
+//TODO: resize with the size of the image
+	window.SetSizeRequest(600, 600)
+	window.ShowAll()
+	// add first series of images
+	for i := 0; i<len(imgs.paths); i++ {
+		framebox2.Add(imgs.bufs[i])
+	}
+	frame2.SetLabel(path.Base(imgs.dir))
+	imgs.visible = imgs.bufs[0]
+	imgs.visible.Show()
+
+	//--------------------------------------------------------
+	// listen for signal from the other (i.e labview) program
+	//--------------------------------------------------------
+	go func() {
+		lastsignal := 0
+		configuration := 0
+		for {
+			signal := <-c
+			if togglebutton.GetActive() {
+				if signal != lastsignal {
+					configuration = signaltoconf[signal]
+					if configuration == 0  {
+						log.Stderrf("Unknown signal: %d \n", signal)
+						continue
+					}
+					imgs.visible.Hide()
+					imgs.visible = imgs.bufs[configuration-1]
+					imgs.visible.Show()
+					lastsignal = signal
+				}
+			}
+		}
+	}()
+	gtk.Main()
+}
+
+func main() {
+	c1 := make(chan int)  
+	c2 := make(chan int)  
+	go gtkStuff(c1)
+	go readSignal(c2)
+	for {
+		foo := <- c2
+		c1 <- foo
+	}
+
+}
+include $(GOROOT)/src/Make.inc
+ 
+TARG=feed
+
+GOFILES=\
+	feed.go 
+
+include $(GOROOT)/src/Make.cmd
+package main
+
+import (
+	"syscall"
+	"rand"
+	"math"
+)
+
+func randFeed() {
+	var i, feed uint8 = 0, 0
+	for ;; {
+		feed = 0
+		for i = 0; i<8 ; i++ {
+			if rand.Float() > 0.5 {
+				feed += (1 << i)
+			}
+		}
+		print(feed, "\n")
+		syscall.Sleep(1000000000)
+	}
+}
+
+func randFeed2() {
+	var feed int = 0
+	for ;; {
+		feed = int(math.Ceil(rand.Float64() * 6.0))
+		print(feed, "\n")
+		syscall.Sleep(1000000000)
+	}
+}
+
+func randFeed3() {
+	var key int = 0
+	var conftosig = map[int]int{
+		1:41, 
+		2:37, 
+		3:24,
+		4:21,
+		5:42,
+		6:40,
+		7:26,
+		8:32,
+		9:1,
+		10:2,
+		11:3,
+	}
+	for ;; {
+		key = int(math.Ceil(rand.Float64() * 11.0))
+		print(conftosig[key], "\n")
+		syscall.Sleep(1000000000)
+	}
+}
+
+func feedInFive() {
+	var i, feed uint8 = 0, 0
+	for ;; {
+		if i % 5 == 0 {
+			feed = 5
+		} else {
+			feed = 1
+		}
+		print(feed, "\n")
+		syscall.Sleep(1000000000)
+		i++
+	}
+}
+
+func binFeed() {
+	feed, i := 0, 0
+	for ;; {
+		if i % 5 == 0 {
+			if feed == 0 {
+				feed = 1
+			} else {
+				feed = 0
+			}
+		}
+		print(feed, "\n")
+		syscall.Sleep(1000000000)
+		i++
+	}
+}
+
+func main() {
+	randFeed3()
+}
+include $(GOROOT)/src/Make.inc
+ 
+TARG=prep
+
+GOFILES=\
+	prep.go 
+
+include $(GOROOT)/src/Make.cmd
+#!/usr/local/plan9/bin/rc
+
+cd $1
+mv $1-1.jpg $1.jpg
+../prep $1.jpg 8
+rm $1.jpg
+package main
+
+import (
+	"image"
+	"image/png"
+	"image/jpeg"
+	"log"
+	"os"
+	"flag"
+	"fmt"
+	"strings"
+	"strconv"
+)
+
+var _ = fmt.Printf
+
+var pic string
+var out string
+
+func openImage(pic string) (m image.Image) {
+	f, err := os.Open(pic, os.O_RDONLY, 0666)
+	if err != nil {
+		log.Exit(err)
+	}
+	if strings.HasSuffix(pic, "png") {
+		m, err = png.Decode(f)
+	} else {
+		if strings.HasSuffix(pic, "jpeg") || strings.HasSuffix(pic, "jpg") {
+			m, err = jpeg.Decode(f)
+		} else {
+			log.Exit("unkown file format")
+		}
+	}
+	if err != nil{
+		log.Exit(err)
+	}
+	return m
+}
+
+func degrade(inputfile string, counter int, width int, height int) os.Error {
+	suffixes := []string{".jpg", ".jpeg", ".png"}
+	var suffix string
+	for _, suffix = range suffixes {
+		if strings.Index(inputfile,	suffix) != -1 {
+			break
+		}
+	}
+	outputfile := strings.Replace(inputfile, suffix, "-" + fmt.Sprint(counter) + suffix, 1)
+	tmpfile := "/tmp/foo" + suffix
+	w := width / counter
+	h := height / counter
+	geometry := fmt.Sprint(w) + "x" + fmt.Sprint(h)
+
+	if counter == 1 {
+		var args []string = make([]string, 3)
+		args[0] = "/bin/cp"
+		args[1] = inputfile
+		args[2] = outputfile
+		fds := []*os.File{os.Stdin, os.Stdout, os.Stderr}
+		pid, err := os.ForkExec(args[0], args, os.Environ(), "", fds)
+		if err != nil {
+			return err
+		}
+		_, err = os.Wait(pid, os.WSTOPPED)
+		if err != nil {
+			return err
+		}
+		return nil
+	}
+
+	var args []string = make([]string, 5)
+	args[0] = "/usr/bin/convert"
+	args[1] = inputfile
+	args[2] = "-scale"
+	args[3] = geometry
+	args[4] = tmpfile
+	fds := []*os.File{os.Stdin, os.Stdout, os.Stderr}
+	pid, err := os.ForkExec(args[0], args, os.Environ(), "", fds)
+	if err != nil {
+		return err
+	}
+	_, err = os.Wait(pid, os.WSTOPPED)
+	if err != nil {
+		return err
+	}
+
+	geometry = fmt.Sprint(width) + "x" + fmt.Sprint(height)
+	args[1] = tmpfile
+	args[2] = "-resize"
+	args[3] = geometry
+	args[4] = outputfile
+	pid, err = os.ForkExec(args[0], args, os.Environ(), "", fds)
+	if err != nil {
+		return err
+	}
+	_, err = os.Wait(pid, os.WSTOPPED)
+	if err != nil {
+		return err
+	}
+	return nil
+}
+func usage() {
+	fmt.Fprintf(os.Stderr, "usage:\t prep image [n] \n");
+	os.Exit(2);
+}
+
+func main() {
+	flag.Usage = usage
+	flag.Parse()
+	args := flag.Args()
+
+	var n int
+	var err os.Error
+	switch flag.NArg() {
+	case 1:
+		n = 6
+	case 2:
+		n, err = strconv.Atoi(args[1])
+		if err != nil {
+			os.Exit(1)
+		}
+	default:
+		usage()
+	}
+
+	pic = args[0]
+	// lazy testing for image format
+	im := openImage(pic)
+
+	for i:=0; i<n; i++ {
+		degrade(pic, i+1, im.Bounds().Dx(), im.Bounds().Dy())
+	}
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.