
import  time

import  wx
import  wx.gizmos   as  gizmos
import config

#----------------------------------------------------------------------
from ctypes import *
#       load BCA_Lib.dll
bca = cdll.LoadLibrary("BCA_Lib.dll")
# function
CMPFUNC = CFUNCTYPE(None, c_void_p, c_ubyte, c_ubyte, c_void_p, c_uint, c_void_p)

#
show_temp = 1
show_pres = 1
show_humi = 1
show_alc = 1
#
verbose = 1
#       receive function
def recv_func(contx, channel, event, data, length, cookie):
        dat = cast(data, POINTER(c_ubyte))
        if channel == 0: # temp sensor
                config.temp = dat[2]
                if verbose:
                    print "recv_func(temp):", config.temp
        elif channel == 1: # pressure sensor
                        config.pres = dat[4]<<24|dat[3]<<16|dat[2]<<8|dat[1]
                        if verbose:
                            print "recv_func(pres):", config.pres
        elif channel == 2: # humidity sensor
                        config.humi = dat[2]<<8|dat[1]
                        if verbose:
                            xxx = "%02x" % dat[4]
                            print xxx
                            xxx = "%02x" % dat[3]
                            print xxx
                            xxx = "%02x" % dat[2]
                            print xxx
                            xxx = "%02x" % dat[1]
                            print xxx
                            print "recv_func(humi):", config.humi
        elif channel == 3: # alchole sensor
                config.alc = dat[2]
                if verbose:
                    print "recv_func(alc):", config.alc
#        time.sleep(0)
        return
#

class TestPanel(wx.Panel):
    def __init__(self, parent, log):
        wx.Panel.__init__(self, parent, -1)
        self.log = log

        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.text_temp = wx.StaticText(self, label="temp")
        self.text_pres = wx.StaticText(self, label="pres")
        self.text_humi = wx.StaticText(self, label="humi")
        self.text_alc = wx.StaticText(self, label="mois")

        self.sizer_temp = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer_pres = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer_humi = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer_alc = wx.BoxSizer(wx.HORIZONTAL)

        # temp
        led = gizmos.LEDNumberCtrl(self, -1) #, (25,25), (280, 50))
        self.led_temp = led
#        self.temp.SetValue("99999999")
        self.led_temp.SetAlignment(gizmos.LED_ALIGN_RIGHT)
        self.led_temp.SetDrawFaded(True)
        self.led_temp.SetForegroundColour("orange")

        # pres
        led = gizmos.LEDNumberCtrl(self, -1) #, (25,100), (280, 50))
        self.led_pres = led
#        self.pres.SetValue("56789")
        self.led_pres.SetAlignment(gizmos.LED_ALIGN_RIGHT)
        self.led_pres.SetDrawFaded(True)
        self.led_pres.SetForegroundColour("white")

        # humi
        led = gizmos.LEDNumberCtrl(self, -1) #, (25,175), (280, 50))
        self.led_humi = led
#        self.humi.SetValue("56789")
        self.led_humi.SetAlignment(gizmos.LED_ALIGN_RIGHT)
        self.led_humi.SetDrawFaded(True)
        self.led_humi.SetForegroundColour("yellow")


        # alc
        led = gizmos.LEDNumberCtrl(self, -1) #, (25,250), (280, 50),
#                              gizmos.LED_ALIGN_CENTER)# | gizmos.LED_DRAW_FADED)
        self.led_alc = led
        self.led_alc.SetAlignment(gizmos.LED_ALIGN_RIGHT)
        self.led_alc.SetDrawFaded(True)
        self.led_alc.SetForegroundColour("blue")

#
        self.sizer_temp.Add(self.text_temp, 1, wx.EXPAND)
        self.sizer_temp.Add(self.led_temp, 1, wx.EXPAND)
#        self.sizer_temp.Add(button_temp, 1, wx.EXPAND)

