现在的位置: 首页 > 综合 > 正文

PWM Demo

2013年09月17日 ⁄ 综合 ⁄ 共 13924字 ⁄ 字号 评论关闭

PWMActivity.java

  1. package com.mini6410.PWM;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.text.Editable;  
  7. import android.text.TextWatcher;  
  8. import android.util.Log;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.CompoundButton;  
  12. import android.widget.EditText;  
  13. import android.widget.ToggleButton;  
  14.   
  15. import com.friendlyarm.AndroidSDK.HardwareControler;  
  16. import com.mini6410.R;  
  17.   
  18. /** 
  19.  *  
  20.  * ClassName:PWMActivity 
  21.  * Reason:   PWM Demo 
  22.  * 
  23.  * @author   snowdream 
  24.  * @version   
  25.  * @since    Ver 1.1 
  26.  * @Date     2011   2012-03-11      17:21 
  27.  * 
  28.  * @see       
  29.  */  
  30. public class PWMActivity extends Activity implements Button.OnClickListener,ToggleButton.OnCheckedChangeListener{  
  31.     public static final String TAG = "PWMActivity";  
  32.   
  33.     /*PWM频率*/  
  34.     public static final int PWM_FREQUENCY_1 = 1;  
  35.     public static final int PWM_FREQUENCY_100 = 100;  
  36.     public static final int PWM_FREQUENCY_200 = 200;  
  37.     public static final int PWM_FREQUENCY_500 = 500;  
  38.     public static final int PWM_FREQUENCY_1000 = 1000;  
  39.     public static final int PWM_FREQUENCY_2000 = 2000;  
  40.     public static final int PWM_FREQUENCY_5000 = 5000;  
  41.     public static final int PWM_FREQUENCY_10000 = 10000;  
  42.   
  43.     /*PWM播放时长*/  
  44.     public static final int PWM_PLAY_PERIOD_FOR_EVER = -1;  
  45.     public static final int PWM_PLAY_PERIOD_100 = 100;  
  46.     public static final int PWM_PLAY_PERIOD_200 = 200;  
  47.     public static final int PWM_PLAY_PERIOD_300 = 300;  
  48.     public static final int PWM_PLAY_PERIOD_500 = 500;  
  49.     public static final int PWM_PLAY_PERIOD_1000 = 1000;  
  50.   
  51.     /*PWM播放间隔*/  
  52.     public static final int PWM_WAIT_PERIOD_100 = 100;  
  53.     public static final int PWM_WAIT_PERIOD_200 = 200;  
  54.     public static final int PWM_WAIT_PERIOD_300 = 300;  
  55.     public static final int PWM_WAIT_PERIOD_500 = 500;  
  56.     public static final int PWM_WAIT_PERIOD_1000 = 1000;  
  57.   
  58.   
  59.     /*播放频率*/  
  60.     public int mfrequency = PWM_FREQUENCY_1000;  
  61.       
  62.     /*频率改变步长Step*/  
  63.     public int mfrequencystep = 100;  
  64.       
  65.     /*播放时长*/  
  66.     public int mplayperiod = PWM_PLAY_PERIOD_1000;  
  67.       
  68.     /*播放间隔*/  
  69.     public int mwaitperiod = PWM_WAIT_PERIOD_1000;  
  70.       
  71.     /*播放次数*/  
  72.     public int mplaynum = 20;  
  73.   
  74.     private boolean mStop = false;  
  75.   
  76.     private Button mButtonSub = null;  
  77.     private Button mButtonAdd = null;  
  78.   
  79.     private ToggleButton mToggleButtonStartForEver= null;  
  80.     private ToggleButton mToggleButtonStartForTimes = null;  
  81.   
  82.     private EditText mEditTextFrequency = null;  
  83.     private EditText mEditTextPlayPeriod = null;  
  84.     private EditText mTextViewWaitPeriod = null;  
  85.     private EditText mEditTextPlayNum = null;  
  86.   
  87.   
  88.     @Override  
  89.     protected void onCreate(Bundle savedInstanceState) {  
  90.         super.onCreate(savedInstanceState);  
  91.         setContentView(R.layout.pwmdemo);  
  92.   
  93.         initUI();  
  94.         initData();  
  95.   
  96.     }  
  97.   
  98.     @Override  
  99.     protected void onDestroy() {  
  100.         mStop = true;  
  101.         super.onDestroy();  
  102.     }  
  103.   
  104.     /** 
  105.      *  
  106.      * initUI: 初始化UI 
  107.      * 
  108.      * @param    
  109.      * @return      
  110.      * @throws  
  111.      */  
  112.     public void initUI(){  
  113.         mButtonSub = (Button)findViewById(R.id.ButtonSUB);  
  114.         mButtonAdd = (Button)findViewById(R.id.ButtonADD);  
  115.   
  116.         /*按钮监听器,具体实现参考下面的函数:onClick*/  
  117.         mButtonSub.setOnClickListener(this);  
  118.         mButtonAdd.setOnClickListener(this);  
  119.   
  120.         mToggleButtonStartForEver= (ToggleButton)findViewById(R.id.ToggleButtonStartForEver);  
  121.         mToggleButtonStartForTimes = (ToggleButton)findViewById(R.id.ToggleButtonStartForTimes);  
  122.   
  123.         /*开关按钮监听器,具体实现参考下面的函数:onCheckedChanged*/  
  124.         mToggleButtonStartForEver.setOnCheckedChangeListener(this);  
  125.         mToggleButtonStartForTimes.setOnCheckedChangeListener(this);  
  126.   
  127.         mEditTextFrequency = (EditText)findViewById(R.id.EditTextFrequency);  
  128.         mEditTextPlayPeriod = (EditText)findViewById(R.id.EditTextPlayPeriod);  
  129.         mTextViewWaitPeriod = (EditText)findViewById(R.id.EditTextWaitPeriod);  
  130.         mEditTextPlayNum = (EditText)findViewById(R.id.EditTextPlayNum);  
  131.   
  132.         /*EditText监听器,具体实现参考下面的函数:onTextChanged 
  133.          * 主要用于获取更改后最新的数值,下同。 
  134.          * */  
  135.         mEditTextFrequency.addTextChangedListener(new TextWatcher() {  
  136.   
  137.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  138.                 // TODO Auto-generated method stub   
  139.                 try {  
  140.                     mfrequency = Integer.parseInt(mEditTextFrequency.getText().toString());  
  141.                 } catch (NumberFormatException e) {  
  142.                     // TODO Auto-generated catch block   
  143.                     e.printStackTrace();  
  144.                 }  
  145.             }  
  146.   
  147.             public void beforeTextChanged(CharSequence s, int start, int count,  
  148.                     int after) {  
  149.                 // TODO Auto-generated method stub   
  150.   
  151.             }  
  152.   
  153.             public void afterTextChanged(Editable s) {  
  154.                 // TODO Auto-generated method stub   
  155.   
  156.             }  
  157.         });  
  158.   
  159.         mEditTextPlayPeriod.addTextChangedListener(new TextWatcher() {  
  160.   
  161.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  162.                 // TODO Auto-generated method stub   
  163.                 try {  
  164.                     mplayperiod = Integer.parseInt(mEditTextPlayPeriod.getText().toString());  
  165.                 } catch (NumberFormatException e) {  
  166.                     // TODO Auto-generated catch block   
  167.                     e.printStackTrace();  
  168.                 }  
  169.             }  
  170.   
  171.             public void beforeTextChanged(CharSequence s, int start, int count,  
  172.                     int after) {  
  173.                 // TODO Auto-generated method stub   
  174.   
  175.             }  
  176.   
  177.             public void afterTextChanged(Editable s) {  
  178.                 // TODO Auto-generated method stub   
  179.   
  180.             }  
  181.         });  
  182.   
  183.         mTextViewWaitPeriod.addTextChangedListener(new TextWatcher() {  
  184.   
  185.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  186.                 // TODO Auto-generated method stub   
  187.                 try {  
  188.                     mwaitperiod = Integer.parseInt(mTextViewWaitPeriod.getText().toString());  
  189.                 } catch (NumberFormatException e) {  
  190.                     // TODO Auto-generated catch block   
  191.                     e.printStackTrace();  
  192.                 }  
  193.             }  
  194.   
  195.             public void beforeTextChanged(CharSequence s, int start, int count,  
  196.                     int after) {  
  197.                 // TODO Auto-generated method stub   
  198.   
  199.             }  
  200.   
  201.             public void afterTextChanged(Editable s) {  
  202.                 // TODO Auto-generated method stub   
  203.   
  204.             }  
  205.         });  
  206.   
  207.         mEditTextPlayNum.addTextChangedListener(new TextWatcher() {  
  208.   
  209.             public void onTextChanged(CharSequence s, int start, int before, int count) {  
  210.                 // TODO Auto-generated method stub   
  211.                 try {  
  212.                     mplaynum = Integer.parseInt(mEditTextPlayNum.getText().toString());  
  213.                 } catch (NumberFormatException e) {  
  214.                     // TODO Auto-generated catch block   
  215.                     e.printStackTrace();  
  216.                 }  
  217.             }  
  218.   
  219.             public void beforeTextChanged(CharSequence s, int start, int count,  
  220.                     int after) {  
  221.                 // TODO Auto-generated method stub   
  222.   
  223.             }  
  224.   
  225.             public void afterTextChanged(Editable s) {  
  226.                 // TODO Auto-generated method stub   
  227.   
  228.             }  
  229.         });  
  230.   
  231.   
  232.     }  
  233.   
  234.     /** 
  235.      *  
  236.      * initData: 初始化各个控件的数值 
  237.      * 
  238.      * @param    
  239.      * @return      
  240.      * @throws  
  241.      */  
  242.     public void initData(){  
  243.         mEditTextFrequency.setText(String.valueOf(mfrequency));  
  244.         mEditTextPlayPeriod.setText(String.valueOf(mplayperiod));  
  245.         mTextViewWaitPeriod.setText(String.valueOf(mwaitperiod));  
  246.         mEditTextPlayNum.setText(String.valueOf(mplaynum));  
  247.     }  
  248.   
  249.   
  250.     /** 
  251.      *  
  252.      * onCheckedChanged: 开关按钮监听器。当有开关按钮的状态改变时,响应点击。 
  253.      * 
  254.      * @param   buttonView 改变状态的开关按钮对象; 
  255.      * @param   isChecked  该开关按钮是否被选中; 
  256.      * @return      
  257.      * @throws  
  258.      */  
  259.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  260.         ToggleButton mToggleButton = (ToggleButton)buttonView;  
  261.   
  262.         switch (mToggleButton.getId()) {  
  263.         case R.id.ToggleButtonStartForEver:  
  264.             if(isChecked){  
  265.                 PWMPlay(mfrequency);  
  266.             }else{  
  267.                 mStop = true;  
  268.                 PWMStop();  
  269.             }  
  270.             break;  
  271.         case R.id.ToggleButtonStartForTimes:  
  272.             if(isChecked){  
  273.                 mStop = false;  
  274.                 PWMThread mPWMThread = new PWMThread();  
  275.                 mPWMThread.start();  
  276.             }else{  
  277.                 mStop = true;  
  278.             }  
  279.             break;        
  280.         default:  
  281.             break;  
  282.         }  
  283.     }  
  284.   
  285.     /** 
  286.      *  
  287.      * onClick: 按钮监听器。当有按钮被点击时,响应点击。 
  288.      * 
  289.      * @param   v 被点击的按钮对象; 
  290.      * @return      
  291.      * @throws  
  292.      */  
  293.     public void onClick(View v) {  
  294.         Button mButton = (Button)v;  
  295.   
  296.         switch (mButton.getId()) {  
  297.         case R.id.ButtonSUB:  
  298.             if(mfrequency <= 1)  
  299.                 mfrequency = PWM_FREQUENCY_1;  
  300.             else if(mfrequency == PWM_FREQUENCY_100)  
  301.                 mfrequency = PWM_FREQUENCY_1;     
  302.             else  
  303.                 mfrequency -= mfrequencystep;  
  304.   
  305.             mEditTextFrequency.setText(String.valueOf(mfrequency));  
  306.             break;  
  307.         case R.id.ButtonADD:  
  308.             if(mfrequency <= 1)  
  309.                 mfrequency = PWM_FREQUENCY_100;  
  310.             else  
  311.                 mfrequency += mfrequencystep;  
  312.   
  313.             mEditTextFrequency.setText(String.valueOf(mfrequency));  
  314.             break;  
  315.         default:  
  316.             break;  
  317.         }  
  318.     }  
  319.   
  320.     /** 
  321.      *  
  322.      * PWMPlay: 启动蜂鸣器 ,并播放。 
  323.      * 
  324.      * @param   frequency 播放频率 
  325.      * @return  true 表示播放成功;否则,表示播放失败。   
  326.      * @throws  
  327.      */  
  328.     public boolean PWMPlay(int frequency)  
  329.     {  
  330.         boolean ret  = false;  
  331.         int result = -1;  
  332.   
  333.         result = HardwareControler.PWMPlay(frequency);  
  334.   
  335.         if(result == 0)  
  336.             ret = true;  
  337.         else  
  338.             ret = false;  
  339.   
  340.         return ret;  
  341.     }  
  342.   
  343.     /** 
  344.      *  
  345.      * PWMStop: 停止并关闭蜂鸣器。 
  346.      * 
  347.      * @param    
  348.      * @return  true 表示停止成功;否则,表示停止失败。   
  349.      * @throws  
  350.      */  
  351.     public boolean PWMStop(){  
  352.         boolean ret  = false;  
  353.         int result = -1;  
  354.   
  355.         result = HardwareControler.PWMStop();  
  356.   
  357.         if(result == 0)  
  358.             ret = true;  
  359.         else  
  360.             ret = false;  
  361.   
  362.         return ret;       
  363.     }  
  364.   
  365.     /** 
  366.      *  
  367.      * PWMThread: PWM播放线程 
  368.      * 
  369.      * @param    
  370.      * @return    
  371.      * @throws  
  372.      */  
  373.     public class PWMThread extends Thread{  
  374.   
  375.         @Override  
  376.         public void run() {  
  377.             Log.i(TAG, "PWMThread Start");  
  378.   
  379.             for(int i = 0 ; i < mplaynum; i++ )  
  380.             {  
  381.                 Log.i(TAG, String.valueOf(i));  
  382.   
  383.                 if(mStop)  
  384.                 {  
  385.                     PWMStop();  
  386.                     break;  
  387.                 }  
  388.   
  389.                 /*启动蜂鸣器,并使用频率 mfrequency 进行播放*/  
  390.                 PWMPlay(mfrequency);  
  391.   
  392.                 /*等待播放时长 mplayperiod 结束*/  
  393.                 try {  
  394.                     sleep(mplayperiod);  
  395.                 } catch (InterruptedException e) {  
  396.                     e.printStackTrace();  
  397.                 }  
  398.                   
  399.                 /*停止蜂鸣器*/  
  400.                 PWMStop();  
  401.   
  402.                 if(mStop)  
  403.                     break;  
  404.   
  405.                 /*等待播放间隔 mwaitperiod 结束*/  
  406.                 try {  
  407.                     sleep(mwaitperiod);  
  408.                 } catch (InterruptedException e) {  
  409.                     e.printStackTrace();  
  410.                 }                 
  411.   
  412.             }  
  413.             Log.i(TAG, "PWMThread Stop");  
  414.         }  
  415.     }  
  416. }  

