Subversion Repositories HomeAutomation

Rev

Rev 1854 | Rev 1858 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1854 Rev 1856
Line 110... Line 110...
110
#if sns_irTransceive_PRONTO_SUPPORT==1
110
#if sns_irTransceive_PRONTO_SUPPORT==1
111
volatile uint32_t sns_irTransceive_LastPronto=0;
111
volatile uint32_t sns_irTransceive_LastPronto=0;
112
static uint8_t activeChannel = 0;
112
static uint8_t activeChannel = 0;
113
#define sns_irTransceive_MAXTIMING (16*1000)
113
#define sns_irTransceive_MAXTIMING (16*1000)
114
 
114
 
115
typedef enum {
-
 
116
    PRONTO_RESPONSE_OK              = 0,
-
 
117
    PRONTO_RESPONSE_STOPPED         = 1,
-
 
118
    PRONTO_RESPONSE_WAITING         = 2,
-
 
119
    PRONTO_RESPONSE_TRANSMITTING    = 4,
-
 
120
 
-
 
121
    PRONTO_RESPONSE_ALREADY_STOPPED = 241,
-
 
122
    PRONTO_RESPONSE_ABORTED         = 243,
-
 
123
    PRONTO_RESPONSE_ERROR           = 255,
-
 
124
} prontoResponse_t;
-
 
125
 
-
 
126
static void pronto_sendResponse(uint8_t channel, prontoResponse_t responseValue)
115
static void pronto_sendResponse(uint8_t channel, uint8_t responseValue)
127
{
116
{
128
    StdCan_Msg_t msg;
117
    StdCan_Msg_t msg;
129
    StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
118
    StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
130
    StdCan_Set_direction(msg.Header, DIRECTIONFLAG_FROM_OWNER);
119
    StdCan_Set_direction(msg.Header, DIRECTIONFLAG_FROM_OWNER);
131
    msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
120
    msg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRTRANSCEIVE;
Line 297... Line 286...
297
    msg.Length = j;
286
    msg.Length = j;
298
    /* data kept from loop, if any */
287
    /* data kept from loop, if any */
299
    /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
288
    /* buffers will be filled when sending more than 2-3 messages, so retry until sent */
300
    while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
289
    while (StdCan_Put(&msg) != StdCan_Ret_OK) {}
301
    _delay_ms(1);
290
    _delay_ms(1);
302
}
291
}
303
 
292
 
304
 
293
 
305
static int decodeProntoData(int channel, uint8_t data)
294
static int decodeProntoData(int channel, uint8_t data)
306
{
295
{
307
    static uint8_t waitingForLSB = FALSE;
296
    static uint8_t waitingForLSB = FALSE;
308
    static uint8_t previousValueWas16bit = FALSE;
297
    static uint8_t previousValueWas16bit = FALSE;
309
 
298
 
Line 476... Line 465...
476
                    Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
465
                    Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
477
                    Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
466
                    Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
478
                    if (power&0x1)
467
                    if (power&0x1)
479
                    {
468
                    {
480
                        Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
469
                        Pca95xx_set_in(sns_irTransceive_TX2_PWRl);
481
                    }
470
                    }
482
                    if (power&0x2)
471
                    if (power&0x2)
483
                    {
472
                    {
484
                        Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
473
                        Pca95xx_set_in(sns_irTransceive_TX2_PWRh);
485
                    }
474
                    }
486
#endif
475
#endif
487
                    break;
476
                    break;
488
            }
477
            }
489
            irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
478
            irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
490
        }
479
        }
491
#endif
480
#endif
492
    }
481
    }
493
}
482
}
494
 
483
 
495
 
484
 
