'tower code rev. 13 ' last updated dec 5 2005 by Cory Forsyth ' created by Zach Eveland ' this code is the controller code for the iTunes Jukebox CD tower. ' the variable BTBaudMode is a misnomer; originally this variable was ' used to send data via bluetooth but due to some communication issues ' we are using a serial cable instead. ' for more information, see www.itjukebox.com ' changes: ' rev 13: changed the rx/tx pins (from pic to computer and vice versa) to C4 for transmit, C5 receive ' rev 12: added an analog input (for volume control) ' Ê Ê Ê and changed the query out string to include the volume ' rev 8: added some debug commands ' updated dec 4 2005 ' to query all the ports for their addresses ' 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 TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify DEFINE OSC 4 start: 'we define two different baud modes because the PICs in each of the cases are 'expecting inverted-logic, pseudo-RS232 signalling while the Bluetooth module 'linking the tower to the computer is expecting true logic levels BTbaudMode VAR WORD 'BTbaudMode = 84 Ê Ê Ê Ê '9600,8,N,1 - non-inverted (true TTL) BTbaudMode = 16468 PICbaudMode VAR WORD PICbaudMode = 16468 Ê Ê '9600,8,N,1 - inverted (pseudo-RS232) powerpin VAR PORTC.6 Ê Ê'only use is to blink on startup of the tower rxpin VAR PORTC.5 Ê Ê Ê Ê Ê Ê Ê 'used for serial communication with the host txpin VAR PORTC.4 Ê Ê Ê Ê Ê Ê Ê 'used for serial communication with the host slot1pin VAR PORTB.0 Ê Ê'these pins serve as the rx/tx link with the slot2pin VAR PORTB.1 Ê Ê'cases - traffic is carried as inverted-logic slot3pin VAR PORTB.2 Ê Ê'pseudo-RS232 slot4pin VAR PORTB.3 slot5pin VAR PORTB.4 slot6pin VAR PORTB.5 slot7pin VAR PORTB.6 slot8pin VAR PORTB.7 slot1Present VAR PORTD.0 Ê Ê Ê Ê'these pins are inputs for shunts inside slot2Present VAR PORTD.1 Ê Ê Ê Ê'each case that are used to indicate whether slot3Present VAR PORTD.2 Ê Ê Ê Ê'a case is present without having to actually slot4Present VAR PORTD.3 Ê Ê Ê Ê'poll the slot and wait for the SERIN2 timeout slot5Present VAR PORTD.4 Ê Ê Ê Ê'period to elapse slot6Present VAR PORTD.5 slot7Present VAR PORTD.6 slot8Present VAR PORTD.7 INPUT slot1Present INPUT slot2Present INPUT slot3Present INPUT slot4Present INPUT slot5Present INPUT slot6Present INPUT slot7Present INPUT slot8Present OUTPUT powerpin INPUT rxpin OUTPUT txpin rxFromMac VAR BYTE txToMac VAR BYTE rxFromPIC VAR BYTE txToPIC VAR BYTE i VAR BYTE askForID VAR BYTE slotValues var BYTE[8] c VAR BYTE slotToColor VAR BYTE slotDelay VAR BYTE volume VAR word rxFromMac = 0 txToMac = 0 rxFromPIC = 0 txToPIC = 0 i = 0 askForID = 63 FOR i = 0 TO 7 ÊslotValues[i] = 0 NEXT i c = 0 slotToColor = 0 slotDelay = 50 FOR i = 0 TO 4 Ê Ê Ê PAUSE 50 Ê Ê Ê LOW powerpin Ê Ê Ê PAUSE 50 Ê Ê Ê HIGH powerpin NEXT i main: Ê'wait for traffic from teh host computer... ÊSERIN2 rxpin, BTbaudMode, 100, timedout, [rxFromMac] Ê'if the host sends "?" it indicates a request for the values Ê'of the cases in all eight slots. Êthe controller jumps to a sub-routine, Ê'checks the value of all slots with a case present, and presents them to Ê'the host as a string of bytes. ÊIF (rxFromMac == 63) THEN ' "?" -- ask for all slot values Ê Ê Ê ÊGOTO getSlotValues ÊENDIF Ê'the rest of the code in the main loop is used for setting the color of each Ê'case. Êthis bit watches for the ASCII values "0" to "7" indicating slots Ê'1 through 8 on the tower. Êall subsequent color traffic will be addressed to Ê'the slot indicated here ÊIF (rxFromMac >= 48) && (rxFromMac <= 55) THEN Ê ÊslotToColor = rxFromMac - 48 ÊENDIF Ê'determines the color to set the case to - the host will send color Ê'instructions as a value from ASCII "a" to ASCII "h" indicating either Ê'off, green, red, blue, purple, aqua, orange, or white (though not in that Ê'particular order) ÊIF (rxFromMac >= 97) && (rxFromMac <= 104) THEN Ê Êc = rxFromMac - 97 Ê ÊIF (slotToColor == 0) THEN Ê Ê ÊSEROUT2 slot1pin, PICbaudMode, [DEC c] Ê ÊENDIF Ê ÊIF (slotToColor == 1) THEN Ê Ê ÊSEROUT2 slot2pin, PICbaudMode, [DEC c] Ê ÊENDIF Ê ÊIF (slotToColor == 2) THEN Ê Ê ÊSEROUT2 slot3pin, PICbaudMode, [DEC c] Ê ÊENDIF Ê ÊIF (slotToColor == 3) THEN Ê Ê ÊSEROUT2 slot4pin, PICbaudMode, [DEC c] Ê ÊENDIF Ê ÊIF (slotToColor == 4) THEN Ê Ê ÊSEROUT2 slot5pin, PICbaudMode, [DEC c] Ê ÊENDIF Ê ÊIF (slotToColor == 5) THEN Ê Ê ÊSEROUT2 slot6pin, PICbaudMode, [DEC c] Ê ÊENDIF Ê ÊIF (slotToColor == 6) THEN Ê Ê ÊSEROUT2 slot7pin, PICbaudMode, [DEC c] Ê ÊENDIF Ê ÊIF (slotToColor == 7) THEN Ê Ê ÊSEROUT2 slot8pin, PICbaudMode, [DEC c] Ê ÊENDIF ÊENDIF ÊGOTO timedout timedout: ÊrxFromMac = 0 ÊtxToMac = 0 ÊrxFromPIC = 0 ÊtxToPIC = 0 ÊGOTO main getSlotValues: Ê Ê' get volume ÊADCIN 0, volume ÊIF slot1Present = 1 THEN Ê ÊSEROUT2 slot1pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot1pin, PICbaudMode, slotDelay, slot1timeout, [rxFromPIC] Ê ÊslotValues[0] = rxFromPIC ÊELSE Ê Êslot1timeout: Ê ÊslotValues[0] = 48 ÊENDIF ÊIF slot2Present = 1 THEN Ê ÊSEROUT2 slot2pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot2pin, PICbaudMode, slotDelay, slot2timeout, [rxFromPIC] Ê ÊslotValues[1] = rxFromPIC ÊELSE Ê Êslot2timeout: Ê ÊslotValues[1] = 48 ÊENDIF ÊIF slot3Present = 1 THEN Ê ÊSEROUT2 slot3pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot3pin, PICbaudMode, slotDelay, slot3timeout, [rxFromPIC] Ê ÊslotValues[2] = rxFromPIC ÊELSE Ê Êslot3timeout: Ê ÊslotValues[2] = 48 ÊENDIF ÊIF slot4Present = 1 THEN Ê ÊSEROUT2 slot4pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot4pin, PICbaudMode, slotDelay, slot4timeout, [rxFromPIC] Ê ÊslotValues[3] = rxFromPIC ÊELSE Ê Êslot4timeout: Ê ÊslotValues[3] = 48 ÊENDIF ÊIF slot5Present = 1 THEN Ê ÊSEROUT2 slot5pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot5pin, PICbaudMode, slotDelay, slot5timeout, [rxFromPIC] Ê ÊslotValues[4] = rxFromPIC ÊELSE Ê Êslot5timeout: Ê ÊslotValues[4] = 48 ÊENDIF ÊIF slot6Present = 1 THEN Ê ÊSEROUT2 slot6pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot6pin, PICbaudMode, slotDelay, slot6timeout, [rxFromPIC] Ê ÊslotValues[5] = rxFromPIC ÊELSE Ê Êslot6timeout: Ê ÊslotValues[5] = 48 ÊENDIF ÊIF slot7Present = 1 THEN Ê ÊSEROUT2 slot7pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot7pin, PICbaudMode, slotDelay, slot7timeout, [rxFromPIC] Ê ÊslotValues[6] = rxFromPIC ÊELSE Ê Êslot7timeout: Ê ÊslotValues[6] = 48 ÊENDIF ÊIF slot8Present = 1 THEN Ê ÊSEROUT2 slot8pin, PICbaudMode, [askForID] Ê ÊSERIN2 slot8pin, PICbaudMode, slotDelay, slot8timeout, [rxFromPIC] Ê ÊslotValues[7] = rxFromPIC ÊELSE Ê Êslot8timeout: Ê ÊslotValues[7] = 48 ÊENDIF ÊSEROUT2 txpin, BTbaudMode, ["a", 32] ÊFOR i = 0 TO 7 Ê ÊSEROUT2 txpin, BTbaudMode, [slotValues[i], 32] ÊNEXT i ÊSEROUT2 txpin, BTbaudMode, [DEC volume, 32] ÊSEROUT2 txpin, BTbaudMode, ["z", 10, 13] ÊGOTO timedout