查看: 4287|回复: 6

靶子

[复制链接]
  • TA的每日心情
    郁闷
    1 小时前
  • 签到天数: 1648 天

    连续签到: 5 天

    [LV.Master]伴坛终老

    发表于 2012-10-16 11:37:46 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 nemon 于 2012-10-16 11:41 编辑

    国外有人用树莓派做靶子:

    近景:


    被打中,会给射击者照相,用的这个网络摄像头:

    摄像头 Dropcam据说很好用,官网www.dropcam.com售价149$,直接从https://nexusapi.dropcam.com/get_image?width=800&uuid=XXXXX就可以得到jpg(忒好用了,怪不得有那么多xx门)
    不废话了,上代码——
    脚本:
    1. #!/bin/bash
    2. #/etc/init.d/target

    3. export HOME
    4. case "$1" in
    5.   start)
    6.      if [ ! -e "/dev/ttyS1" ]; then
    7.        ln /dev/ttyAMA0 /dev/ttyS1
    8.      fi     
    9.      /home/pi/lcd.py `ifconfig wlan2 | grep inet | awk '{print $2}' | sed 's/addr://'`
    10.      java -cp .:/home/pi:/home/pi/lib/* Target >> /home/pi/target.log &
    11.      /home/pi/shutdown-listener.py &
    12.      ;;
    13.   stop)
    14.      LCD_PID=`ps auxwww | grep Target | head -1 | awk '{print $2}'`
    15.      kill $LCD_PID   
    16.      ;;
    17.   *)
    18.     echo "Usage: /etc/init.d/target {start|stop}"
    19.     exit 1
    20.     ;;
    21. esac


    22. exit 0
    复制代码
    java(能看出那个头怎么用了吧):
    1. import gnu.io.CommPortIdentifier;
    2. import gnu.io.SerialPort;
    3. import java.io.OutputStream;
    4. import be.doubleyouit.raspberry.gpio.Boardpin;
    5. import be.doubleyouit.raspberry.gpio.Direction;
    6. import be.doubleyouit.raspberry.gpio.GpioGateway;
    7. import be.doubleyouit.raspberry.gpio.impl.GpioGatewayImpl;

    8. import java.io.BufferedReader;
    9. import java.io.File;
    10. import java.io.FileOutputStream;
    11. import java.io.IOException;
    12. import java.io.InputStreamReader;
    13. import java.text.SimpleDateFormat;
    14. import java.util.Date;
    15. import javax.activation.MimetypesFileTypeMap;
    16. import org.apache.http.HttpEntity;
    17. import org.apache.http.HttpResponse;
    18. import org.apache.http.client.HttpClient;
    19. import org.apache.http.client.methods.HttpGet;
    20. import org.apache.http.client.methods.HttpPost;
    21. import org.apache.http.client.methods.HttpPut;
    22. import org.apache.http.entity.FileEntity;
    23. import org.apache.http.entity.StringEntity;
    24. import org.apache.http.impl.client.DefaultHttpClient;
    25. import org.apache.http.util.EntityUtils;

    26. import twitter4j.StatusUpdate;
    27. import twitter4j.Twitter;
    28. import twitter4j.TwitterException;
    29. import twitter4j.TwitterFactory;
    30. import twitter4j.conf.ConfigurationBuilder;

    31. import java.util.Random;

    32. public class Target {
    33.     static OutputStream output;
    34.     static GpioGateway gpio;
    35.     static SerialPort port;
    36.     static CommPortIdentifier portId;

    37.     static HttpClient httpclient = new DefaultHttpClient();
    38.     static String replyURL;
    39.     static String message;
    40.     static boolean ready = true;
    41.     static String dropcam = "https://nexusapi.dropcam.com/get_image?width=800&uuid=XXXX";
    42.     static String osnImg;
    43.     public static Date now;
    44.     static Random generator = new Random();
    45.    
    46.     public static String[] messages = {
    47.         "Lucky! Looks like you will be taking me home tonight!",
    48.         "It paid off to stop by the OTN Lounge tonight.",
    49.         "Looks like someone has been practicing.",
    50.         "Not bad for your first try. Now go ahead and brag to your friends.",
    51.         "I love hanging out at the OTN Lounge, but It looks like you will be taking me home tonight!",
    52.         "That was nice. No go and tell your friends to stop by the OTN Lounge.",
    53.         "I just love to scream and fly at the OTN Lounge.",
    54.         "Roses are red and Oracle too. Now you can take me home too.",
    55.         "I just wanna fly.",
    56.         "Thanks for stopping by. Looks like you got lucky tonight.",
    57.         "I just love to scream and fly at the OTN Lounge."
    58.         };

    59.     public static void main(String[] args) throws Exception{
    60.         AddShutdownHookSample kill = new AddShutdownHookSample();
    61.         kill.attachShutDownHook();
    62.         

    63.         try{
    64.             portId = CommPortIdentifier.getPortIdentifier("/dev/ttyS1");
    65.             port = (SerialPort)portId.open("Raspi LCD", 4000);
    66.             output = port.getOutputStream();
    67.             port.setSerialPortParams(9600,SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
    68.             

    69.             gpio = new GpioGatewayImpl();
    70.             gpio.unexport(Boardpin.PIN11_GPIO17);
    71.             gpio.export(Boardpin.PIN11_GPIO17);
    72.             gpio.setDirection(Boardpin.PIN11_GPIO17, Direction.IN);
    73.             
    74.             while (true){

    75.                     brightness("9");
    76.                     clear();
    77.                     print("     Ready      ");
    78.                     delayBacklight("0",5);
    79.                     int i = 0;
    80.                 //remove negation!
    81.                 while (!gpio.getValue(Boardpin.PIN11_GPIO17)){
    82.                    i = 1;
    83.                 }

    84.                 if (ready){
    85.                     //System.out.println("start");
    86.                     ready = false;
    87.                     int rnd = generator.nextInt(10);
    88.                     Process proc = Runtime.getRuntime().exec(new String[]{"/usr/bin/mpg321", "-q","/home/pi/"+rnd+".mp3"});
    89.                     brightness("9");
    90.                     clear();
    91.                     print(" Congratulations");
    92.                     //Login
    93.                     downloadPicture();
    94.                     uploadPicture(messages[rnd]);        

    95.                     clear();
    96.                     delayBacklight("0",5);
    97.                     Thread.sleep(2 * 5000);
    98.                     ready = true;
    99.                     //System.out.println("end");
    100.                 }

    101.             }   
    102.         }catch (Exception e){
    103.             System.out.println(e.toString());
    104.         }
    105.     }




    106.     public static void downloadPicture() throws IOException{
    107.         now = new Date();
    108.         String formatDate = new SimpleDateFormat("MMddyyyy-hhmmss").format(now);
    109.         osnImg = "OTN-" + formatDate + ".jpeg";
    110.         
    111.         HttpGet httpget = new HttpGet(dropcam);
    112.         HttpResponse response = httpclient.execute(httpget);
    113.         HttpEntity entity = response.getEntity();
    114.         //System.out.println("downloadPicture: " +response.getStatusLine());
    115.         if (entity != null) {

    116.             byte[] bytes = EntityUtils.toByteArray(entity);
    117.             File file = new File("/var/www/" + osnImg);
    118.             FileOutputStream fos = new FileOutputStream(file);
    119.             fos.write(bytes);
    120.             fos.close();     
    121.                
    122.         }   
    123.         EntityUtils.consume(entity);
    124.         
    125.     }   

    126.     public static void uploadPicture(String message) throws TwitterException {
    127.         ConfigurationBuilder cb = new ConfigurationBuilder();
    128.         cb.setOAuthConsumerKey("XXXX");
    129.         cb.setOAuthConsumerSecret("XXXX");
    130.         cb.setOAuthAccessToken("XXXX");
    131.         cb.setOAuthAccessTokenSecret("XXXX");

    132.         StatusUpdate status = new StatusUpdate(message);

    133.         File imageFile = new File ("/var/www/" +  osnImg);
    134.         status.setMedia(imageFile);
    135.                
    136.         TwitterFactory tf = new TwitterFactory(cb.build());
    137.         Twitter twitter = tf.getInstance();
    138.             
    139.                
    140.         try {
    141.             twitter.updateStatus(status);
    142.         } catch ( Exception ex ) {
    143.             ex.printStackTrace();
    144.         }

    145.     }


    146.     public static void delayBacklight(String level, int delay ) throws Exception{
    147.         startCommand();
    148.         output.write("T".getBytes());
    149.         output.write(level.getBytes());
    150.         output.write(delay);
    151.     }

    152.     public static void brightness(String level) throws Exception{
    153.         startCommand();
    154.         output.write("B".getBytes());
    155.         output.write(level.getBytes());
    156.     }

    157.     public static void print(String message) throws Exception{
    158.         output.write(message.getBytes());
    159.     }

    160.     public static void clear() throws Exception{
    161.         startCommand();
    162.         output.write("C".getBytes());
    163.     }

    164.     public static void startCommand() throws Exception{
    165.         output.write(254);
    166.     }

    167. }

    168. //http://www.javabeat.net/2010/11/runtime-addshutdownhook/
    169. class AddShutdownHookSample {
    170.   void attachShutDownHook() {
    171.   Runtime.getRuntime().addShutdownHook(new Thread() {
    172.    @Override
    173.    public void run(){
    174.     try{
    175.         System.out.println("Shuting down target");
    176.         File file = new File("/var/lock/LCK..ttyS1");
    177.         Lcd.httpclient.getConnectionManager().shutdown();
    178.         if(file.delete()){
    179.             System.out.println(file.getName() + " is deleted!");
    180.         }
    181.     }catch(Exception e){
    182.         e.printStackTrace();
    183.     }
    184.    }
    185.   });
    186.     //System.out.println("Shut Down Hook Attached.");
    187. }
    188. }
    复制代码
    简言之,等着直到被射中,然后抓张图

    话说有点大材小用



    回复

    使用道具 举报

  • TA的每日心情
    奋斗
    2015-11-4 19:13
  • 签到天数: 281 天

    连续签到: 1 天

    [LV.8]以坛为家I

    发表于 2012-10-16 11:41:50 | 显示全部楼层
    这个有创意
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    发表于 2012-10-16 11:42:38 | 显示全部楼层
    如果打不中呢,
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    郁闷
    1 小时前
  • 签到天数: 1648 天

    连续签到: 5 天

    [LV.Master]伴坛终老

    发表于 2012-10-16 12:03:14 | 显示全部楼层
    本帖最后由 nemon 于 2012-10-16 12:06 编辑
    tina 发表于 2012-10-16 11:42
    如果打不中呢,

    打不中就是个死循环,一直显示ready:
    while (!gpio.getValue(Boardpin.PIN11_GPIO17)){
    i = 1;
    }
    确实有完善的空间,比如可以给弓弩做做改装,这样知道射击了几次,把屡击不中的也留个影
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2016-8-25 12:42
  • 签到天数: 692 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2012-10-16 12:41:05 | 显示全部楼层
    这个不错啊,顶一下!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    奋斗
    2020-9-28 10:10
  • 签到天数: 1018 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2012-10-16 13:19:33 | 显示全部楼层
    楼主有兴致也做个让大家玩玩啊!
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2012-11-23 16:50
  • 签到天数: 15 天

    连续签到: 1 天

    [LV.4]偶尔看看III

    发表于 2012-10-16 18:24:03 | 显示全部楼层
    哈哈,老外真会想呀。。。。
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-5-20 19:06 , Processed in 0.178489 second(s), 26 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.