496
void sns_irTransceive_Init(void)
485
void sns_irTransceive_Init(void)
497
{
486
{
Line 565... Line 554...
565
 
554
 
566
    Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
555
    Pca95xx_clr_pin(sns_irTransceive_TX0_PWRl);
567
    Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
556
    Pca95xx_set_out(sns_irTransceive_TX0_PWRl);
568
    Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
557
    Pca95xx_clr_pin(sns_irTransceive_TX0_PWRh);
569
    Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
558
    Pca95xx_set_out(sns_irTransceive_TX0_PWRh);
570
 
559
 
571
    Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
560
    Pca95xx_clr_pin(sns_irTransceive_TX1_PWRl);
572
    Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
561
    Pca95xx_set_out(sns_irTransceive_TX1_PWRl);
573
    Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
562
    Pca95xx_clr_pin(sns_irTransceive_TX1_PWRh);
574
    Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
563
    Pca95xx_set_out(sns_irTransceive_TX1_PWRh);
575
 
564
 
576
    Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
565
    Pca95xx_clr_pin(sns_irTransceive_TX2_PWRl);
577
    Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
566
    Pca95xx_set_out(sns_irTransceive_TX2_PWRl);
578
    Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
567
    Pca95xx_clr_pin(sns_irTransceive_TX2_PWRh);
579
    Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
568
    Pca95xx_set_out(sns_irTransceive_TX2_PWRh);
580
#endif
569
#endif
581
 
570
 
582
#ifdef sns_irTransceive_USEEEPROM
571
#ifdef sns_irTransceive_USEEEPROM
583
    if (EEDATA_OK)
572
    if (EEDATA_OK)
584
    {
573
    {
585
      /* Use stored data to set initial values for the module */
574
      /* Use stored data to set initial values for the module */
586
        sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower), eeprom_read_word(EEDATA16.ch0_modfreq));
575
        sns_irTransceive_setConfig(0, eeprom_read_byte(EEDATA.ch0_config), eeprom_read_byte(EEDATA.ch0_txpower), eeprom_read_word(EEDATA16.ch0_modfreq));
587
        sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower), eeprom_read_word(EEDATA16.ch1_modfreq));
576
        sns_irTransceive_setConfig(1, eeprom_read_byte(EEDATA.ch1_config), eeprom_read_byte(EEDATA.ch1_txpower), eeprom_read_word(EEDATA16.ch1_modfreq));
588
        sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower), eeprom_read_word(EEDATA16.ch2_modfreq));
577
        sns_irTransceive_setConfig(2, eeprom_read_byte(EEDATA.ch2_config), eeprom_read_byte(EEDATA.ch2_txpower), eeprom_read_word(EEDATA16.ch2_modfreq));
589
    }
578
    }
590
    else
579
    else
