text chat demo application for sx1232

Dependencies:   SX1232 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "sx1232.h"
00002 
00003 /* Chat application over text console via SerialPC.
00004  * Tested with teraterm.
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *     http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 // pins for freescale freedom:
00020 //           mosi, miso, sclk,   cs,  rst, dio0
00021 SX1232 radio(PTD2, PTD3, PTD1, PTD0, PTD5, PTA13);
00022 
00023 Serial pc(USBTX, USBRX);
00024 
00025 void radio_init_user()
00026 {    
00027     // default narrow-band settings
00028     radio.set_bitrate(4800);
00029     radio.set_tx_fdev_hz(5000); //   tx deviation
00030     radio.set_rx_dcc_bw_hz(0, 10000);  // rx bandwidth  
00031     radio.enable_afc(1);
00032     
00033    // pick your own frequency within your regulatory limits.
00034     radio.set_frf_MHz(914.1);
00035     
00036     /* radio.RegPaConfig.bits.OutputPower = X
00037      * radio.RegPaConfig.bits.PaSelect = 1 PA_BOOST
00038      * radio.write_reg(REG_PACONFIG, RegPaConfig.octet); */
00039 }
00040 
00041 void
00042 service_radio()
00043 {
00044     int len;
00045 
00046     switch (radio.service_action) {
00047         case SERVICE_READ_FIFO:  // this occurs when CrcOk in DIO1 pin from radio
00048             radio.service_action = SERVICE_NONE;
00049             len = radio.read_fifo();
00050             radio.rx_buf[len] = 0; // null terminate for printing text
00051             printf("%s\r\n", radio.rx_buf);
00052             break;
00053         case SERVICE_ERROR:
00054             radio.service_action = SERVICE_NONE;
00055             printf("dio0_callback() %d\r\n", radio.RegDioMapping1.bits.Dio0Mapping);
00056             break;
00057         case SERVICE_TX_DONE:
00058             radio.service_action = SERVICE_NONE;
00059             radio.start_rx();
00060             break;
00061         default: // nothing necessary to do
00062             break;
00063     } // ...switch (radio.service_action)            
00064 }
00065         
00066 int main()
00067 {
00068     int txbuf_idx = 0;
00069     const int maxmsg = sizeof(radio.tx_buf)-1;
00070     
00071     radio_init_user();
00072     radio.start_rx();
00073     
00074     printf("\r\nsx1232_chat\r\n");
00075     while (1) {
00076         if (pc.readable()) {
00077             char c = pc.getc();
00078             if (c == 8 && txbuf_idx > 0) { // backspace
00079                 pc.putc(8);
00080                 pc.putc(' ');
00081                 pc.putc(8);
00082                 txbuf_idx--;
00083             } else if (c == '\r') {
00084                 //radio.tx_buf[txbuf_idx] = 0; // null terminate (if printing)
00085                 radio.start_tx(txbuf_idx);
00086                 printf("\r\n");
00087                 txbuf_idx = 0;
00088             } else if (txbuf_idx < maxmsg) {
00089                 radio.tx_buf[txbuf_idx++] = c;
00090                 pc.putc(c);
00091             }
00092         } else
00093             service_radio();
00094     } // ...while(1)
00095 
00096 }