pwmdemo.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="horizontal" >  
  11.   
  12.         <TextView  
  13.             android:id="@+id/TextViewFrequency"  
  14.             android:layout_width="80dip"  
  15.             android:layout_height="40dip"  
  16.             android:gravity="center"  
  17.             android:text="@string/frequency" />  
  18.   
  19.         <Button  
  20.             android:id="@+id/ButtonSUB"  
  21.             android:layout_width="40dip"  
  22.             android:layout_height="40dip"  
  23.             android:gravity="center"  
  24.             android:text="@string/sub" />  
  25.   
  26.         <EditText  
  27.             android:id="@+id/EditTextFrequency"  
  28.             android:layout_width="120dip"  
  29.             android:layout_height="40dip"  
  30.             android:gravity="center"  
  31.             android:inputType="number" />  
  32.   
  33.         <Button  
  34.             android:id="@+id/ButtonADD"  
  35.             android:layout_width="40dip"  
  36.             android:layout_height="40dip"  
  37.             android:gravity="center"  
  38.             android:text="@string/add" />  
  39.     </LinearLayout>  
  40.   
  41.     <LinearLayout  
  42.         android:layout_width="match_parent"  
  43.         android:layout_height="wrap_content"  
  44.         android:orientation="horizontal" >  
  45.   
  46.         <TextView  
  47.             android:id="@+id/TextViewPlayPeriod"  
  48.             android:layout_width="80dip"  
  49.             android:layout_height="40dip"  
  50.             android:gravity="center"  
  51.             android:text="@string/playperiod" />  
  52.   
  53.         <EditText  
  54.             android:id="@+id/EditTextPlayPeriod"  
  55.             android:layout_width="200dip"  
  56.             android:layout_height="40dip"  
  57.             android:gravity

抱歉!评论已关闭.