591
    {
580
    {
592
    /* The CRC of the EEPROM is not correct, store default values and update CRC */
581
    /* The CRC of the EEPROM is not correct, store default values and update CRC */
593
        eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
582
        eeprom_write_byte_crc(EEDATA.ch0_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
594
        eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
583
        eeprom_write_byte_crc(EEDATA.ch0_txpower, 0, WITHOUT_CRC);
595
        eeprom_write_word_crc(EEDATA16.ch0_modfreq, sns_irTransceive_BaseFrq/38000UL, WITHOUT_CRC);
584
        eeprom_write_word_crc(EEDATA16.ch0_modfreq, sns_irTransceive_BaseFrq/38000UL, WITHOUT_CRC);
596
        eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
585
        eeprom_write_byte_crc(EEDATA.ch1_config, CAN_MODULE_ENUM_IRTRANSCEIVE_IRCONFIG_DIRECTION_AUTO, WITHOUT_CRC);
Line 616... Line 605...
616
            irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
605
            irRxChannel[channel].state = sns_irTransceive_STATE_START_RECEIVE;
617
            break;
606
            break;
618
 
607
 
619
        case sns_irTransceive_STATE_START_RECEIVE:
608
        case sns_irTransceive_STATE_START_RECEIVE:
620
            IrTransceiver_ResetRx(channel);
609
            IrTransceiver_ResetRx(channel);
621
            cli();
610
            cli();
622
            irRxChannel[channel].newData = FALSE;
611
            irRxChannel[channel].newData = FALSE;
623
            sei();
612
            sei();
624
            irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
613
            irRxChannel[channel].state = sns_irTransceive_STATE_RECEIVING;
625
            break;
614
            break;
626
 
615
 
627
        case sns_irTransceive_STATE_RECEIVING:
616
        case sns_irTransceive_STATE_RECEIVING:
628
            if (irRxChannel[channel].newData == TRUE) {
617
            if (irRxChannel[channel].newData == TRUE) {
629
                /* TODO: move this line to the RX callback */
618
                /* TODO: move this line to the RX callback */
630
                IrTransceiver_DisableRx(channel);
619
                IrTransceiver_DisableRx(channel);
631
                cli();
620
                cli();
632
                irRxChannel[channel].newData = FALSE;
621
                irRxChannel[channel].newData = FALSE;
633
                sei();
622
                sei();
634
 
623
 
635
                /* Let protocol driver parse and then send on CAN */
624
                /* Let protocol driver parse and then send on CAN */
636
                uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
625
                uint8_t res2 = parseProtocol(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, &irRxChannel[channel].proto);
637
                if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
626
                if (res2 == IR_OK && irRxChannel[channel].proto.protocol != IR_PROTO_UNKNOWN) {
638
                    irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
627
                    irTxMsg.Data[0] = 0xf&CAN_MODULE_ENUM_PHYSICAL_IR_STATUS_PRESSED;
639
                    irTxMsg.Data[0] |= channel<<4;
628
                    irTxMsg.Data[0] |= channel<<4;
640
                    irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
629
                    irTxMsg.Data[1] = irRxChannel[channel].proto.protocol;
641
                    irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
630
                    irTxMsg.Data[2] = (irRxChannel[channel].proto.data>>24)&0xff;
642
                    irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
631
                    irTxMsg.Data[3] = (irRxChannel[channel].proto.data>>16)&0xff;
643
                    irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
632
                    irTxMsg.Data[4] = (irRxChannel[channel].proto.data>>8)&0xff;
644
                    irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
633
                    irTxMsg.Data[5] = irRxChannel[channel].proto.data&0xff;
645
 
634
 
646
                    StdCan_Put(&irTxMsg);
635
                    StdCan_Put(&irTxMsg);
647
                }
636
                }
648
                else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
637
                else if (irRxChannel[channel].proto.protocol == IR_PROTO_UNKNOWN)
649
                {
638
                {
650
#if (sns_irTransceive_SEND_DEBUG==1)
639
#if (sns_irTransceive_SEND_DEBUG==1)
651
                    send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
640
                    send_debug(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen);
652
                    irRxChannel[channel].proto.timeout=300;
641
                    irRxChannel[channel].proto.timeout=300;
653
#elif sns_irTransceive_PRONTO_SUPPORT==1
642
#elif sns_irTransceive_PRONTO_SUPPORT==1
654
                    pronto_sendData(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, channel, irRxChannel[channel].modfreq);
643
                    pronto_sendData(irRxChannel[channel].rxbuf, irRxChannel[channel].rxlen, channel, irRxChannel[channel].modfreq);
655
                    irRxChannel[channel].proto.timeout=1;
644
                    irRxChannel[channel].proto.timeout=1;
656
#endif
645
#endif
657
                }
646
                }
658
 
647
 
659
                /* Enable the receiver again */
648
                /* Enable the receiver again */
660
                IrTransceiver_EnableRx(channel);
649
                IrTransceiver_EnableRx(channel);
661
 
650
 
662
                irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
651
                irRxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
663
            }
652
            }
664
            break;
653
            break;
665
 
654
 
666
        case sns_irTransceive_STATE_START_PAUSE:
655
        case sns_irTransceive_STATE_START_PAUSE:
667
            /* set a timer so we can send release button event when no new IR is arriving */
656
            /* set a timer so we can send release button event when no new IR is arriving */
668
            Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
657
            Timer_SetTimeout(irRxChannel[channel].timerNum, irRxChannel[channel].proto.timeout, TimerTypeOneShot, 0);
669
            irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
658
            irRxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
670
            break;
659
            break;
671
 
660
 
672
        case sns_irTransceive_STATE_PAUSING:
661
        case sns_irTransceive_STATE_PAUSING:
673
            /* reset timer if new IR arrived */
662
            /* reset timer if new IR arrived */
674
            if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
663
            if (irRxChannel[channel].newData == TRUE || IrTransceiver_GetStoreEnableRx(channel) == TRUE) {
675
                cli();
664
                cli();
Line 698... Line 687...
698
            }
687
            }
699
            irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
688
            irRxChannel[channel].state = sns_irTransceive_STATE_IDLE;
700
            break;
689
            break;
701
 
690
 
702
        default:
691
        default:
703
            break;
692
            break;
704
        }
