{\r
usb = u;\r
nl_in_rx = 0;\r
+ attach = attached = false;\r
}\r
\r
int USBSerial::_putc(int c)\r
{\r
-// send((uint8_t *)&c, 1);\r
+ if (!attached)\r
+ return 1;\r
if (c == '\r')\r
return 1;\r
if (txbuf.free())\r
\r
int USBSerial::_getc()\r
{\r
+ if (!attached)\r
+ return 0;\r
uint8_t c = 0;\r
setled(4, 1); while (rxbuf.isEmpty()); setled(4, 0);\r
rxbuf.dequeue(&c);\r
\r
int USBSerial::puts(const char *str)\r
{\r
+ if (!attached)\r
+ return strlen(str);\r
int i = 0;\r
while (*str)\r
{\r
\r
uint16_t USBSerial::writeBlock(uint8_t * buf, uint16_t size)\r
{\r
+ if (!attached)\r
+ return size;\r
if (size > txbuf.free())\r
{\r
size = txbuf.free();\r
\r
void USBSerial::on_module_loaded()\r
{\r
- this->register_for_event(ON_MAIN_LOOP);\r
-// this->kernel->streams->append_stream(this);\r
+ this->register_for_event(ON_IDLE);\r
}\r
\r
-void USBSerial::on_main_loop(void *argument)\r
+void USBSerial::on_idle(void *argument)\r
{\r
-// this->kernel->streams->printf("!");\r
+ if (attach != attached)\r
+ {\r
+ if (attach)\r
+ {\r
+ attached = true;\r
+ kernel->streams->append_stream(this);\r
+ writeBlock((uint8_t *) "Smoothie\nok\n", 12);\r
+ }\r
+ else\r
+ {\r
+ attached = false;\r
+ kernel->streams->remove_stream(this);\r
+ txbuf.flush();\r
+ rxbuf.flush();\r
+ nl_in_rx = 0;\r
+ }\r
+ }\r
if (nl_in_rx)\r
{\r
string received;\r
\r
void USBSerial::on_attach()\r
{\r
- this->kernel->streams->append_stream(this);\r
- writeBlock((uint8_t *) "Smoothie\nok\n", 12);\r
+ attach = true;\r
}\r
\r
void USBSerial::on_detach()\r
{\r
- this->kernel->streams->remove_stream(this);\r
- txbuf.flush();\r
- rxbuf.flush();\r
- nl_in_rx = 0;\r
+ attach = false;\r
}\r
public:\r
USBSerial(USB *);\r
\r
- virtual int _putc(int c);\r
- virtual int _getc();\r
- virtual int puts(const char *);\r
+ int _putc(int c);\r
+ int _getc();\r
+ int puts(const char *);\r
\r
uint8_t available();\r
\r
CircBuffer<uint8_t> rxbuf;\r
CircBuffer<uint8_t> txbuf;\r
\r
- virtual void on_module_loaded(void);\r
- virtual void on_main_loop(void *);\r
+ void on_module_loaded(void);\r
+ void on_idle(void *);\r
\r
protected:\r
// virtual bool EpCallback(uint8_t, uint8_t);\r
virtual void on_attach(void);\r
virtual void on_detach(void);\r
\r
+ volatile bool attach;\r
+ bool attached;\r
+\r
volatile int nl_in_rx;\r
private:\r
USB *usb;\r