/** * Try and get a character for transmission. * \return The next byte to transmit or -1 if there is none. */ int jshGetCharToTransmit( IOEventFlags device // The device being looked at for a transmission. ) { if (DEVICE_IS_USART(device)) { JshSerialDeviceState *deviceState = &jshSerialDeviceStates[TO_SERIAL_DEVICE_STATE(device)]; if ((*deviceState)&SDS_XOFF_PENDING) { (*deviceState) = ((*deviceState)&(~SDS_XOFF_PENDING)) | SDS_XOFF_SENT; return 19/*XOFF*/; } if ((*deviceState)&SDS_XON_PENDING) { (*deviceState) = ((*deviceState)&(~(SDS_XON_PENDING|SDS_XOFF_SENT))); return 17/*XON*/; } } unsigned char tempTail = txTail; while (txHead != tempTail) { if (IOEVENTFLAGS_GETTYPE(txBuffer[tempTail].flags) == device) { unsigned char data = txBuffer[tempTail].data; if (tempTail != txTail) { // so we weren't right at the back of the queue // we need to work back from tempTail (until we hit tail), shifting everything forwards unsigned char this = tempTail; unsigned char last = (unsigned char)((this+TXBUFFERMASK)&TXBUFFERMASK); while (this!=txTail) { // if this==txTail, then last is before it, so stop here txBuffer[this] = txBuffer[last]; this = last; last = (unsigned char)((this+TXBUFFERMASK)&TXBUFFERMASK); } } txTail = (unsigned char)((txTail+1)&TXBUFFERMASK); // advance the tail return data; // return data }
/** * Initialize all the devices. */ void jshInitDevices() { // called from jshInit unsigned int i; // setup flow control for (i=0;i<sizeof(jshSerialDeviceStates) / sizeof(JshSerialDeviceState);i++) jshSerialDeviceStates[i] = SDS_NONE; jshSerialDeviceStates[TO_SERIAL_DEVICE_STATE(EV_USBSERIAL)] = SDS_FLOW_CONTROL_XON_XOFF; // set up callbacks for events for (i=EV_EXTI0;i<=EV_EXTI_MAX;i++) jshEventCallbacks[i-EV_EXTI0] = 0; }
/** Reset any devices that could have been set up differently by JS code. * Called from jshReset */ void jshResetDevices() { unsigned int i; // Reset list of pins that were set manually jshResetPinStateIsManual(); // setup flow control for (i=0;i<sizeof(jshSerialDeviceStates) / sizeof(JshSerialDeviceState);i++) jshSerialDeviceStates[i] = SDS_NONE; jshSerialDeviceStates[TO_SERIAL_DEVICE_STATE(EV_USBSERIAL)] = SDS_FLOW_CONTROL_XON_XOFF; // set up callbacks for events for (i=EV_EXTI0;i<=EV_EXTI_MAX;i++) jshEventCallbacks[i-EV_EXTI0] = 0; }
/** Reset any devices that could have been set up differently by JS code. * Called from jshReset */ void jshResetDevices() { unsigned int i; // Reset list of pins that were set manually jshResetPinStateIsManual(); // setup flow control for (i=0;i<sizeof(jshSerialDeviceStates) / sizeof(JshSerialDeviceState);i++) jshSerialDeviceStates[i] = SDS_NONE; jshSerialDeviceStates[TO_SERIAL_DEVICE_STATE(EV_USBSERIAL)] = SDS_FLOW_CONTROL_XON_XOFF; // reset callbacks for events for (i=EV_EXTI0;i<=EV_EXTI_MAX;i++) jshEventCallbacks[i-EV_EXTI0] = 0; // Reset pin state for button #ifdef BTN1_PININDEX #ifdef BTN1_PINSTATE jshSetPinStateIsManual(BTN1_PININDEX, true); // so subsequent reads don't overwrite the state jshPinSetState(BTN1_PININDEX, BTN1_PINSTATE); #else jshPinSetState(BTN1_PININDEX, JSHPINSTATE_GPIO_IN); #endif #endif }