693
        }
705
#endif
694
#endif
706
 
695
 
707
#if IR_TX_ENABLE==1
696
#if IR_TX_ENABLE==1
708
        switch (irTxChannel[channel].state)
697
        switch (irTxChannel[channel].state)
Line 717... Line 706...
717
                IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen);
706
                IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].txlen);
718
                irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
707
                irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
719
            }
708
            }
720
            else {
709
            else {
721
                irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
710
                irTxChannel[channel].state = sns_irTransceive_STATE_IDLE;
722
            }
711
            }
723
            break;
712
            break;
724
        }
713
        }
725
 
714
 
726
        case sns_irTransceive_STATE_START_TRANSMIT_PRONTO:
715
        case sns_irTransceive_STATE_START_TRANSMIT_PRONTO:
727
        {
716
        {
728
            /* Start IR transmission. */
717
            /* Start IR transmission. */
729
            IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].onceSeqLen - 1);
718
            IrTransceiver_Transmit(channel, irTxChannel[channel].txbuf, 0, irTxChannel[channel].onceSeqLen - 1);
730
 
719
 
731
            /* Enter transmitting state. */
720
            /* Enter transmitting state. */
732
            irTxChannel[channel].sendingPronto = TRUE;
721
            irTxChannel[channel].sendingPronto = TRUE;
733
            irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
722
            irTxChannel[channel].state = sns_irTransceive_STATE_TRANSMITTING;
734
            break;
723
            break;
735
        }
724
        }
736
 
725
 
737
        case sns_irTransceive_STATE_PRONTO_REPEAT:
726
        case sns_irTransceive_STATE_PRONTO_REPEAT:
738
        {
727
        {
739
            /* Check if done. */
728
            /* Check if done. */
740
            if ((irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats && irTxChannel[channel].proto.repeats != 0xFF) || irTxChannel[channel].stopSend) {
729
            if ((irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats && irTxChannel[channel].proto.repeats != 0xFF) || irTxChannel[channel].stopSend) {
-
 
730
                /* Pronto transmission was stopped/completed */
-
 
731
                pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
741
                irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
732
                irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
742
                break;
733
                break;
743
            }
734
            }
744
 
735
 
745
            if (irTxChannel[channel].repeatCount < 255) {
736
            if (irTxChannel[channel].repeatCount < 255) {
746
                irTxChannel[channel].repeatCount++;
737
                irTxChannel[channel].repeatCount++;
747
            }
738
            }
748
 
739
 
749
            /* Repeat sequence exists? */
740
            /* Repeat sequence exists? */
750
            if (irTxChannel[channel].repSeqLen != 0) {
741
            if (irTxChannel[channel].repSeqLen != 0) {
751
                /* Use repeat seq */
742
                /* Use repeat seq */
752
                uint16_t offset = ((uint16_t)irTxChannel[channel].onceSeqLen);
743
                uint16_t offset = ((uint16_t)irTxChannel[channel].onceSeqLen);
Line 774... Line 765...
774
                    /* First repeat, or no repeat sequence defined => use last passive time in once seq */
765
                    /* First repeat, or no repeat sequence defined => use last passive time in once seq */
775
                    if(irTxChannel[channel].repeatCount == 0 || irTxChannel[channel].repSeqLen == 0)
766
                    if(irTxChannel[channel].repeatCount == 0 || irTxChannel[channel].repSeqLen == 0)
776
                    {
767
                    {
777
                        /* Use last passive time as timeout */
768
                        /* Use last passive time as timeout */
778
                        uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].onceSeqLen - 1] / 1000;
769
                        uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].onceSeqLen - 1] / 1000;
779
                        Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
770
                        Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
780
                    }
771
                    }
781
                    /* Second, or later repeat => use last passive time in repeat seq */
772
                    /* Second, or later repeat => use last passive time in repeat seq */
782
                    else
773
                    else
