The files contained in this repository can be downloaded to your computer using a svn client.
On Linux you simply type the command displayed below.

This URL has Read-Only access.

Statistics
| Revision:

root / trunk / DomotiGa / COneWire.class @ 25

History | View | Annotate | Download (3.1 kB)

1
' Gambas class file
2
3
' Description:
4
' COneWire.class
5
' Support for Midon's TEMP08 1-wire interface
6
7
' Development Status:
8
' Working, but only temp sensors implemented tested yet.
9
10
' DomotiGa - an open source home automation program.
11
' Copyright(C) 2008 Ron Klinkien
12
13
' Read file called COPYING for license details.
14
15
PROPERTY Port AS String
16
PROPERTY Baud AS String
17
PROPERTY OneWireDebug AS Boolean
18
19
PRIVATE sPort AS String
20
PRIVATE sBaud AS String
21
PRIVATE bOneWireDebug AS Boolean
22
23
PUBLIC hOneWire AS NEW SerialPort
24
PUBLIC tOneWire AS NEW Timer
25
26
PUBLIC sBuffer AS String
27
28
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29
' open serial port
30
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
PUBLIC FUNCTION Connect() AS Boolean
32
33
  ' try to close the port
34
  TRY hOneWire.Close
35
36
  ' get a new one
37
  hOneWire = NEW Serialport AS "OneWire"
38
39
  WITH hOneWire
40
    .PortName = sPort
41
    .Speed = sBaud
42
    .Parity = 0
43
    .DataBits = 8
44
    .StopBits = 1
45
    .Open()
46
  END WITH
47
48
   ' start poll timer for OneWire status LED
49
  tOneWire = NEW Timer AS "tOneWireLED"
50
  tOneWire.Delay = 250
51
  tOneWire.Stop
52
53
  ' all ok
54
  RETURN TRUE
55
56
CATCH ' some errors
57
  Main.WriteLog("OneWire Error: " & ERROR.Text)
58
  RETURN FALSE
59
60
END
61
62
PUBLIC SUB OneWire_Read()
63
64
  DIM sData AS String
65
66
  READ #hOneWire, sData, 1
67
  IF sData = Chr(10) THEN ' buffer until newline then parse
68
    IF Len(sBuffer) > 1 THEN ParseLine(sBuffer)
69
    sBuffer = NULL
70
  ELSE
71
    sBuffer &= sData
72
  END IF
73
74
END
75
76
PUBLIC SUB ParseLine(sStr AS String)
77
78
  DIM iDeviceId AS Integer
79
  DIM aScan AS String[]
80
81
  FMain.pbOneWire.Picture = Main.pLedOn
82
  tOneWire.Start
83
84
  ' example output
85
  ' SUN 00:36:20
86
  ' Reading Sensors...
87
  ' Voltage #01[4 D0000004FC78A26] = 00.32 V 04.98 V
88
  ' Temp #01[4D0000004FC78A26]=23.75C
89
  ' Temp #02[150008013A024910]=22.43C
90
  ' Temp #03[7A0008014A6BC310]=22.68C
91
92
  IF bOneWireDebug THEN Main.WriteDebugLog("[1-Wire] " & sStr)
93
94
  ' 1-wire temp sensor support
95
  IF Mid$(sStr, 1, 4) = "Temp" THEN
96
    IF bOneWireDebug THEN Main.WriteDebugLog("[1-Wire] We got DS1820 temperature data.")
97
    aScan = Scan(sStr, "*[[]*]=*")
98
    iDeviceId = Devices.Find(aScan[1], Devices.FindInterface("Midon TEMP08"))
99
    IF iDeviceId THEN Devices.Update(iDeviceId, Left(aScan[2], -1))
100
  END IF
101
  ' put support for other sensortypes here
102
103
END
104
105
PUBLIC SUB tOneWireLED_Timer()
106
107
  FMain.pbOneWire.Picture = Main.pLedOff
108
  tOneWire.Stop
109
110
END
111
112
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113
' close port
114
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
115
PUBLIC FUNCTION Disconnect() AS Boolean
116
117
  ' try to close the connection
118
  TRY hOneWire.Close
119
  Main.WriteLog("OneWire serial port close.")
120
121
  ' all ok
122
  RETURN TRUE
123
124
CATCH ' some errors
125
  Main.WriteLog("1-Wire Error: " & ERROR.Text)
126
  RETURN FALSE
127
128
END
129
130
' implement properties
131
FUNCTION Port_Read() AS String
132
133
  RETURN sPort
134
135
END
136
137
SUB Port_Write(Value AS String)
138
139
  sPort = Value
140
141
END
142
143
PRIVATE FUNCTION Baud_Read() AS String
144
145
  RETURN sBaud
146
147
END
148
149
PRIVATE SUB Baud_Write(Value AS String)
150
151
  sBaud = Value
152
153
END
154
155
PRIVATE FUNCTION OneWireDebug_Read() AS Boolean
156
157
  RETURN bOneWireDebug
158
159
END
160
161
PRIVATE SUB OneWireDebug_Write(Value AS Boolean)
162
163
  bOneWireDebug = Value
164
165
END