#        self.SetSizer(self.sizer_temp)

        self.sizer_pres.Add(self.text_pres, 1, wx.EXPAND)
        self.sizer_pres.Add(self.led_pres, 1, wx.EXPAND)

        self.sizer_humi.Add(self.text_humi, 1, wx.EXPAND)
        self.sizer_humi.Add(self.led_humi, 1, wx.EXPAND)

        self.sizer_alc.Add(self.text_alc, 1, wx.EXPAND)
        self.sizer_alc.Add(self.led_alc, 1, wx.EXPAND)

        self.sizer.Add(self.sizer_temp, 1, wx.EXPAND)
        self.sizer.Add(self.sizer_pres, 1, wx.EXPAND)
        self.sizer.Add(self.sizer_humi, 1, wx.EXPAND)
        self.sizer.Add(self.sizer_alc, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        self.SetAutoLayout(1)
        self.sizer.Fit(self)
#
        self.OnTimer(None)

        self.timer = wx.Timer(self)
        self.timer.Start(5000) # update every 5000msec
        self.Bind(wx.EVT_TIMER, self.OnTimer)

    def OnPaint(self,event):
        print "OnPaint"

    def OnTimer(self, evt):
        if show_temp == 1:
         st = "%d" % config.temp
#        print "temp",st
         self.led_temp.SetValue(st)
        if show_pres == 1:
         st = "%d" % (config.pres/100)
#        print "pres",st
         self.led_pres.SetValue(st)
        if show_humi == 1:
         st = "%d" % (config.humi*100/16383)
#         print "humi",st
         self.led_humi.SetValue(st)
        if show_alc == 1:
         st = "%d" % config.alc
#        print "alc",st
         self.led_alc.SetValue(st)
#        time.sleep(0)


    def ShutdownDemo(self):
        self.timer.Stop()
        del self.timer

#----------------------------------------------------------------------

def runTest(frame, nb, log):
    win = TestPanel(nb, log)
    return win

#----------------------------------------------------------------------

overview = """\
<html>
<body>
<font size=-1>The following was gleaned as best I could from the wxWindows
source, which was a bit reluctant to reveal its secrets. My appologies if
I missed anything - jmg</font>
<p>
<code><b>LEDNumberCtrl</b>( parent, id=-1, pos=wx.DefaultPosition,
size=wx.DefaultSize, style=LED_ALIGN_LEFT | LED_DRAW_FADED)</code>

<p>This is a control that simulates an LED clock display. It only accepts
numeric input.

<p><b>Styles</b>

<p><dl>
<dt><b>LED_ALIGN_LEFT</b>
<dd>Align to the left.

<dt><b>LED_ALIGN_RIGHT</b>
<dd>Align to the right.

<dt><b>LED_ALIGN_CENTER</b>
<dd>Center on display.

<dt><b>LED_DRAW_FADED</b>
<dd>Not implemented.

</dl>

<p><b>Methods</b> (and best guesses at what they do)

<p><dl>
<dt><b>GetAlignment()</b>
<dd>Returns the alignment attribute for the control.

<dt><b>GetDrawFaded()</b>
<dd>Returns the DrawFaded attribute for the control.

<dt><b>GetValue()</b>
<dd>Returns the current value of the control.

<dt><b>SetAlignment(alignment)</b>
<dd>Set the alignment attribute for the control.

<dt><b>SetDrawFaded(value)</b>
<dd>Set the DrawFaded attribute for the control.

<dt><b>SetValue(number)</b>
<dd>Set the value for the control. Only numeric values are accepted.

</dl>

<p>Additionally, several methods of wx.Window are available as well.

</body>
</html>
"""

func = CMPFUNC(recv_func)


if __name__ == '__main__':
    import sys,os
    import run

#       open device
    print "OpenDevice"
    context = bca.BCA_OpenDevice(0)
#       regist callback function
    print "RegisterReceiveFunc"
    bca.BCA_RegisterReceiveFunc(context, func, context)
#       open channel
    if show_temp == 1:
     print "OpenChannel for temp"
     res = bca.BCA_OpenChannel(context,\
                                                                                                                0,\
                                                                                                                16,\
                                                                                                                0,\
                                                                                                                33,\
                                                                                                                0,\
                                                                                                                0,\
                                                                                                                66,\
                                                                                                                c_double(0.5))
#
    if show_pres == 1:
     print "OpenChannel for pres"
     res = bca.BCA_OpenChannel(context,\
                                                                                                                1,\
                                                                                                                16,\
                                                                                                                0,\
                                                                                                                34,\
                                                                                                                0,\
                                                                                                                0,\
                                                                                                                67,\
                                                                                                                c_double(0.5))
#
    if show_humi == 1:
     print "OpenChannel for humi"
     res = bca.BCA_OpenChannel(context,\
                                                                                                                2,\
                                                                                                                16,\
                                                                                                                0,\
                                                                                                                35,\
                                                                                                                0,\
                                                                                                                0,\
                                                                                                                68,\
                                                                                                                c_double(0.5))
#
    if show_alc == 1:
     print "OpenChannel for alc"
     res = bca.BCA_OpenChannel(context,\
                                                                                                                3,\
                                                                                                                16,\
                                                                                                                0,\
                                                                                                                1,\
                                                                                                                0,\
                                                                                                                0,\
                                                                                                                50,\
                                                                                                                c_double(0.5))
#
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])