783
                    {
774
                    {
784
                        /* Use last passive time as timeout */
775
                        /* Use last passive time as timeout */
785
                        uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1]  / 1000;
776
                        uint16_t lastPasTime = irTxChannel[channel].txbuf[irTxChannel[channel].txlen - 1]  / 1000;
786
                        Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
777
                        Timer_SetTimeout(irTxChannel[channel].timerNum, lastPasTime==0 ? 1 : lastPasTime, TimerTypeOneShot, 0);
787
                    }
778
                    }
788
                    irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
779
                    irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
789
                }
780
                }
790
                else
781
                else
791
                {
782
                {
792
                    irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
783
                    irTxChannel[channel].state = sns_irTransceive_STATE_START_PAUSE;
793
                }
784
                }
794
            }
785
            }
795
            break;
786
            break;
796
        }
787
        }
Line 808... Line 799...
808
            {
799
            {
809
                irTxChannel[channel].proto.framecnt++;
800
                irTxChannel[channel].proto.framecnt++;
810
            }
801
            }
811
 
802
 
812
            irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
803
            irTxChannel[channel].state = sns_irTransceive_STATE_PAUSING;
813
            break;
804
            break;
814
        }
805
        }
815
 
806
 
816
        case sns_irTransceive_STATE_PAUSING:
807
        case sns_irTransceive_STATE_PAUSING:
817
        {
808
        {
818
            if (irTxChannel[channel].sendingPronto)
809
            if (irTxChannel[channel].sendingPronto)
Line 830... Line 821...
830
            }
821
            }
831
 
822
 
832
            /* Transmission is stopped when such command is recevied on can */
823
            /* Transmission is stopped when such command is recevied on can */
833
            if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
824
            if (irTxChannel[channel].stopSend == TRUE && irTxChannel[channel].repeatCount >= irTxChannel[channel].proto.repeats)
834
            {
825
            {
-
 
826
                /* Non-pronto transmission was stopped/completed */
835
                /* TODO: maybe send message on can for status? to confirm stopped sending, ready for new command */
827
                /* TODO: Not sure we want this response when sending non-pronto data, but probably doesn't hurt? /jm */
-
 
828
                pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_STOPPED);
-
 
829
 
836
                irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
830
                irTxChannel[channel].state = sns_irTransceive_STATE_START_IDLE;
837
            }
831
            }
838
            break;
832
            break;
839
        }
833
        }
840
 
834
 
Line 912... Line 906...
912
        uint8_t channel = rxMsg->Data[0] >> 4;
906
        uint8_t channel = rxMsg->Data[0] >> 4;
913
 
907
 
914
        /* Sanity check. */
908
        /* Sanity check. */
915
        if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
909
        if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
916
            /* Invalid channel */
910
            /* Invalid channel */
917
            pronto_sendResponse(channel, PRONTO_RESPONSE_ERROR);
911
            pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
918
            break;
912
            break;
919
        }
913
        }
920
        if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
914
        if (irTxChannel[channel].state != sns_irTransceive_STATE_IDLE) {
921
            /* We're already transmitting on this channel */
915
            /* We're already transmitting on this channel */
922
            pronto_sendResponse(channel, PRONTO_RESPONSE_ABORTED);
916
            pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ABORTED);
923
            break;
917
            break;
924
        }
918
        }
925
 
919
 
926
        /* Check if format is supported. */
920
        /* Check if format is supported. */
927
        if ((((uint16_t)(rxMsg->Data[0] & 0x0F) << 8) | (uint16_t)(rxMsg->Data[1])) != 0) {
921
        if ((((uint16_t)(rxMsg->Data[0] & 0x0F) << 8) | (uint16_t)(rxMsg->Data[1])) != 0) {
928
            /* Invalid pronto format */
922
            /* Invalid pronto format */
929
            pronto_sendResponse(channel, PRONTO_RESPONSE_ERROR);
923
            pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
930
            break;
924
            break;
931
        }
925
        }
932
 
926
 
933
        /* Extract received data. */
927
        /* Extract received data. */
934
 
928
 
935
        uint32_t divider = (rxMsg->Data[2] << 8) | (rxMsg->Data[3] << 0);
929
        uint32_t divider = (rxMsg->Data[2] << 8) | (rxMsg->Data[3] << 0);
