toggle ok

This commit is contained in:
2019-08-27 08:42:32 +02:00
parent bd0b562dcd
commit 9b9a94a83b
3 changed files with 136 additions and 72 deletions

View File

@@ -31,19 +31,47 @@ func (b I2CBoard) channelStatus(ch uint) (bool, error) {
return false, errors.New("Invalid channel") return false, errors.New("Invalid channel")
} }
write := []byte{0x0A} write := []byte{0x0A}
b.data = make([]byte, 2)
err := b.i2cdev.Tx(write, b.data) err := b.i2cdev.Tx(write, b.data)
if err != nil { if err != nil {
return false, err return false, err
} }
byteToConsider := ch / b.channels byteToConsider := ch / b.channels
value := (b.data[byteToConsider] >> ch & 1) == 1 value := (b.data[byteToConsider] >> (ch % 8) & 1) == 1
if b.inverted { if b.inverted {
value = !value value = !value
} }
return value, nil return value, nil
} }
func (b I2CBoard) channelToggle(ch uint) error {
var mask byte
if b.channels <= 0 {
return errors.New("Board without channels")
}
if ch >= b.channels {
return errors.New("Invalid channel")
}
// if b.data == nil {
_, _ = b.channelStatus(ch)
// }
byteToConsider := ch / b.channels
mask = 1 << (ch % 8)
v := b.data[byteToConsider]
v ^= mask
b.data[byteToConsider] = v
write := append([]byte{0x09}, b.data...)
_, err := b.i2cdev.Write(write)
if err != nil {
return err
}
return nil
}
func initI2C() { func initI2C() {
// Make sure periph is initialized. // Make sure periph is initialized.
if _, err := host.Init(); err != nil { if _, err := host.Init(); err != nil {
@@ -71,6 +99,7 @@ func initI2C() {
} }
MyBoard.i2cdev = *mydevice MyBoard.i2cdev = *mydevice
MyBoard.data = make([]byte, (MyBoard.channels-1)/8+1)
go func() { go func() {
var i uint var i uint

View File

@@ -4,18 +4,13 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"strconv"
"github.com/go-macaron/pongo2" "github.com/go-macaron/pongo2"
"gopkg.in/macaron.v1" "gopkg.in/macaron.v1"
) )
func startServer() { func statusPage(ctx *macaron.Context) {
m := macaron.Classic()
m.Use(pongo2.Pongoer())
m.Use(macaron.Static("static"))
// m.Get("/", myHandler)
m.Get("/", func(ctx *macaron.Context) {
ctx.Data["pluglist"] = []Dictionary{ ctx.Data["pluglist"] = []Dictionary{
{"id": 1, "description": "p1"}, {"id": 1, "description": "p1"},
{"id": 2, "description": "p2"}, {"id": 2, "description": "p2"},
@@ -27,9 +22,9 @@ func startServer() {
{"id": 8, "description": "p8"}, {"id": 8, "description": "p8"},
} }
ctx.HTML(200, "status") // 200 is the response code. ctx.HTML(200, "status") // 200 is the response code.
}) }
m.Get("/json/status", func(ctx *macaron.Context) { func jsonStatus(ctx *macaron.Context) {
p0, err := MyBoard.channelStatus(0) p0, err := MyBoard.channelStatus(0)
if err != nil { if err != nil {
@@ -71,16 +66,43 @@ func startServer() {
log.Fatal(err) log.Fatal(err)
} }
p1 = p1 && p2 && p3 && p4 && p5 && p6 && p7
ctx.JSON(http.StatusOK, Dictionary{ ctx.JSON(http.StatusOK, Dictionary{
"data": [][]string{ "data": [][]string{
{"0", "p0", fmt.Sprint(p0)}, {"0", "p0", fmt.Sprint(p0)},
{"1", "p1", fmt.Sprint(p1)}, {"1", "p1", fmt.Sprint(p1)},
{"2", "p2", fmt.Sprint(p2)}, {"2", "p2", fmt.Sprint(p2)},
{"3", "p3", fmt.Sprint(p3)},
{"4", "p4", fmt.Sprint(p4)},
{"5", "p5", fmt.Sprint(p5)},
{"6", "p6", fmt.Sprint(p6)},
{"7", "p7", fmt.Sprint(p7)},
}, },
}) })
}
func jsonOutletToggle(ctx *macaron.Context) {
id, err := strconv.ParseUint(ctx.Params(":id"), 10, 64)
if err != nil {
log.Fatal(err)
}
err = MyBoard.channelToggle(uint(id))
if err != nil {
log.Fatal(err)
}
ctx.JSON(http.StatusOK, Dictionary{
"data": "ok",
}) })
}
func startServer() {
m := macaron.Classic()
m.Use(pongo2.Pongoer())
m.Use(macaron.Static("static"))
// m.Get("/", myHandler)
m.Get("/", statusPage)
m.Get("/json/status", jsonStatus)
m.Post("/json/outlet/:id/toggle", jsonOutletToggle)
m.Get("/boards", func(ctx *macaron.Context) { m.Get("/boards", func(ctx *macaron.Context) {
ctx.HTML(200, "boards") // 200 is the response code. ctx.HTML(200, "boards") // 200 is the response code.

View File

@@ -85,7 +85,7 @@
</div>`; </div>`;
$('#example2').DataTable({ var table = $('#example2').DataTable({
'ajax': '/json/status', 'ajax': '/json/status',
"columns" : [ "columns" : [
{ "data" : 0 }, { "data" : 0 },
@@ -103,8 +103,21 @@
// fa-toggle-off // fa-toggle-off
$('#example2 tbody').on( 'click', '[script="toggle"]', function () { $('#example2 tbody').on( 'click', '[script="toggle"]', function (e) {
alert( 'ciao' ); e.preventDefault();
var id = $( this ).closest('tr').find('td:first').text();
$.ajax({
url: '/json/outlet/'+id+'/toggle',
type: 'post'
})
.done(function(result) {
console.log(result);
table.ajax.reload();
})
.fail(function(jqXHR, textStatus, errorThrown) {
// needs to implement if it fails
console.log(textStatus);
});
}); });