' Furniture Client 0001 ' By Tom Igoe, 2004 ' modified by Rob Seward 2005 ' Serial out is on pin RC6 ' serial in is on pin RC7 ' an analog sensor is on pin RA0 ' Define ADCIN parameters DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS 'LED constants statusLed var portb.7 ' variables and constants for the serial port: tx var portc.6 ' transmit pin rx var portc.7 ' receive pin inv9600 con 16468 ' baudmode for serin2 and serout2: 9600 8-N-1 inverted non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted 'changes for 18f252 xportTx var portc.2 xportRx var portc.1 xportDTR var portc.3 adcPicRx var portc.5 inputMaxLength con 96 ' max. input string nameMaxLength con 20 ' max name string cmdMaxLength con 10 ' max command string asciiSpace con 32 ' constant for ascii SPACE character lf con 10 ' constant for ascii linefeed character cr con 13 ' constant for ascii carriage return character i var byte ' counter inputArray var byte(inputMaxLength) ' input strung from cobox nameArray var byte(nameMaxLength) ' name of person who sent to us cmdArray var byte(cmdMaxLength) ' command string they sent us nameLength var byte ' length of their name cmdLength var byte ' length of the command chunkCursorPos var byte ' array pointer currentChar var byte ' array pointer dataByte var byte ' used when we just want one byte adcPicByte var byte adcPicByte2 var byte flag var bit mode var bit prevSoundState var byte prevAvailState var bit clear ' clear all variables 'variables for ADC: ADCvar var word ' Create variable to store result TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result Pause 1000 ' Wait a second at startup login: ' wait for an ascii C, signifying a connection. ' this assumes you're in autoconnect mode (ConnectMode D5) while dataByte <> 67 serin2 xportRx, non9600, [dataByte] wend ' wait until the intro text is past. It ends with a > and a linefeed: serin2 xportRx, non9600, [WAIT(">"), dataByte] pause 100 ' send your login serout2 xportTx, non9600, ["login pj", lf] pause 500 serout2 xportTx, non9600, ["set_info This is a chair called pj. Send '?' to pj to "] serout2 xportTx, non9600, ["get ambient audio level and avalability. Send 'N' to "] serout2 xportTx, non9600, ["turn state change notification on/off.", lf] 'serout2 xportTx, non9600, ["exit", lf] mode = 0 main: 'adcvar = 0 ' read the incoming string from the cobox: if mode = 1 then serin2 xportRx, non9600, 1000, listen2, [STR inputArray\96\lf] else serin2 xportRx, non9600, [STR inputArray\96\lf] endif ' parse the name from the input string: gosub getName ' if we got no name, it's garbage. Go back to main: if nameLength = 0 then main ' parse the command from the input string: gosub getCommand ' do something based on the first letter of the command: select case CmdArray[0] case 65: ' ascii A serout2 xportTx, non9600, ["send ", STR nameArray, " I'll shut up already!", lf] case 69: ' ascii E serout2 xportTx, non9600, ["send ", STR nameArray, " I'll shut up already!", lf] CASE "Q": serout2 xportTx, non9600, ["exit", lf] case "N": gosub modeChange CASE "1": gosub flash CASE "?": 'listen for ADC pic gosub listen end select ' clear the arrays for the next round of input: gosub clearName gosub clearCommand gosub clearInput goto main readADC: 'read the ADC value: adcin 0, ADCvar 'GOTO main return timeout: serout2 xportTx, non9600, ["send ", STR nameArray, " ADC Pic Timeout", lf] adcPicByte = 0 gosub clearName gosub clearCommand gosub clearInput goto main flash: high statusLed pause 200 low statusLed pause 200 high statusLed pause 200 low statusLEd return getName: ' iterate over the input array, copying bytes into the name array ' until we get a space (ascii 32) currentChar = 0 while inputArray[currentChar] <> asciiSpace nameArray[currentChar] = inputArray[currentChar] currentChar = currentChar + 1 ' if we overflow the array, we have a bogus name. go back to main if currentChar >= nameMaxLength then main wend nameLength = currentChar return clearName: ' fill the name array with 0 for currentChar = 0 to (nameMaxLength - 1) nameArray[currentChar] = 0 next nameLength = 0 return getCommand: ' iterate over the input array, copying bytes into the command array ' start from three bytes after the name ' (because the name is followed by " : " ) currentChar = nameLength + 3 chunkCursorPos = 0 while inputArray[currentChar] <> asciispace cmdArray[chunkCursorPos] = inputArray[currentChar] chunkCursorPos = chunkCursorPos + 1 currentChar = currentChar + 1 cmdLength = chunkCursorPos ' if we overflow the array, we have a bogus command. go back to main if chunkCursorPos >= cmdMaxLength then main wend return clearCommand: ' fill the command array with 0 for currentChar = 0 to (cmdMaxLength - 1) cmdArray[currentChar] = 0 next cmdLength = 0 return clearInput: ' fill the input array with 0 for currentChar = 0 to (inputMaxLength -1) inputArray[currentChar] = 0 next return listen: SERIN2 adcPicRx, non9600, 1000, timeout, [adcPicByte] SERIN2 adcPicRx, non9600, [WAIT("P"), adcPicByte, adcPicByte2] '[adcPicByte]' if adcPicByte = 0 then serout2 xportTx, non9600, ["send ", STR nameArray, " Sound Level: LOW"] endif if adcPicByte = 1 then serout2 xportTx, non9600, ["send ", STR nameArray, " Sound Level: MEDIUM"] endif if adcPicByte = 2 then serout2 xportTx, non9600, ["send ", STR nameArray, " Sound Level: HIGH"] endif IF ADCpICBYTE2 = 1 then serout2 xportTx, non9600, [" | Chair Occupied", lf] else serout2 xportTx, non9600, [" | Chair available", lf] endif prevSoundState = adcPicByte prevAvailState = adcPicByte2 return listen2: SERIN2 adcPicRx, non9600, 1000, timeout, [adcPicByte] SERIN2 adcPicRx, non9600, [WAIT("P"), adcPicByte, adcPicByte2] if adcPicByte <> prevSoundState OR adcPicByte2 <> prevAvailState then SERIN2 adcPicRx, non9600, 1000, timeout, [adcPicByte] SERIN2 adcPicRx, non9600, [WAIT("P"), adcPicByte, adcPicByte2] '[adcPicByte]' if adcPicByte = 0 then serout2 xportTx, non9600, ["send all Sound Level: LOW"] endif if adcPicByte = 1 then serout2 xportTx, non9600, ["send all Sound Level: MEDIUM"] endif if adcPicByte = 2 then serout2 xportTx, non9600, ["send all Sound Level: HIGH"] endif IF ADCpICBYTE2 = 1 then serout2 xportTx, non9600, [" | Chair Occupied", lf] else serout2 xportTx, non9600, [" | Chair available", lf] endif endif prevSoundState = adcPicByte prevAvailState = adcPicByte2 goto main; modeChange: 'serout2 xportTx, non9600, ["send ", STR nameArray, " I'll shut up already!", lf] if mode = 0 then serout2 xportTx, non9600, ["send ", STR nameArray, " State change notification ON", lf] mode = 1 else serout2 xportTx, non9600, ["send ", STR nameArray, " State change notification OFF", lf] mode = 0 endif return