936
        if (divider == 0) {
930
        if (divider == 0) {
937
            /* Invalid wFrqDiv value */
931
            /* Invalid wFrqDiv value */
938
            pronto_sendResponse(channel, PRONTO_RESPONSE_ERROR);
932
            pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
939
            break;
933
            break;
940
        }
934
        }
941
        uint32_t freqkHz = (sns_irTransceive_BaseFrq / (divider*1000));
935
        uint32_t freqkHz = (sns_irTransceive_BaseFrq / (divider*1000));
942
        if (freqkHz == 0) {
936
        if (freqkHz == 0) {
943
            /* Invalid wFrqDiv value */
937
            /* Invalid wFrqDiv value */
944
            pronto_sendResponse(channel, PRONTO_RESPONSE_ERROR);
938
            pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
945
            break;
939
            break;
946
        }
940
        }
947
        irTxChannel[channel].proto.modfreq = freqkHz;
941
        irTxChannel[channel].proto.modfreq = freqkHz;
948
 
942
 
949
        /* TODO: reconfigure with new modfreq, if no other channels sending pronto */
943
        /* TODO: reconfigure with new modfreq, if no other channels sending pronto */
Line 957... Line 951...
957
        irTxChannel[channel].txlen = 0;
951
        irTxChannel[channel].txlen = 0;
958
        irTxChannel[channel].expectedSeqNr = 0;
952
        irTxChannel[channel].expectedSeqNr = 0;
959
        activeChannel = channel;
953
        activeChannel = channel;
960
 
954
 
961
        /* We're now waiting for pronto data */
955
        /* We're now waiting for pronto data */
962
        pronto_sendResponse(channel, PRONTO_RESPONSE_WAITING);
956
        pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
963
        break;
957
        break;
964
    }
958
    }
965
 
959
 
966
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTOP:
960
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOSTOP:
967
    {
961
    {
Line 969... Line 963...
969
        uint8_t channel = rxMsg->Data[0] >> 4;
963
        uint8_t channel = rxMsg->Data[0] >> 4;
970
 
964
 
971
        /* Sanity check. */
965
        /* Sanity check. */
972
        if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
966
        if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
973
            /* Invalid channel */
967
            /* Invalid channel */
974
            pronto_sendResponse(channel, PRONTO_RESPONSE_ERROR);
968
            pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
975
            break;
969
            break;
976
        }
970
        }
977
 
971
 
978
        irTxChannel[channel].stopSend = TRUE;
972
        irTxChannel[channel].stopSend = TRUE;
979
 
973
 
980
        /* Pronto transmission was stopped */
974
        /* Response will be sent when transmission has been completed (i.e. stopped) */
981
        pronto_sendResponse(channel, PRONTO_RESPONSE_STOPPED);
-
 
982
        break;
975
        break;
983
    }
976
    }
984
 
977
 
985
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOCONTINUE:
978
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOCONTINUE:
986
    {
979
    {
Line 988... Line 981...
988
        uint8_t channel = rxMsg->Data[0] >> 4;
981
        uint8_t channel = rxMsg->Data[0] >> 4;
989
 
982
 
990
        /* Sanity check. */
983
        /* Sanity check. */
991
        if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
984
        if (channel >= IR_SUPPORTED_NUM_CHANNELS) {
992
            /* Invalid channel */
985
            /* Invalid channel */
993
            pronto_sendResponse(channel, PRONTO_RESPONSE_ERROR);
986
            pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
994
            break;
987
            break;
995
        }
988
        }
996
 
989
 
997
        /* State check. We must already be in some transmit state */
990
        /* State check. We must already be in some transmit state */
998
        /* TODO: better way to check this? /jm */
991
        /* TODO: better way to check this? /jm */
Line 1001... Line 994...
1001
            irTxChannel[activeChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
994
            irTxChannel[activeChannel].state != sns_irTransceive_STATE_PRONTO_REPEAT &&
1002
            irTxChannel[activeChannel].state != sns_irTransceive_STATE_PAUSING &&
995
            irTxChannel[activeChannel].state != sns_irTransceive_STATE_PAUSING &&
1003
            irTxChannel[activeChannel].state != sns_irTransceive_STATE_START_PAUSE)
996
            irTxChannel[activeChannel].state != sns_irTransceive_STATE_START_PAUSE)
