<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="C">
/*<pre>Program generated from XML*/
<xsl:for-each select="DFA/STATE">
#define <xsl:value-of select="@name"/><xsl:text> </xsl:text><xsl:value-of select="position()"/>
</xsl:for-each>
<xsl:apply-templates/>
/*End of program</pre>*/
</xsl:template>

<xsl:template match="DFA">
{
unsigned int 
      dfa_time,
      dfa_timeout;
unsigned char
      dfa_timerRunning = 0,
      dfa_state = <xsl:value-of select="@start"/>;
  while(1) {
    if(dfa_timerRunning) dfa_time++;
    switch(dfa_state) {
      <xsl:apply-templates/>
    }
  }
}
</xsl:template>

<xsl:template match="STATE">
      case <xsl:value-of select="@name"/>:
          <xsl:apply-templates/>
        break;
</xsl:template>

<xsl:template match="ON">
          if(<xsl:value-of select="@event"/>) {
            <xsl:apply-templates/>
            dfa_state = <xsl:value-of select="@next"/>;
            break;
          }
</xsl:template>


<xsl:template match="T_ON">
  dfa_time = 0;
  dfa_timeout = <xsl:value-of select="@time"/>;
  dfa_timerRunning = 1;
</xsl:template>

<xsl:template match="T_OFF">
  dfa_timerRunning = 0;
</xsl:template>

<xsl:template match="ONTIMEOUT">
        if(dfa_time>=dfa_timeout) {
          dfa_time = 0;
          dfa_timerRunning = 0;
          <xsl:apply-templates/>
          dfa_state = <xsl:value-of select="@next"/>;
          break;
        }
</xsl:template>