1004
        {
997
        {
1005
            /* Too late to continue! Not in TX state anymore */
998
            /* Too late to continue! Not in TX state anymore */
1006
            pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ALREADY_STOPPED);
999
            pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ALREADYSTOPPED);
1007
            break;
1000
            break;
1008
        }
1001
        }
1009
 
1002
 
1010
        /* Reset repeat counter, i.e. request additional repeats. */
1003
        /* Reset repeat counter, i.e. request additional repeats. */
1011
        irTxChannel[channel].repeatCount = 0;
1004
        irTxChannel[channel].repeatCount = 0;
1012
 
1005
 
1013
        /* The repeat period was extended. Still transmitting */
1006
        /* The repeat period was extended. Still transmitting */
1014
        pronto_sendResponse(channel, PRONTO_RESPONSE_TRANSMITTING);
1007
        pronto_sendResponse(channel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
1015
        break;
1008
        break;
1016
    }
1009
    }
1017
 
1010
 
1018
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1:  /* Fall through */
1011
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1:  /* Fall through */
1019
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA2:  /* Fall through */
1012
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA2:  /* Fall through */
Line 1032... Line 1025...
1032
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA15: /* Fall through */
1025
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA15: /* Fall through */
1033
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA16:
1026
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA16:
1034
    {
1027
    {
1035
        if (irTxChannel[activeChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
1028
        if (irTxChannel[activeChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
1036
            /* Invalid state. CAN msg probably out of order */
1029
            /* Invalid state. CAN msg probably out of order */
1037
            pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ERROR);
1030
            pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
1038
            break;
1031
            break;
1039
        }
1032
        }
1040
 
1033
 
1041
        if ((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1) != (irTxChannel[activeChannel].expectedSeqNr % 16)) {
1034
        if ((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTODATA1) != (irTxChannel[activeChannel].expectedSeqNr % 16)) {
1042
            /* Unexpected CAN msg, out of order */
1035
            /* Unexpected CAN msg, out of order */
1043
            pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ERROR);
1036
            pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
1044
            irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1037
            irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1045
            break;
1038
            break;
1046
        }
1039
        }
1047
 
1040
 
1048
        /* Decode pronto data */
1041
        /* Decode pronto data */
1049
        for (uint8_t i = 0; i < rxMsg->Length; i++) {
1042
        for (uint8_t i = 0; i < rxMsg->Length; i++) {
1050
            if (decodeProntoData(activeChannel, rxMsg->Data[i]) != 1) {
1043
            if (decodeProntoData(activeChannel, rxMsg->Data[i]) != 1) {
1051
                /* Decode error */
1044
                /* Decode error */
1052
                pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ERROR);
1045
                pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
1053
                irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1046
                irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1054
                return;
1047
                return;
1055
            }
1048
            }
1056
        }
1049
        }
1057
 
1050
 
1058
        irTxChannel[activeChannel].expectedSeqNr++;
1051
        irTxChannel[activeChannel].expectedSeqNr++;
1059
 
1052
 
1060
        /* We're still waiting for more pronto data, or for pronto end */
1053
        /* We're still waiting for more pronto data, or for pronto end */
1061
        pronto_sendResponse(activeChannel, PRONTO_RESPONSE_WAITING);
1054
        pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_WAITING);
1062
        break;
1055
        break;
1063
    }
1056
    }
1064
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1:   /* Fall through */
1057
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1:   /* Fall through */
1065
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND2:   /* Fall through */
1058
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND2:   /* Fall through */
1066
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND3:   /* Fall through */
1059
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND3:   /* Fall through */
Line 1075... Line 1068...
1075
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND12:  /* Fall through */
1068
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND12:  /* Fall through */
1076
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND13:  /* Fall through */
1069
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND13:  /* Fall through */
1077
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND14:  /* Fall through */
1070
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND14:  /* Fall through */
1078
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND15:  /* Fall through */
1071
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND15:  /* Fall through */
1079
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND16:  /* Fall through */
1072
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND16:  /* Fall through */
1080
    case CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND17:
-
 
1081
    {
1073
    {
1082
        if (irTxChannel[activeChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
1074
        if (irTxChannel[activeChannel].state != sns_irTransceive_STATE_PREPARING_PRONTO) {
1083
            /* Invalid state. CAN msg probably out of order */
1075
            /* Invalid state. CAN msg probably out of order */
1084
            pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ERROR);
1076
            pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
1085
            break;
1077
            break;
1086
        }
1078
        }
1087
 
1079
 
1088
        if (((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1) % 16) != (irTxChannel[activeChannel].expectedSeqNr % 16)) {
1080
        if (((rxMsg->Header.Command - CAN_MODULE_CMD_IRTRANSCEIVE_IRPRONTOEND1) % 16) != (irTxChannel[activeChannel].expectedSeqNr % 16)) {
1089
            /* Unexpected CAN msg, out of order */
1081
            /* Unexpected CAN msg, out of order */
1090
            pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ERROR);
1082
            pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
1091
            irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1083
            irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1092
            break;
1084
            break;
1093
        }
1085
        }
1094
 
1086
 
1095
        /* Decode remaining pronto data (if any). -1 because last byte is repeat count */
1087
        /* Decode remaining pronto data (if any). -1 because last byte is repeat count */
1096
        for (uint16_t i = 0; i < rxMsg->Length - 1; i++) {
1088
        for (uint16_t i = 0; i < rxMsg->Length - 1; i++) {
1097
            if (decodeProntoData(activeChannel, rxMsg->Data[i]) < 0) {
1089
            if (decodeProntoData(activeChannel, rxMsg->Data[i]) < 0) {
1098
                /* Decode error */
1090
                /* Decode error */
1099
                pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ERROR);
1091
                pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
1100
                irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1092
                irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_IDLE;
1101
                return;
1093
                return;
1102
            }
1094
            }
1103
        }
1095
        }
1104
 
1096
 
1105
        /* Sanity check */
1097
        /* Sanity check */
1106
        if (irTxChannel[activeChannel].txlen != (((uint16_t)irTxChannel[activeChannel].onceSeqLen + (uint16_t)irTxChannel[activeChannel].repSeqLen))) {
1098
        if (irTxChannel[activeChannel].txlen != (((uint16_t)irTxChannel[activeChannel].onceSeqLen + (uint16_t)irTxChannel[activeChannel].repSeqLen))) {
1107
            /* Length doesn't match the expected length */
1099
            /* Length doesn't match the expected length */
1108
            pronto_sendResponse(activeChannel, PRONTO_RESPONSE_ERROR);
1100
            pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_ERROR);
1109
            irTxChannel[activeChannel].state = sns_irTransceive_STATE_IDLE;
1101
            irTxChannel[activeChannel].state = sns_irTransceive_STATE_IDLE;
1110
            break;
1102
            break;
1111
        }
1103
        }
1112
 
1104
 
1113
        /* Last byte determines nr of repeats. 0xFF means INF, 0 means no repeats */
1105
        /* Last byte determines nr of repeats. 0xFF means INF, 0 means no repeats */
Line 1126... Line 1118...
1126
 
1118
 
1127
        /* Send IR data */
1119
        /* Send IR data */
1128
        irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_TRANSMIT_PRONTO;
1120
        irTxChannel[activeChannel].state = sns_irTransceive_STATE_START_TRANSMIT_PRONTO;
1129
 
1121
 
1130
        /* We're now transmitting the pronto data */
1122
        /* We're now transmitting the pronto data */
1131
        pronto_sendResponse(activeChannel, PRONTO_RESPONSE_TRANSMITTING);
1123
        pronto_sendResponse(activeChannel, CAN_MODULE_ENUM_IRTRANSCEIVE_IRPRONTORESPONSE_RESPONSE_TRANSMITTING);
1132
        break;
1124
        break;
1133
    }
1125
    }
1134
 
1126
 
1135
/* TODO: In IR state machine add sending a response frame when ir stops sending, also implement pronto repeat */
1127
/* TODO: In IR state machine add sending a response frame when ir stops sending, also implement pronto repeat */
1136
#endif
1128
#endif